Skip to main content

nautilus_binance/spot/sbe/generated/
trades_response_codec.rs

1pub use decoder::TradesResponseDecoder;
2pub use encoder::TradesResponseEncoder;
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 = 201;
9
10pub mod encoder {
11    use message_header_codec::*;
12
13    use super::*;
14
15    #[derive(Debug, Default)]
16    pub struct TradesResponseEncoder<'a> {
17        buf: WriteBuf<'a>,
18        initial_offset: usize,
19        offset: usize,
20        limit: usize,
21    }
22
23    impl<'a> Writer<'a> for TradesResponseEncoder<'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 TradesResponseEncoder<'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> TradesResponseEncoder<'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 trades_encoder(
99            self,
100            count: u32,
101            trades_encoder: TradesEncoder<Self>,
102        ) -> TradesEncoder<Self> {
103            trades_encoder.wrap(self, count)
104        }
105    }
106
107    #[derive(Debug, Default)]
108    pub struct TradesEncoder<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 TradesEncoder<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 TradesEncoder<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> TradesEncoder<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            42
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 'id'
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 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 'quoteQty'
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 quote_qty(&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 'time'
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 time(&mut self, value: i64) {
267            let offset = self.offset + 32;
268            self.get_buf_mut().put_i64_at(offset, value);
269        }
270
271        /// REQUIRED enum
272        #[inline]
273        pub fn is_buyer_maker(&mut self, value: bool_enum::BoolEnum) {
274            let offset = self.offset + 40;
275            self.get_buf_mut().put_u8_at(offset, value as u8)
276        }
277
278        /// REQUIRED enum
279        #[inline]
280        pub fn is_best_match(&mut self, value: bool_enum::BoolEnum) {
281            let offset = self.offset + 41;
282            self.get_buf_mut().put_u8_at(offset, value as u8)
283        }
284    }
285} // end encoder
286
287pub mod decoder {
288    use message_header_codec::*;
289
290    use super::*;
291
292    #[derive(Clone, Copy, Debug, Default)]
293    pub struct TradesResponseDecoder<'a> {
294        buf: ReadBuf<'a>,
295        initial_offset: usize,
296        offset: usize,
297        limit: usize,
298        pub acting_block_length: u16,
299        pub acting_version: u16,
300    }
301
302    impl ActingVersion for TradesResponseDecoder<'_> {
303        #[inline]
304        fn acting_version(&self) -> u16 {
305            self.acting_version
306        }
307    }
308
309    impl<'a> Reader<'a> for TradesResponseDecoder<'a> {
310        #[inline]
311        fn get_buf(&self) -> &ReadBuf<'a> {
312            &self.buf
313        }
314    }
315
316    impl<'a> Decoder<'a> for TradesResponseDecoder<'a> {
317        #[inline]
318        fn get_limit(&self) -> usize {
319            self.limit
320        }
321
322        #[inline]
323        fn set_limit(&mut self, limit: usize) {
324            self.limit = limit;
325        }
326    }
327
328    impl<'a> TradesResponseDecoder<'a> {
329        pub fn wrap(
330            mut self,
331            buf: ReadBuf<'a>,
332            offset: usize,
333            acting_block_length: u16,
334            acting_version: u16,
335        ) -> Self {
336            let limit = offset + acting_block_length as usize;
337            self.buf = buf;
338            self.initial_offset = offset;
339            self.offset = offset;
340            self.limit = limit;
341            self.acting_block_length = acting_block_length;
342            self.acting_version = acting_version;
343            self
344        }
345
346        #[inline]
347        pub fn encoded_length(&self) -> usize {
348            self.limit - self.offset
349        }
350
351        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
352            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
353            let acting_block_length = header.block_length();
354            let acting_version = header.version();
355
356            self.wrap(
357                header.parent().unwrap(),
358                offset + message_header_codec::ENCODED_LENGTH,
359                acting_block_length,
360                acting_version,
361            )
362        }
363
364        /// primitive field - 'REQUIRED'
365        #[inline]
366        pub fn price_exponent(&self) -> i8 {
367            self.get_buf().get_i8_at(self.offset)
368        }
369
370        /// primitive field - 'REQUIRED'
371        #[inline]
372        pub fn qty_exponent(&self) -> i8 {
373            self.get_buf().get_i8_at(self.offset + 1)
374        }
375
376        /// GROUP DECODER (id=100)
377        #[inline]
378        pub fn trades_decoder(self) -> TradesDecoder<Self> {
379            TradesDecoder::default().wrap(self)
380        }
381    }
382
383    #[derive(Debug, Default)]
384    pub struct TradesDecoder<P> {
385        parent: Option<P>,
386        block_length: u16,
387        count: u32,
388        index: usize,
389        offset: usize,
390    }
391
392    impl<'a, P> ActingVersion for TradesDecoder<P>
393    where
394        P: Reader<'a> + ActingVersion + Default,
395    {
396        #[inline]
397        fn acting_version(&self) -> u16 {
398            self.parent.as_ref().unwrap().acting_version()
399        }
400    }
401
402    impl<'a, P> Reader<'a> for TradesDecoder<P>
403    where
404        P: Reader<'a> + Default,
405    {
406        #[inline]
407        fn get_buf(&self) -> &ReadBuf<'a> {
408            self.parent.as_ref().expect("parent missing").get_buf()
409        }
410    }
411
412    impl<'a, P> Decoder<'a> for TradesDecoder<P>
413    where
414        P: Decoder<'a> + ActingVersion + Default,
415    {
416        #[inline]
417        fn get_limit(&self) -> usize {
418            self.parent.as_ref().expect("parent missing").get_limit()
419        }
420
421        #[inline]
422        fn set_limit(&mut self, limit: usize) {
423            self.parent
424                .as_mut()
425                .expect("parent missing")
426                .set_limit(limit);
427        }
428    }
429
430    impl<'a, P> TradesDecoder<P>
431    where
432        P: Decoder<'a> + ActingVersion + Default,
433    {
434        pub fn wrap(mut self, mut parent: P) -> Self {
435            let initial_offset = parent.get_limit();
436            let block_length = parent.get_buf().get_u16_at(initial_offset);
437            let count = parent.get_buf().get_u32_at(initial_offset + 2);
438            parent.set_limit(initial_offset + 6);
439            self.parent = Some(parent);
440            self.block_length = block_length;
441            self.count = count;
442            self.index = usize::MAX;
443            self.offset = 0;
444            self
445        }
446
447        /// group token - Token{signal=BEGIN_GROUP, name='trades', referencedName='null', description='null', packageName='null', id=100, version=0, deprecated=0, encodedLength=42, offset=2, componentTokenCount=33, 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'}}
448        #[inline]
449        pub fn parent(&mut self) -> SbeResult<P> {
450            self.parent.take().ok_or(SbeErr::ParentNotSet)
451        }
452
453        #[inline]
454        pub fn acting_version(&mut self) -> u16 {
455            self.parent.as_ref().unwrap().acting_version()
456        }
457
458        #[inline]
459        pub fn count(&self) -> u32 {
460            self.count
461        }
462
463        /// will return Some(current index) when successful otherwise None
464        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
465            let index = self.index.wrapping_add(1);
466            if index >= self.count as usize {
467                return Ok(None);
468            }
469
470            if let Some(parent) = self.parent.as_mut() {
471                self.offset = parent.get_limit();
472                parent.set_limit(self.offset + self.block_length as usize);
473                self.index = index;
474                Ok(Some(index))
475            } else {
476                Err(SbeErr::ParentNotSet)
477            }
478        }
479
480        /// primitive field - 'REQUIRED'
481        #[inline]
482        pub fn id(&self) -> i64 {
483            self.get_buf().get_i64_at(self.offset)
484        }
485
486        /// primitive field - 'REQUIRED'
487        #[inline]
488        pub fn price(&self) -> i64 {
489            self.get_buf().get_i64_at(self.offset + 8)
490        }
491
492        /// primitive field - 'REQUIRED'
493        #[inline]
494        pub fn qty(&self) -> i64 {
495            self.get_buf().get_i64_at(self.offset + 16)
496        }
497
498        /// primitive field - 'REQUIRED'
499        #[inline]
500        pub fn quote_qty(&self) -> i64 {
501            self.get_buf().get_i64_at(self.offset + 24)
502        }
503
504        /// primitive field - 'REQUIRED'
505        #[inline]
506        pub fn time(&self) -> i64 {
507            self.get_buf().get_i64_at(self.offset + 32)
508        }
509
510        /// REQUIRED enum
511        #[inline]
512        pub fn is_buyer_maker(&self) -> bool_enum::BoolEnum {
513            self.get_buf().get_u8_at(self.offset + 40).into()
514        }
515
516        /// REQUIRED enum
517        #[inline]
518        pub fn is_best_match(&self) -> bool_enum::BoolEnum {
519            self.get_buf().get_u8_at(self.offset + 41).into()
520        }
521    }
522} // end decoder