Skip to main content

nautilus_binance/spot/sbe/generated/
depth_response_codec.rs

1pub use decoder::DepthResponseDecoder;
2pub use encoder::DepthResponseEncoder;
3
4use super::*;
5pub use super::{SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_SEMANTIC_VERSION};
6
7pub const SBE_BLOCK_LENGTH: u16 = 10;
8pub const SBE_TEMPLATE_ID: u16 = 200;
9
10pub mod encoder {
11    use message_header_codec::*;
12
13    use super::*;
14
15    #[derive(Debug, Default)]
16    pub struct DepthResponseEncoder<'a> {
17        buf: WriteBuf<'a>,
18        initial_offset: usize,
19        offset: usize,
20        limit: usize,
21    }
22
23    impl<'a> Writer<'a> for DepthResponseEncoder<'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 DepthResponseEncoder<'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> DepthResponseEncoder<'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        /// primitive field 'lastUpdateId'
67        /// - min value: -9223372036854775807
68        /// - max value: 9223372036854775807
69        /// - null value: -9223372036854775808_i64
70        /// - characterEncoding: null
71        /// - semanticType: null
72        /// - encodedOffset: 0
73        /// - encodedLength: 8
74        /// - version: 0
75        #[inline]
76        pub fn last_update_id(&mut self, value: i64) {
77            let offset = self.offset;
78            self.get_buf_mut().put_i64_at(offset, value);
79        }
80
81        /// primitive field 'priceExponent'
82        /// - min value: -127
83        /// - max value: 127
84        /// - null value: -128_i8
85        /// - characterEncoding: null
86        /// - semanticType: null
87        /// - encodedOffset: 8
88        /// - encodedLength: 1
89        /// - version: 0
90        #[inline]
91        pub fn price_exponent(&mut self, value: i8) {
92            let offset = self.offset + 8;
93            self.get_buf_mut().put_i8_at(offset, value);
94        }
95
96        /// primitive field 'qtyExponent'
97        /// - min value: -127
98        /// - max value: 127
99        /// - null value: -128_i8
100        /// - characterEncoding: null
101        /// - semanticType: null
102        /// - encodedOffset: 9
103        /// - encodedLength: 1
104        /// - version: 0
105        #[inline]
106        pub fn qty_exponent(&mut self, value: i8) {
107            let offset = self.offset + 9;
108            self.get_buf_mut().put_i8_at(offset, value);
109        }
110
111        /// GROUP ENCODER (id=100)
112        #[inline]
113        pub fn bids_encoder(
114            self,
115            count: u32,
116            bids_encoder: BidsEncoder<Self>,
117        ) -> BidsEncoder<Self> {
118            bids_encoder.wrap(self, count)
119        }
120
121        /// GROUP ENCODER (id=101)
122        #[inline]
123        pub fn asks_encoder(
124            self,
125            count: u32,
126            asks_encoder: AsksEncoder<Self>,
127        ) -> AsksEncoder<Self> {
128            asks_encoder.wrap(self, count)
129        }
130    }
131
132    #[derive(Debug, Default)]
133    pub struct BidsEncoder<P> {
134        parent: Option<P>,
135        count: u32,
136        index: usize,
137        offset: usize,
138        initial_limit: usize,
139    }
140
141    impl<'a, P> Writer<'a> for BidsEncoder<P>
142    where
143        P: Writer<'a> + Default,
144    {
145        #[inline]
146        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
147            if let Some(parent) = self.parent.as_mut() {
148                parent.get_buf_mut()
149            } else {
150                panic!("parent was None")
151            }
152        }
153    }
154
155    impl<'a, P> Encoder<'a> for BidsEncoder<P>
156    where
157        P: Encoder<'a> + Default,
158    {
159        #[inline]
160        fn get_limit(&self) -> usize {
161            self.parent.as_ref().expect("parent missing").get_limit()
162        }
163
164        #[inline]
165        fn set_limit(&mut self, limit: usize) {
166            self.parent
167                .as_mut()
168                .expect("parent missing")
169                .set_limit(limit);
170        }
171    }
172
173    impl<'a, P> BidsEncoder<P>
174    where
175        P: Encoder<'a> + Default,
176    {
177        #[inline]
178        pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
179            let initial_limit = parent.get_limit();
180            parent.set_limit(initial_limit + 6);
181            parent
182                .get_buf_mut()
183                .put_u16_at(initial_limit, Self::block_length());
184            parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
185            self.parent = Some(parent);
186            self.count = count;
187            self.index = usize::MAX;
188            self.offset = usize::MAX;
189            self.initial_limit = initial_limit;
190            self
191        }
192
193        #[inline]
194        pub fn block_length() -> u16 {
195            16
196        }
197
198        #[inline]
199        pub fn parent(&mut self) -> SbeResult<P> {
200            self.parent.take().ok_or(SbeErr::ParentNotSet)
201        }
202
203        /// will return Some(current index) when successful otherwise None
204        #[inline]
205        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
206            let index = self.index.wrapping_add(1);
207            if index >= self.count as usize {
208                return Ok(None);
209            }
210
211            if let Some(parent) = self.parent.as_mut() {
212                self.offset = parent.get_limit();
213                parent.set_limit(self.offset + Self::block_length() as usize);
214                self.index = index;
215                Ok(Some(index))
216            } else {
217                Err(SbeErr::ParentNotSet)
218            }
219        }
220
221        /// primitive field 'price'
222        /// - min value: -9223372036854775807
223        /// - max value: 9223372036854775807
224        /// - null value: -9223372036854775808_i64
225        /// - characterEncoding: null
226        /// - semanticType: null
227        /// - encodedOffset: 0
228        /// - encodedLength: 8
229        /// - version: 0
230        #[inline]
231        pub fn price(&mut self, value: i64) {
232            let offset = self.offset;
233            self.get_buf_mut().put_i64_at(offset, value);
234        }
235
236        /// primitive field 'qty'
237        /// - min value: -9223372036854775807
238        /// - max value: 9223372036854775807
239        /// - null value: -9223372036854775808_i64
240        /// - characterEncoding: null
241        /// - semanticType: null
242        /// - encodedOffset: 8
243        /// - encodedLength: 8
244        /// - version: 0
245        #[inline]
246        pub fn qty(&mut self, value: i64) {
247            let offset = self.offset + 8;
248            self.get_buf_mut().put_i64_at(offset, value);
249        }
250    }
251
252    #[derive(Debug, Default)]
253    pub struct AsksEncoder<P> {
254        parent: Option<P>,
255        count: u32,
256        index: usize,
257        offset: usize,
258        initial_limit: usize,
259    }
260
261    impl<'a, P> Writer<'a> for AsksEncoder<P>
262    where
263        P: Writer<'a> + Default,
264    {
265        #[inline]
266        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
267            if let Some(parent) = self.parent.as_mut() {
268                parent.get_buf_mut()
269            } else {
270                panic!("parent was None")
271            }
272        }
273    }
274
275    impl<'a, P> Encoder<'a> for AsksEncoder<P>
276    where
277        P: Encoder<'a> + Default,
278    {
279        #[inline]
280        fn get_limit(&self) -> usize {
281            self.parent.as_ref().expect("parent missing").get_limit()
282        }
283
284        #[inline]
285        fn set_limit(&mut self, limit: usize) {
286            self.parent
287                .as_mut()
288                .expect("parent missing")
289                .set_limit(limit);
290        }
291    }
292
293    impl<'a, P> AsksEncoder<P>
294    where
295        P: Encoder<'a> + Default,
296    {
297        #[inline]
298        pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
299            let initial_limit = parent.get_limit();
300            parent.set_limit(initial_limit + 6);
301            parent
302                .get_buf_mut()
303                .put_u16_at(initial_limit, Self::block_length());
304            parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
305            self.parent = Some(parent);
306            self.count = count;
307            self.index = usize::MAX;
308            self.offset = usize::MAX;
309            self.initial_limit = initial_limit;
310            self
311        }
312
313        #[inline]
314        pub fn block_length() -> u16 {
315            16
316        }
317
318        #[inline]
319        pub fn parent(&mut self) -> SbeResult<P> {
320            self.parent.take().ok_or(SbeErr::ParentNotSet)
321        }
322
323        /// will return Some(current index) when successful otherwise None
324        #[inline]
325        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
326            let index = self.index.wrapping_add(1);
327            if index >= self.count as usize {
328                return Ok(None);
329            }
330
331            if let Some(parent) = self.parent.as_mut() {
332                self.offset = parent.get_limit();
333                parent.set_limit(self.offset + Self::block_length() as usize);
334                self.index = index;
335                Ok(Some(index))
336            } else {
337                Err(SbeErr::ParentNotSet)
338            }
339        }
340
341        /// primitive field 'price'
342        /// - min value: -9223372036854775807
343        /// - max value: 9223372036854775807
344        /// - null value: -9223372036854775808_i64
345        /// - characterEncoding: null
346        /// - semanticType: null
347        /// - encodedOffset: 0
348        /// - encodedLength: 8
349        /// - version: 0
350        #[inline]
351        pub fn price(&mut self, value: i64) {
352            let offset = self.offset;
353            self.get_buf_mut().put_i64_at(offset, value);
354        }
355
356        /// primitive field 'qty'
357        /// - min value: -9223372036854775807
358        /// - max value: 9223372036854775807
359        /// - null value: -9223372036854775808_i64
360        /// - characterEncoding: null
361        /// - semanticType: null
362        /// - encodedOffset: 8
363        /// - encodedLength: 8
364        /// - version: 0
365        #[inline]
366        pub fn qty(&mut self, value: i64) {
367            let offset = self.offset + 8;
368            self.get_buf_mut().put_i64_at(offset, value);
369        }
370    }
371} // end encoder
372
373pub mod decoder {
374    use message_header_codec::*;
375
376    use super::*;
377
378    #[derive(Clone, Copy, Debug, Default)]
379    pub struct DepthResponseDecoder<'a> {
380        buf: ReadBuf<'a>,
381        initial_offset: usize,
382        offset: usize,
383        limit: usize,
384        pub acting_block_length: u16,
385        pub acting_version: u16,
386    }
387
388    impl ActingVersion for DepthResponseDecoder<'_> {
389        #[inline]
390        fn acting_version(&self) -> u16 {
391            self.acting_version
392        }
393    }
394
395    impl<'a> Reader<'a> for DepthResponseDecoder<'a> {
396        #[inline]
397        fn get_buf(&self) -> &ReadBuf<'a> {
398            &self.buf
399        }
400    }
401
402    impl<'a> Decoder<'a> for DepthResponseDecoder<'a> {
403        #[inline]
404        fn get_limit(&self) -> usize {
405            self.limit
406        }
407
408        #[inline]
409        fn set_limit(&mut self, limit: usize) {
410            self.limit = limit;
411        }
412    }
413
414    impl<'a> DepthResponseDecoder<'a> {
415        pub fn wrap(
416            mut self,
417            buf: ReadBuf<'a>,
418            offset: usize,
419            acting_block_length: u16,
420            acting_version: u16,
421        ) -> Self {
422            let limit = offset + acting_block_length as usize;
423            self.buf = buf;
424            self.initial_offset = offset;
425            self.offset = offset;
426            self.limit = limit;
427            self.acting_block_length = acting_block_length;
428            self.acting_version = acting_version;
429            self
430        }
431
432        #[inline]
433        pub fn encoded_length(&self) -> usize {
434            self.limit - self.offset
435        }
436
437        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
438            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
439            let acting_block_length = header.block_length();
440            let acting_version = header.version();
441
442            self.wrap(
443                header.parent().unwrap(),
444                offset + message_header_codec::ENCODED_LENGTH,
445                acting_block_length,
446                acting_version,
447            )
448        }
449
450        /// primitive field - 'REQUIRED'
451        #[inline]
452        pub fn last_update_id(&self) -> i64 {
453            self.get_buf().get_i64_at(self.offset)
454        }
455
456        /// primitive field - 'REQUIRED'
457        #[inline]
458        pub fn price_exponent(&self) -> i8 {
459            self.get_buf().get_i8_at(self.offset + 8)
460        }
461
462        /// primitive field - 'REQUIRED'
463        #[inline]
464        pub fn qty_exponent(&self) -> i8 {
465            self.get_buf().get_i8_at(self.offset + 9)
466        }
467
468        /// GROUP DECODER (id=100)
469        #[inline]
470        pub fn bids_decoder(self) -> BidsDecoder<Self> {
471            BidsDecoder::default().wrap(self)
472        }
473
474        /// GROUP DECODER (id=101)
475        #[inline]
476        pub fn asks_decoder(self) -> AsksDecoder<Self> {
477            AsksDecoder::default().wrap(self)
478        }
479    }
480
481    #[derive(Debug, Default)]
482    pub struct BidsDecoder<P> {
483        parent: Option<P>,
484        block_length: u16,
485        count: u32,
486        index: usize,
487        offset: usize,
488    }
489
490    impl<'a, P> ActingVersion for BidsDecoder<P>
491    where
492        P: Reader<'a> + ActingVersion + Default,
493    {
494        #[inline]
495        fn acting_version(&self) -> u16 {
496            self.parent.as_ref().unwrap().acting_version()
497        }
498    }
499
500    impl<'a, P> Reader<'a> for BidsDecoder<P>
501    where
502        P: Reader<'a> + Default,
503    {
504        #[inline]
505        fn get_buf(&self) -> &ReadBuf<'a> {
506            self.parent.as_ref().expect("parent missing").get_buf()
507        }
508    }
509
510    impl<'a, P> Decoder<'a> for BidsDecoder<P>
511    where
512        P: Decoder<'a> + ActingVersion + Default,
513    {
514        #[inline]
515        fn get_limit(&self) -> usize {
516            self.parent.as_ref().expect("parent missing").get_limit()
517        }
518
519        #[inline]
520        fn set_limit(&mut self, limit: usize) {
521            self.parent
522                .as_mut()
523                .expect("parent missing")
524                .set_limit(limit);
525        }
526    }
527
528    impl<'a, P> BidsDecoder<P>
529    where
530        P: Decoder<'a> + ActingVersion + Default,
531    {
532        pub fn wrap(mut self, mut parent: P) -> Self {
533            let initial_offset = parent.get_limit();
534            let block_length = parent.get_buf().get_u16_at(initial_offset);
535            let count = parent.get_buf().get_u32_at(initial_offset + 2);
536            parent.set_limit(initial_offset + 6);
537            self.parent = Some(parent);
538            self.block_length = block_length;
539            self.count = count;
540            self.index = usize::MAX;
541            self.offset = 0;
542            self
543        }
544
545        /// group token - Token{signal=BEGIN_GROUP, name='bids', referencedName='null', description='null', packageName='null', id=100, version=0, deprecated=0, encodedLength=16, offset=10, componentTokenCount=12, 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'}}
546        #[inline]
547        pub fn parent(&mut self) -> SbeResult<P> {
548            self.parent.take().ok_or(SbeErr::ParentNotSet)
549        }
550
551        #[inline]
552        pub fn acting_version(&mut self) -> u16 {
553            self.parent.as_ref().unwrap().acting_version()
554        }
555
556        #[inline]
557        pub fn count(&self) -> u32 {
558            self.count
559        }
560
561        /// will return Some(current index) when successful otherwise None
562        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
563            let index = self.index.wrapping_add(1);
564            if index >= self.count as usize {
565                return Ok(None);
566            }
567
568            if let Some(parent) = self.parent.as_mut() {
569                self.offset = parent.get_limit();
570                parent.set_limit(self.offset + self.block_length as usize);
571                self.index = index;
572                Ok(Some(index))
573            } else {
574                Err(SbeErr::ParentNotSet)
575            }
576        }
577
578        /// primitive field - 'REQUIRED'
579        #[inline]
580        pub fn price(&self) -> i64 {
581            self.get_buf().get_i64_at(self.offset)
582        }
583
584        /// primitive field - 'REQUIRED'
585        #[inline]
586        pub fn qty(&self) -> i64 {
587            self.get_buf().get_i64_at(self.offset + 8)
588        }
589    }
590
591    #[derive(Debug, Default)]
592    pub struct AsksDecoder<P> {
593        parent: Option<P>,
594        block_length: u16,
595        count: u32,
596        index: usize,
597        offset: usize,
598    }
599
600    impl<'a, P> ActingVersion for AsksDecoder<P>
601    where
602        P: Reader<'a> + ActingVersion + Default,
603    {
604        #[inline]
605        fn acting_version(&self) -> u16 {
606            self.parent.as_ref().unwrap().acting_version()
607        }
608    }
609
610    impl<'a, P> Reader<'a> for AsksDecoder<P>
611    where
612        P: Reader<'a> + Default,
613    {
614        #[inline]
615        fn get_buf(&self) -> &ReadBuf<'a> {
616            self.parent.as_ref().expect("parent missing").get_buf()
617        }
618    }
619
620    impl<'a, P> Decoder<'a> for AsksDecoder<P>
621    where
622        P: Decoder<'a> + ActingVersion + Default,
623    {
624        #[inline]
625        fn get_limit(&self) -> usize {
626            self.parent.as_ref().expect("parent missing").get_limit()
627        }
628
629        #[inline]
630        fn set_limit(&mut self, limit: usize) {
631            self.parent
632                .as_mut()
633                .expect("parent missing")
634                .set_limit(limit);
635        }
636    }
637
638    impl<'a, P> AsksDecoder<P>
639    where
640        P: Decoder<'a> + ActingVersion + Default,
641    {
642        pub fn wrap(mut self, mut parent: P) -> Self {
643            let initial_offset = parent.get_limit();
644            let block_length = parent.get_buf().get_u16_at(initial_offset);
645            let count = parent.get_buf().get_u32_at(initial_offset + 2);
646            parent.set_limit(initial_offset + 6);
647            self.parent = Some(parent);
648            self.block_length = block_length;
649            self.count = count;
650            self.index = usize::MAX;
651            self.offset = 0;
652            self
653        }
654
655        /// group token - Token{signal=BEGIN_GROUP, name='asks', referencedName='null', description='null', packageName='null', id=101, version=0, deprecated=0, encodedLength=16, offset=-1, componentTokenCount=12, 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'}}
656        #[inline]
657        pub fn parent(&mut self) -> SbeResult<P> {
658            self.parent.take().ok_or(SbeErr::ParentNotSet)
659        }
660
661        #[inline]
662        pub fn acting_version(&mut self) -> u16 {
663            self.parent.as_ref().unwrap().acting_version()
664        }
665
666        #[inline]
667        pub fn count(&self) -> u32 {
668            self.count
669        }
670
671        /// will return Some(current index) when successful otherwise None
672        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
673            let index = self.index.wrapping_add(1);
674            if index >= self.count as usize {
675                return Ok(None);
676            }
677
678            if let Some(parent) = self.parent.as_mut() {
679                self.offset = parent.get_limit();
680                parent.set_limit(self.offset + self.block_length as usize);
681                self.index = index;
682                Ok(Some(index))
683            } else {
684                Err(SbeErr::ParentNotSet)
685            }
686        }
687
688        /// primitive field - 'REQUIRED'
689        #[inline]
690        pub fn price(&self) -> i64 {
691            self.get_buf().get_i64_at(self.offset)
692        }
693
694        /// primitive field - 'REQUIRED'
695        #[inline]
696        pub fn qty(&self) -> i64 {
697            self.get_buf().get_i64_at(self.offset + 8)
698        }
699    }
700} // end decoder