11use napi_derive:: napi;
22
3- use napi:: { bindgen_prelude:: { BufferSlice , JsValuesTuple } , Env , Result } ;
3+ use napi:: {
4+ bindgen_prelude:: { BufferSlice , JsValuesTuple } ,
5+ Env , Result ,
6+ } ;
47
58#[ napi]
69pub struct BinaryStream < ' env > {
710 /**
811 * The buffer reference that this stream will read/write to.
9- */
12+ */
1013 buffer : BufferSlice < ' env > ,
1114
1215 /**
1316 * The current offset in the buffer where the next read/write will occur.
14- */
17+ */
1518 offset : usize ,
1619}
1720
@@ -67,7 +70,7 @@ impl<'env> BinaryStream<'env> {
6770
6871 /**
6972 * Returns the current offset in the stream.
70- */
73+ */
7174 #[ napi( js_name = "getOffset" ) ]
7275 pub fn get_offset_napi ( & self ) -> Result < u32 > {
7376 // Cast the offset to u32 and return it
@@ -77,7 +80,7 @@ impl<'env> BinaryStream<'env> {
7780 /**
7881 * Sets the current offset in the stream.
7982 * If the offset is out of bounds, it returns an error.
80- */
83+ */
8184 #[ napi( js_name = "setOffset" ) ]
8285 pub fn set_offset_napi ( & mut self , offset : u32 ) -> Result < ( ) > {
8386 // Check if the offset is within the bounds of the buffer
@@ -113,7 +116,7 @@ impl<'env> BinaryStream<'env> {
113116 Ok ( ( ) )
114117 }
115118
116- #[ napi( js_name = "read" ) ]
119+ #[ napi( js_name = "read" ) ]
117120 pub fn read_napi ( & mut self , length : u32 ) -> Result < BufferSlice < ' env > > {
118121 // Check if the read will exceed the buffer length
119122 if self . offset + length as usize > self . buffer . len ( ) {
@@ -133,16 +136,16 @@ impl<'env> BinaryStream<'env> {
133136 let env = & Env :: from_raw ( self . buffer . env ( ) ) ;
134137
135138 // Create a BufferSlice from the slice
136- let buffer = BufferSlice :: from_data ( env , slice )
137- . expect ( "Failed to create BufferSlice from data" ) ;
139+ let buffer =
140+ BufferSlice :: from_data ( env , slice ) . expect ( "Failed to create BufferSlice from data" ) ;
138141
139142 // Return the slice as a BufferSlice
140143 Ok ( buffer)
141144 }
142145
143146 /**
144147 * Checks if the stream has reached the end of the buffer.
145- */
148+ */
146149 #[ napi]
147150 pub fn feof ( & self ) -> bool {
148151 // Check if the current offset is at the end of the buffer
@@ -151,10 +154,10 @@ impl<'env> BinaryStream<'env> {
151154
152155 /**
153156 * Resets the stream's offset to the beginning of the buffer.
154- */
157+ */
155158 #[ napi]
156159 pub fn reset ( & mut self ) {
157160 // Reset the offset to 0
158161 self . offset = 0 ;
159162 }
160- }
163+ }
0 commit comments