nautilus_binance/common/sbe/spot/
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            if let Some(parent) = self.parent.as_mut() {
211                self.offset = parent.get_limit();
212                parent.set_limit(self.offset + Self::block_length() as usize);
213                self.index = index;
214                Ok(Some(index))
215            } else {
216                Err(SbeErr::ParentNotSet)
217            }
218        }
219
220        /// primitive field 'price'
221        /// - min value: -9223372036854775807
222        /// - max value: 9223372036854775807
223        /// - null value: -9223372036854775808_i64
224        /// - characterEncoding: null
225        /// - semanticType: null
226        /// - encodedOffset: 0
227        /// - encodedLength: 8
228        /// - version: 0
229        #[inline]
230        pub fn price(&mut self, value: i64) {
231            let offset = self.offset;
232            self.get_buf_mut().put_i64_at(offset, value);
233        }
234
235        /// primitive field 'qty'
236        /// - min value: -9223372036854775807
237        /// - max value: 9223372036854775807
238        /// - null value: -9223372036854775808_i64
239        /// - characterEncoding: null
240        /// - semanticType: null
241        /// - encodedOffset: 8
242        /// - encodedLength: 8
243        /// - version: 0
244        #[inline]
245        pub fn qty(&mut self, value: i64) {
246            let offset = self.offset + 8;
247            self.get_buf_mut().put_i64_at(offset, value);
248        }
249    }
250
251    #[derive(Debug, Default)]
252    pub struct AsksEncoder<P> {
253        parent: Option<P>,
254        count: u32,
255        index: usize,
256        offset: usize,
257        initial_limit: usize,
258    }
259
260    impl<'a, P> Writer<'a> for AsksEncoder<P>
261    where
262        P: Writer<'a> + Default,
263    {
264        #[inline]
265        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
266            if let Some(parent) = self.parent.as_mut() {
267                parent.get_buf_mut()
268            } else {
269                panic!("parent was None")
270            }
271        }
272    }
273
274    impl<'a, P> Encoder<'a> for AsksEncoder<P>
275    where
276        P: Encoder<'a> + Default,
277    {
278        #[inline]
279        fn get_limit(&self) -> usize {
280            self.parent.as_ref().expect("parent missing").get_limit()
281        }
282
283        #[inline]
284        fn set_limit(&mut self, limit: usize) {
285            self.parent
286                .as_mut()
287                .expect("parent missing")
288                .set_limit(limit);
289        }
290    }
291
292    impl<'a, P> AsksEncoder<P>
293    where
294        P: Encoder<'a> + Default,
295    {
296        #[inline]
297        pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
298            let initial_limit = parent.get_limit();
299            parent.set_limit(initial_limit + 6);
300            parent
301                .get_buf_mut()
302                .put_u16_at(initial_limit, Self::block_length());
303            parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
304            self.parent = Some(parent);
305            self.count = count;
306            self.index = usize::MAX;
307            self.offset = usize::MAX;
308            self.initial_limit = initial_limit;
309            self
310        }
311
312        #[inline]
313        pub fn block_length() -> u16 {
314            16
315        }
316
317        #[inline]
318        pub fn parent(&mut self) -> SbeResult<P> {
319            self.parent.take().ok_or(SbeErr::ParentNotSet)
320        }
321
322        /// will return Some(current index) when successful otherwise None
323        #[inline]
324        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
325            let index = self.index.wrapping_add(1);
326            if index >= self.count as usize {
327                return Ok(None);
328            }
329            if let Some(parent) = self.parent.as_mut() {
330                self.offset = parent.get_limit();
331                parent.set_limit(self.offset + Self::block_length() as usize);
332                self.index = index;
333                Ok(Some(index))
334            } else {
335                Err(SbeErr::ParentNotSet)
336            }
337        }
338
339        /// primitive field 'price'
340        /// - min value: -9223372036854775807
341        /// - max value: 9223372036854775807
342        /// - null value: -9223372036854775808_i64
343        /// - characterEncoding: null
344        /// - semanticType: null
345        /// - encodedOffset: 0
346        /// - encodedLength: 8
347        /// - version: 0
348        #[inline]
349        pub fn price(&mut self, value: i64) {
350            let offset = self.offset;
351            self.get_buf_mut().put_i64_at(offset, value);
352        }
353
354        /// primitive field 'qty'
355        /// - min value: -9223372036854775807
356        /// - max value: 9223372036854775807
357        /// - null value: -9223372036854775808_i64
358        /// - characterEncoding: null
359        /// - semanticType: null
360        /// - encodedOffset: 8
361        /// - encodedLength: 8
362        /// - version: 0
363        #[inline]
364        pub fn qty(&mut self, value: i64) {
365            let offset = self.offset + 8;
366            self.get_buf_mut().put_i64_at(offset, value);
367        }
368    }
369} // end encoder
370
371pub mod decoder {
372    use message_header_codec::*;
373
374    use super::*;
375
376    #[derive(Clone, Copy, Debug, Default)]
377    pub struct DepthResponseDecoder<'a> {
378        buf: ReadBuf<'a>,
379        initial_offset: usize,
380        offset: usize,
381        limit: usize,
382        pub acting_block_length: u16,
383        pub acting_version: u16,
384    }
385
386    impl ActingVersion for DepthResponseDecoder<'_> {
387        #[inline]
388        fn acting_version(&self) -> u16 {
389            self.acting_version
390        }
391    }
392
393    impl<'a> Reader<'a> for DepthResponseDecoder<'a> {
394        #[inline]
395        fn get_buf(&self) -> &ReadBuf<'a> {
396            &self.buf
397        }
398    }
399
400    impl<'a> Decoder<'a> for DepthResponseDecoder<'a> {
401        #[inline]
402        fn get_limit(&self) -> usize {
403            self.limit
404        }
405
406        #[inline]
407        fn set_limit(&mut self, limit: usize) {
408            self.limit = limit;
409        }
410    }
411
412    impl<'a> DepthResponseDecoder<'a> {
413        pub fn wrap(
414            mut self,
415            buf: ReadBuf<'a>,
416            offset: usize,
417            acting_block_length: u16,
418            acting_version: u16,
419        ) -> Self {
420            let limit = offset + acting_block_length as usize;
421            self.buf = buf;
422            self.initial_offset = offset;
423            self.offset = offset;
424            self.limit = limit;
425            self.acting_block_length = acting_block_length;
426            self.acting_version = acting_version;
427            self
428        }
429
430        #[inline]
431        pub fn encoded_length(&self) -> usize {
432            self.limit - self.offset
433        }
434
435        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
436            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
437            let acting_block_length = header.block_length();
438            let acting_version = header.version();
439
440            self.wrap(
441                header.parent().unwrap(),
442                offset + message_header_codec::ENCODED_LENGTH,
443                acting_block_length,
444                acting_version,
445            )
446        }
447
448        /// primitive field - 'REQUIRED'
449        #[inline]
450        pub fn last_update_id(&self) -> i64 {
451            self.get_buf().get_i64_at(self.offset)
452        }
453
454        /// primitive field - 'REQUIRED'
455        #[inline]
456        pub fn price_exponent(&self) -> i8 {
457            self.get_buf().get_i8_at(self.offset + 8)
458        }
459
460        /// primitive field - 'REQUIRED'
461        #[inline]
462        pub fn qty_exponent(&self) -> i8 {
463            self.get_buf().get_i8_at(self.offset + 9)
464        }
465
466        /// GROUP DECODER (id=100)
467        #[inline]
468        pub fn bids_decoder(self) -> BidsDecoder<Self> {
469            BidsDecoder::default().wrap(self)
470        }
471
472        /// GROUP DECODER (id=101)
473        #[inline]
474        pub fn asks_decoder(self) -> AsksDecoder<Self> {
475            AsksDecoder::default().wrap(self)
476        }
477    }
478
479    #[derive(Debug, Default)]
480    pub struct BidsDecoder<P> {
481        parent: Option<P>,
482        block_length: u16,
483        count: u32,
484        index: usize,
485        offset: usize,
486    }
487
488    impl<'a, P> ActingVersion for BidsDecoder<P>
489    where
490        P: Reader<'a> + ActingVersion + Default,
491    {
492        #[inline]
493        fn acting_version(&self) -> u16 {
494            self.parent.as_ref().unwrap().acting_version()
495        }
496    }
497
498    impl<'a, P> Reader<'a> for BidsDecoder<P>
499    where
500        P: Reader<'a> + Default,
501    {
502        #[inline]
503        fn get_buf(&self) -> &ReadBuf<'a> {
504            self.parent.as_ref().expect("parent missing").get_buf()
505        }
506    }
507
508    impl<'a, P> Decoder<'a> for BidsDecoder<P>
509    where
510        P: Decoder<'a> + ActingVersion + Default,
511    {
512        #[inline]
513        fn get_limit(&self) -> usize {
514            self.parent.as_ref().expect("parent missing").get_limit()
515        }
516
517        #[inline]
518        fn set_limit(&mut self, limit: usize) {
519            self.parent
520                .as_mut()
521                .expect("parent missing")
522                .set_limit(limit);
523        }
524    }
525
526    impl<'a, P> BidsDecoder<P>
527    where
528        P: Decoder<'a> + ActingVersion + Default,
529    {
530        pub fn wrap(mut self, mut parent: P) -> Self {
531            let initial_offset = parent.get_limit();
532            let block_length = parent.get_buf().get_u16_at(initial_offset);
533            let count = parent.get_buf().get_u32_at(initial_offset + 2);
534            parent.set_limit(initial_offset + 6);
535            self.parent = Some(parent);
536            self.block_length = block_length;
537            self.count = count;
538            self.index = usize::MAX;
539            self.offset = 0;
540            self
541        }
542
543        /// 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'}}
544        #[inline]
545        pub fn parent(&mut self) -> SbeResult<P> {
546            self.parent.take().ok_or(SbeErr::ParentNotSet)
547        }
548
549        #[inline]
550        pub fn acting_version(&mut self) -> u16 {
551            self.parent.as_ref().unwrap().acting_version()
552        }
553
554        #[inline]
555        pub fn count(&self) -> u32 {
556            self.count
557        }
558
559        /// will return Some(current index) when successful otherwise None
560        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
561            let index = self.index.wrapping_add(1);
562            if index >= self.count as usize {
563                return Ok(None);
564            }
565            if let Some(parent) = self.parent.as_mut() {
566                self.offset = parent.get_limit();
567                parent.set_limit(self.offset + self.block_length as usize);
568                self.index = index;
569                Ok(Some(index))
570            } else {
571                Err(SbeErr::ParentNotSet)
572            }
573        }
574
575        /// primitive field - 'REQUIRED'
576        #[inline]
577        pub fn price(&self) -> i64 {
578            self.get_buf().get_i64_at(self.offset)
579        }
580
581        /// primitive field - 'REQUIRED'
582        #[inline]
583        pub fn qty(&self) -> i64 {
584            self.get_buf().get_i64_at(self.offset + 8)
585        }
586    }
587
588    #[derive(Debug, Default)]
589    pub struct AsksDecoder<P> {
590        parent: Option<P>,
591        block_length: u16,
592        count: u32,
593        index: usize,
594        offset: usize,
595    }
596
597    impl<'a, P> ActingVersion for AsksDecoder<P>
598    where
599        P: Reader<'a> + ActingVersion + Default,
600    {
601        #[inline]
602        fn acting_version(&self) -> u16 {
603            self.parent.as_ref().unwrap().acting_version()
604        }
605    }
606
607    impl<'a, P> Reader<'a> for AsksDecoder<P>
608    where
609        P: Reader<'a> + Default,
610    {
611        #[inline]
612        fn get_buf(&self) -> &ReadBuf<'a> {
613            self.parent.as_ref().expect("parent missing").get_buf()
614        }
615    }
616
617    impl<'a, P> Decoder<'a> for AsksDecoder<P>
618    where
619        P: Decoder<'a> + ActingVersion + Default,
620    {
621        #[inline]
622        fn get_limit(&self) -> usize {
623            self.parent.as_ref().expect("parent missing").get_limit()
624        }
625
626        #[inline]
627        fn set_limit(&mut self, limit: usize) {
628            self.parent
629                .as_mut()
630                .expect("parent missing")
631                .set_limit(limit);
632        }
633    }
634
635    impl<'a, P> AsksDecoder<P>
636    where
637        P: Decoder<'a> + ActingVersion + Default,
638    {
639        pub fn wrap(mut self, mut parent: P) -> Self {
640            let initial_offset = parent.get_limit();
641            let block_length = parent.get_buf().get_u16_at(initial_offset);
642            let count = parent.get_buf().get_u32_at(initial_offset + 2);
643            parent.set_limit(initial_offset + 6);
644            self.parent = Some(parent);
645            self.block_length = block_length;
646            self.count = count;
647            self.index = usize::MAX;
648            self.offset = 0;
649            self
650        }
651
652        /// 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'}}
653        #[inline]
654        pub fn parent(&mut self) -> SbeResult<P> {
655            self.parent.take().ok_or(SbeErr::ParentNotSet)
656        }
657
658        #[inline]
659        pub fn acting_version(&mut self) -> u16 {
660            self.parent.as_ref().unwrap().acting_version()
661        }
662
663        #[inline]
664        pub fn count(&self) -> u32 {
665            self.count
666        }
667
668        /// will return Some(current index) when successful otherwise None
669        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
670            let index = self.index.wrapping_add(1);
671            if index >= self.count as usize {
672                return Ok(None);
673            }
674            if let Some(parent) = self.parent.as_mut() {
675                self.offset = parent.get_limit();
676                parent.set_limit(self.offset + self.block_length as usize);
677                self.index = index;
678                Ok(Some(index))
679            } else {
680                Err(SbeErr::ParentNotSet)
681            }
682        }
683
684        /// primitive field - 'REQUIRED'
685        #[inline]
686        pub fn price(&self) -> i64 {
687            self.get_buf().get_i64_at(self.offset)
688        }
689
690        /// primitive field - 'REQUIRED'
691        #[inline]
692        pub fn qty(&self) -> i64 {
693            self.get_buf().get_i64_at(self.offset + 8)
694        }
695    }
696} // end decoder