Skip to main content

nautilus_binance/spot/sbe/generated/
agg_trades_response_codec.rs

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