Skip to main content

nautilus_binance/spot/sbe/generated/
book_ticker_response_codec.rs

1pub use decoder::BookTickerResponseDecoder;
2pub use encoder::BookTickerResponseEncoder;
3
4use super::*;
5pub use super::{SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_SEMANTIC_VERSION};
6
7pub const SBE_BLOCK_LENGTH: u16 = 0;
8pub const SBE_TEMPLATE_ID: u16 = 212;
9
10pub mod encoder {
11    use message_header_codec::*;
12
13    use super::*;
14
15    #[derive(Debug, Default)]
16    pub struct BookTickerResponseEncoder<'a> {
17        buf: WriteBuf<'a>,
18        initial_offset: usize,
19        offset: usize,
20        limit: usize,
21    }
22
23    impl<'a> Writer<'a> for BookTickerResponseEncoder<'a> {
24        #[inline]
25        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
26            &mut self.buf
27        }
28    }
29
30    impl<'a> Encoder<'a> for BookTickerResponseEncoder<'a> {
31        #[inline]
32        fn get_limit(&self) -> usize {
33            self.limit
34        }
35
36        #[inline]
37        fn set_limit(&mut self, limit: usize) {
38            self.limit = limit;
39        }
40    }
41
42    impl<'a> BookTickerResponseEncoder<'a> {
43        pub fn wrap(mut self, buf: WriteBuf<'a>, offset: usize) -> Self {
44            let limit = offset + SBE_BLOCK_LENGTH as usize;
45            self.buf = buf;
46            self.initial_offset = offset;
47            self.offset = offset;
48            self.limit = limit;
49            self
50        }
51
52        #[inline]
53        pub fn encoded_length(&self) -> usize {
54            self.limit - self.offset
55        }
56
57        pub fn header(self, offset: usize) -> MessageHeaderEncoder<Self> {
58            let mut header = MessageHeaderEncoder::default().wrap(self, offset);
59            header.block_length(SBE_BLOCK_LENGTH);
60            header.template_id(SBE_TEMPLATE_ID);
61            header.schema_id(SBE_SCHEMA_ID);
62            header.version(SBE_SCHEMA_VERSION);
63            header
64        }
65
66        /// GROUP ENCODER (id=100)
67        #[inline]
68        pub fn tickers_encoder(
69            self,
70            count: u32,
71            tickers_encoder: TickersEncoder<Self>,
72        ) -> TickersEncoder<Self> {
73            tickers_encoder.wrap(self, count)
74        }
75    }
76
77    #[derive(Debug, Default)]
78    pub struct TickersEncoder<P> {
79        parent: Option<P>,
80        count: u32,
81        index: usize,
82        offset: usize,
83        initial_limit: usize,
84    }
85
86    impl<'a, P> Writer<'a> for TickersEncoder<P>
87    where
88        P: Writer<'a> + Default,
89    {
90        #[inline]
91        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
92            if let Some(parent) = self.parent.as_mut() {
93                parent.get_buf_mut()
94            } else {
95                panic!("parent was None")
96            }
97        }
98    }
99
100    impl<'a, P> Encoder<'a> for TickersEncoder<P>
101    where
102        P: Encoder<'a> + Default,
103    {
104        #[inline]
105        fn get_limit(&self) -> usize {
106            self.parent.as_ref().expect("parent missing").get_limit()
107        }
108
109        #[inline]
110        fn set_limit(&mut self, limit: usize) {
111            self.parent
112                .as_mut()
113                .expect("parent missing")
114                .set_limit(limit);
115        }
116    }
117
118    impl<'a, P> TickersEncoder<P>
119    where
120        P: Encoder<'a> + Default,
121    {
122        #[inline]
123        pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
124            let initial_limit = parent.get_limit();
125            parent.set_limit(initial_limit + 6);
126            parent
127                .get_buf_mut()
128                .put_u16_at(initial_limit, Self::block_length());
129            parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
130            self.parent = Some(parent);
131            self.count = count;
132            self.index = usize::MAX;
133            self.offset = usize::MAX;
134            self.initial_limit = initial_limit;
135            self
136        }
137
138        #[inline]
139        pub fn block_length() -> u16 {
140            34
141        }
142
143        #[inline]
144        pub fn parent(&mut self) -> SbeResult<P> {
145            self.parent.take().ok_or(SbeErr::ParentNotSet)
146        }
147
148        /// will return Some(current index) when successful otherwise None
149        #[inline]
150        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
151            let index = self.index.wrapping_add(1);
152            if index >= self.count as usize {
153                return Ok(None);
154            }
155
156            if let Some(parent) = self.parent.as_mut() {
157                self.offset = parent.get_limit();
158                parent.set_limit(self.offset + Self::block_length() as usize);
159                self.index = index;
160                Ok(Some(index))
161            } else {
162                Err(SbeErr::ParentNotSet)
163            }
164        }
165
166        /// primitive field 'priceExponent'
167        /// - min value: -127
168        /// - max value: 127
169        /// - null value: -128_i8
170        /// - characterEncoding: null
171        /// - semanticType: null
172        /// - encodedOffset: 0
173        /// - encodedLength: 1
174        /// - version: 0
175        #[inline]
176        pub fn price_exponent(&mut self, value: i8) {
177            let offset = self.offset;
178            self.get_buf_mut().put_i8_at(offset, value);
179        }
180
181        /// primitive field 'qtyExponent'
182        /// - min value: -127
183        /// - max value: 127
184        /// - null value: -128_i8
185        /// - characterEncoding: null
186        /// - semanticType: null
187        /// - encodedOffset: 1
188        /// - encodedLength: 1
189        /// - version: 0
190        #[inline]
191        pub fn qty_exponent(&mut self, value: i8) {
192            let offset = self.offset + 1;
193            self.get_buf_mut().put_i8_at(offset, value);
194        }
195
196        /// primitive field 'bidPrice'
197        /// - min value: -9223372036854775807
198        /// - max value: 9223372036854775807
199        /// - null value: -9223372036854775808_i64
200        /// - characterEncoding: null
201        /// - semanticType: null
202        /// - encodedOffset: 2
203        /// - encodedLength: 8
204        /// - version: 0
205        #[inline]
206        pub fn bid_price(&mut self, value: i64) {
207            let offset = self.offset + 2;
208            self.get_buf_mut().put_i64_at(offset, value);
209        }
210
211        /// primitive field 'bidQty'
212        /// - min value: -9223372036854775807
213        /// - max value: 9223372036854775807
214        /// - null value: -9223372036854775808_i64
215        /// - characterEncoding: null
216        /// - semanticType: null
217        /// - encodedOffset: 10
218        /// - encodedLength: 8
219        /// - version: 0
220        #[inline]
221        pub fn bid_qty(&mut self, value: i64) {
222            let offset = self.offset + 10;
223            self.get_buf_mut().put_i64_at(offset, value);
224        }
225
226        /// primitive field 'askPrice'
227        /// - min value: -9223372036854775807
228        /// - max value: 9223372036854775807
229        /// - null value: -9223372036854775808_i64
230        /// - characterEncoding: null
231        /// - semanticType: null
232        /// - encodedOffset: 18
233        /// - encodedLength: 8
234        /// - version: 0
235        #[inline]
236        pub fn ask_price(&mut self, value: i64) {
237            let offset = self.offset + 18;
238            self.get_buf_mut().put_i64_at(offset, value);
239        }
240
241        /// primitive field 'askQty'
242        /// - min value: -9223372036854775807
243        /// - max value: 9223372036854775807
244        /// - null value: -9223372036854775808_i64
245        /// - characterEncoding: null
246        /// - semanticType: null
247        /// - encodedOffset: 26
248        /// - encodedLength: 8
249        /// - version: 0
250        #[inline]
251        pub fn ask_qty(&mut self, value: i64) {
252            let offset = self.offset + 26;
253            self.get_buf_mut().put_i64_at(offset, value);
254        }
255
256        /// VAR_DATA ENCODER - character encoding: 'UTF-8'
257        #[inline]
258        pub fn symbol(&mut self, value: &str) {
259            let limit = self.get_limit();
260            let data_length = value.len();
261            self.set_limit(limit + 1 + data_length);
262            self.get_buf_mut().put_u8_at(limit, data_length as u8);
263            self.get_buf_mut().put_slice_at(limit + 1, value.as_bytes());
264        }
265    }
266} // end encoder
267
268pub mod decoder {
269    use message_header_codec::*;
270
271    use super::*;
272
273    #[derive(Clone, Copy, Debug, Default)]
274    pub struct BookTickerResponseDecoder<'a> {
275        buf: ReadBuf<'a>,
276        initial_offset: usize,
277        offset: usize,
278        limit: usize,
279        pub acting_block_length: u16,
280        pub acting_version: u16,
281    }
282
283    impl ActingVersion for BookTickerResponseDecoder<'_> {
284        #[inline]
285        fn acting_version(&self) -> u16 {
286            self.acting_version
287        }
288    }
289
290    impl<'a> Reader<'a> for BookTickerResponseDecoder<'a> {
291        #[inline]
292        fn get_buf(&self) -> &ReadBuf<'a> {
293            &self.buf
294        }
295    }
296
297    impl<'a> Decoder<'a> for BookTickerResponseDecoder<'a> {
298        #[inline]
299        fn get_limit(&self) -> usize {
300            self.limit
301        }
302
303        #[inline]
304        fn set_limit(&mut self, limit: usize) {
305            self.limit = limit;
306        }
307    }
308
309    impl<'a> BookTickerResponseDecoder<'a> {
310        pub fn wrap(
311            mut self,
312            buf: ReadBuf<'a>,
313            offset: usize,
314            acting_block_length: u16,
315            acting_version: u16,
316        ) -> Self {
317            let limit = offset + acting_block_length as usize;
318            self.buf = buf;
319            self.initial_offset = offset;
320            self.offset = offset;
321            self.limit = limit;
322            self.acting_block_length = acting_block_length;
323            self.acting_version = acting_version;
324            self
325        }
326
327        #[inline]
328        pub fn encoded_length(&self) -> usize {
329            self.limit - self.offset
330        }
331
332        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
333            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
334            let acting_block_length = header.block_length();
335            let acting_version = header.version();
336
337            self.wrap(
338                header.parent().unwrap(),
339                offset + message_header_codec::ENCODED_LENGTH,
340                acting_block_length,
341                acting_version,
342            )
343        }
344
345        /// GROUP DECODER (id=100)
346        #[inline]
347        pub fn tickers_decoder(self) -> TickersDecoder<Self> {
348            TickersDecoder::default().wrap(self)
349        }
350    }
351
352    #[derive(Debug, Default)]
353    pub struct TickersDecoder<P> {
354        parent: Option<P>,
355        block_length: u16,
356        count: u32,
357        index: usize,
358        offset: usize,
359    }
360
361    impl<'a, P> ActingVersion for TickersDecoder<P>
362    where
363        P: Reader<'a> + ActingVersion + Default,
364    {
365        #[inline]
366        fn acting_version(&self) -> u16 {
367            self.parent.as_ref().unwrap().acting_version()
368        }
369    }
370
371    impl<'a, P> Reader<'a> for TickersDecoder<P>
372    where
373        P: Reader<'a> + Default,
374    {
375        #[inline]
376        fn get_buf(&self) -> &ReadBuf<'a> {
377            self.parent.as_ref().expect("parent missing").get_buf()
378        }
379    }
380
381    impl<'a, P> Decoder<'a> for TickersDecoder<P>
382    where
383        P: Decoder<'a> + ActingVersion + Default,
384    {
385        #[inline]
386        fn get_limit(&self) -> usize {
387            self.parent.as_ref().expect("parent missing").get_limit()
388        }
389
390        #[inline]
391        fn set_limit(&mut self, limit: usize) {
392            self.parent
393                .as_mut()
394                .expect("parent missing")
395                .set_limit(limit);
396        }
397    }
398
399    impl<'a, P> TickersDecoder<P>
400    where
401        P: Decoder<'a> + ActingVersion + Default,
402    {
403        pub fn wrap(mut self, mut parent: P) -> Self {
404            let initial_offset = parent.get_limit();
405            let block_length = parent.get_buf().get_u16_at(initial_offset);
406            let count = parent.get_buf().get_u32_at(initial_offset + 2);
407            parent.set_limit(initial_offset + 6);
408            self.parent = Some(parent);
409            self.block_length = block_length;
410            self.count = count;
411            self.index = usize::MAX;
412            self.offset = 0;
413            self
414        }
415
416        /// group token - Token{signal=BEGIN_GROUP, name='tickers', referencedName='null', description='null', packageName='null', id=100, version=0, deprecated=0, encodedLength=34, offset=0, componentTokenCount=30, encoding=Encoding{presence=REQUIRED, primitiveType=null, byteOrder=LITTLE_ENDIAN, minValue=null, maxValue=null, nullValue=null, constValue=null, characterEncoding='null', epoch='null', timeUnit=null, semanticType='null'}}
417        #[inline]
418        pub fn parent(&mut self) -> SbeResult<P> {
419            self.parent.take().ok_or(SbeErr::ParentNotSet)
420        }
421
422        #[inline]
423        pub fn acting_version(&mut self) -> u16 {
424            self.parent.as_ref().unwrap().acting_version()
425        }
426
427        #[inline]
428        pub fn count(&self) -> u32 {
429            self.count
430        }
431
432        /// will return Some(current index) when successful otherwise None
433        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
434            let index = self.index.wrapping_add(1);
435            if index >= self.count as usize {
436                return Ok(None);
437            }
438
439            if let Some(parent) = self.parent.as_mut() {
440                self.offset = parent.get_limit();
441                parent.set_limit(self.offset + self.block_length as usize);
442                self.index = index;
443                Ok(Some(index))
444            } else {
445                Err(SbeErr::ParentNotSet)
446            }
447        }
448
449        /// primitive field - 'REQUIRED'
450        #[inline]
451        pub fn price_exponent(&self) -> i8 {
452            self.get_buf().get_i8_at(self.offset)
453        }
454
455        /// primitive field - 'REQUIRED'
456        #[inline]
457        pub fn qty_exponent(&self) -> i8 {
458            self.get_buf().get_i8_at(self.offset + 1)
459        }
460
461        /// primitive field - 'OPTIONAL' { null_value: '-9223372036854775808_i64' }
462        #[inline]
463        pub fn bid_price(&self) -> Option<i64> {
464            let value = self.get_buf().get_i64_at(self.offset + 2);
465            if value == -9223372036854775808_i64 {
466                None
467            } else {
468                Some(value)
469            }
470        }
471
472        /// primitive field - 'REQUIRED'
473        #[inline]
474        pub fn bid_qty(&self) -> i64 {
475            self.get_buf().get_i64_at(self.offset + 10)
476        }
477
478        /// primitive field - 'OPTIONAL' { null_value: '-9223372036854775808_i64' }
479        #[inline]
480        pub fn ask_price(&self) -> Option<i64> {
481            let value = self.get_buf().get_i64_at(self.offset + 18);
482            if value == -9223372036854775808_i64 {
483                None
484            } else {
485                Some(value)
486            }
487        }
488
489        /// primitive field - 'REQUIRED'
490        #[inline]
491        pub fn ask_qty(&self) -> i64 {
492            self.get_buf().get_i64_at(self.offset + 26)
493        }
494
495        /// VAR_DATA DECODER - character encoding: 'UTF-8'
496        #[inline]
497        pub fn symbol_decoder(&mut self) -> (usize, usize) {
498            let offset = self.parent.as_ref().expect("parent missing").get_limit();
499            let data_length = self.get_buf().get_u8_at(offset) as usize;
500            self.parent
501                .as_mut()
502                .unwrap()
503                .set_limit(offset + 1 + data_length);
504            (offset + 1, data_length)
505        }
506
507        #[inline]
508        pub fn symbol_slice(&'a self, coordinates: (usize, usize)) -> &'a [u8] {
509            debug_assert!(self.get_limit() >= coordinates.0 + coordinates.1);
510            self.get_buf().get_slice_at(coordinates.0, coordinates.1)
511        }
512    }
513} // end decoder