nautilus_binance/common/sbe/spot/
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            if let Some(parent) = self.parent.as_mut() {
186                self.offset = parent.get_limit();
187                parent.set_limit(self.offset + Self::block_length() as usize);
188                self.index = index;
189                Ok(Some(index))
190            } else {
191                Err(SbeErr::ParentNotSet)
192            }
193        }
194
195        /// primitive field 'id'
196        /// - min value: -9223372036854775807
197        /// - max value: 9223372036854775807
198        /// - null value: -9223372036854775808_i64
199        /// - characterEncoding: null
200        /// - semanticType: null
201        /// - encodedOffset: 0
202        /// - encodedLength: 8
203        /// - version: 0
204        #[inline]
205        pub fn id(&mut self, value: i64) {
206            let offset = self.offset;
207            self.get_buf_mut().put_i64_at(offset, value);
208        }
209
210        /// primitive field 'price'
211        /// - min value: -9223372036854775807
212        /// - max value: 9223372036854775807
213        /// - null value: -9223372036854775808_i64
214        /// - characterEncoding: null
215        /// - semanticType: null
216        /// - encodedOffset: 8
217        /// - encodedLength: 8
218        /// - version: 0
219        #[inline]
220        pub fn price(&mut self, value: i64) {
221            let offset = self.offset + 8;
222            self.get_buf_mut().put_i64_at(offset, value);
223        }
224
225        /// primitive field 'qty'
226        /// - min value: -9223372036854775807
227        /// - max value: 9223372036854775807
228        /// - null value: -9223372036854775808_i64
229        /// - characterEncoding: null
230        /// - semanticType: null
231        /// - encodedOffset: 16
232        /// - encodedLength: 8
233        /// - version: 0
234        #[inline]
235        pub fn qty(&mut self, value: i64) {
236            let offset = self.offset + 16;
237            self.get_buf_mut().put_i64_at(offset, value);
238        }
239
240        /// primitive field 'quoteQty'
241        /// - min value: -9223372036854775807
242        /// - max value: 9223372036854775807
243        /// - null value: -9223372036854775808_i64
244        /// - characterEncoding: null
245        /// - semanticType: null
246        /// - encodedOffset: 24
247        /// - encodedLength: 8
248        /// - version: 0
249        #[inline]
250        pub fn quote_qty(&mut self, value: i64) {
251            let offset = self.offset + 24;
252            self.get_buf_mut().put_i64_at(offset, value);
253        }
254
255        /// primitive field 'time'
256        /// - min value: -9223372036854775807
257        /// - max value: 9223372036854775807
258        /// - null value: -9223372036854775808_i64
259        /// - characterEncoding: null
260        /// - semanticType: null
261        /// - encodedOffset: 32
262        /// - encodedLength: 8
263        /// - version: 0
264        #[inline]
265        pub fn time(&mut self, value: i64) {
266            let offset = self.offset + 32;
267            self.get_buf_mut().put_i64_at(offset, value);
268        }
269
270        /// REQUIRED enum
271        #[inline]
272        pub fn is_buyer_maker(&mut self, value: bool_enum::BoolEnum) {
273            let offset = self.offset + 40;
274            self.get_buf_mut().put_u8_at(offset, value as u8)
275        }
276
277        /// REQUIRED enum
278        #[inline]
279        pub fn is_best_match(&mut self, value: bool_enum::BoolEnum) {
280            let offset = self.offset + 41;
281            self.get_buf_mut().put_u8_at(offset, value as u8)
282        }
283    }
284} // end encoder
285
286pub mod decoder {
287    use message_header_codec::*;
288
289    use super::*;
290
291    #[derive(Clone, Copy, Debug, Default)]
292    pub struct TradesResponseDecoder<'a> {
293        buf: ReadBuf<'a>,
294        initial_offset: usize,
295        offset: usize,
296        limit: usize,
297        pub acting_block_length: u16,
298        pub acting_version: u16,
299    }
300
301    impl ActingVersion for TradesResponseDecoder<'_> {
302        #[inline]
303        fn acting_version(&self) -> u16 {
304            self.acting_version
305        }
306    }
307
308    impl<'a> Reader<'a> for TradesResponseDecoder<'a> {
309        #[inline]
310        fn get_buf(&self) -> &ReadBuf<'a> {
311            &self.buf
312        }
313    }
314
315    impl<'a> Decoder<'a> for TradesResponseDecoder<'a> {
316        #[inline]
317        fn get_limit(&self) -> usize {
318            self.limit
319        }
320
321        #[inline]
322        fn set_limit(&mut self, limit: usize) {
323            self.limit = limit;
324        }
325    }
326
327    impl<'a> TradesResponseDecoder<'a> {
328        pub fn wrap(
329            mut self,
330            buf: ReadBuf<'a>,
331            offset: usize,
332            acting_block_length: u16,
333            acting_version: u16,
334        ) -> Self {
335            let limit = offset + acting_block_length as usize;
336            self.buf = buf;
337            self.initial_offset = offset;
338            self.offset = offset;
339            self.limit = limit;
340            self.acting_block_length = acting_block_length;
341            self.acting_version = acting_version;
342            self
343        }
344
345        #[inline]
346        pub fn encoded_length(&self) -> usize {
347            self.limit - self.offset
348        }
349
350        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
351            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
352            let acting_block_length = header.block_length();
353            let acting_version = header.version();
354
355            self.wrap(
356                header.parent().unwrap(),
357                offset + message_header_codec::ENCODED_LENGTH,
358                acting_block_length,
359                acting_version,
360            )
361        }
362
363        /// primitive field - 'REQUIRED'
364        #[inline]
365        pub fn price_exponent(&self) -> i8 {
366            self.get_buf().get_i8_at(self.offset)
367        }
368
369        /// primitive field - 'REQUIRED'
370        #[inline]
371        pub fn qty_exponent(&self) -> i8 {
372            self.get_buf().get_i8_at(self.offset + 1)
373        }
374
375        /// GROUP DECODER (id=100)
376        #[inline]
377        pub fn trades_decoder(self) -> TradesDecoder<Self> {
378            TradesDecoder::default().wrap(self)
379        }
380    }
381
382    #[derive(Debug, Default)]
383    pub struct TradesDecoder<P> {
384        parent: Option<P>,
385        block_length: u16,
386        count: u32,
387        index: usize,
388        offset: usize,
389    }
390
391    impl<'a, P> ActingVersion for TradesDecoder<P>
392    where
393        P: Reader<'a> + ActingVersion + Default,
394    {
395        #[inline]
396        fn acting_version(&self) -> u16 {
397            self.parent.as_ref().unwrap().acting_version()
398        }
399    }
400
401    impl<'a, P> Reader<'a> for TradesDecoder<P>
402    where
403        P: Reader<'a> + Default,
404    {
405        #[inline]
406        fn get_buf(&self) -> &ReadBuf<'a> {
407            self.parent.as_ref().expect("parent missing").get_buf()
408        }
409    }
410
411    impl<'a, P> Decoder<'a> for TradesDecoder<P>
412    where
413        P: Decoder<'a> + ActingVersion + Default,
414    {
415        #[inline]
416        fn get_limit(&self) -> usize {
417            self.parent.as_ref().expect("parent missing").get_limit()
418        }
419
420        #[inline]
421        fn set_limit(&mut self, limit: usize) {
422            self.parent
423                .as_mut()
424                .expect("parent missing")
425                .set_limit(limit);
426        }
427    }
428
429    impl<'a, P> TradesDecoder<P>
430    where
431        P: Decoder<'a> + ActingVersion + Default,
432    {
433        pub fn wrap(mut self, mut parent: P) -> Self {
434            let initial_offset = parent.get_limit();
435            let block_length = parent.get_buf().get_u16_at(initial_offset);
436            let count = parent.get_buf().get_u32_at(initial_offset + 2);
437            parent.set_limit(initial_offset + 6);
438            self.parent = Some(parent);
439            self.block_length = block_length;
440            self.count = count;
441            self.index = usize::MAX;
442            self.offset = 0;
443            self
444        }
445
446        /// 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'}}
447        #[inline]
448        pub fn parent(&mut self) -> SbeResult<P> {
449            self.parent.take().ok_or(SbeErr::ParentNotSet)
450        }
451
452        #[inline]
453        pub fn acting_version(&mut self) -> u16 {
454            self.parent.as_ref().unwrap().acting_version()
455        }
456
457        #[inline]
458        pub fn count(&self) -> u32 {
459            self.count
460        }
461
462        /// will return Some(current index) when successful otherwise None
463        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
464            let index = self.index.wrapping_add(1);
465            if index >= self.count as usize {
466                return Ok(None);
467            }
468            if let Some(parent) = self.parent.as_mut() {
469                self.offset = parent.get_limit();
470                parent.set_limit(self.offset + self.block_length as usize);
471                self.index = index;
472                Ok(Some(index))
473            } else {
474                Err(SbeErr::ParentNotSet)
475            }
476        }
477
478        /// primitive field - 'REQUIRED'
479        #[inline]
480        pub fn id(&self) -> i64 {
481            self.get_buf().get_i64_at(self.offset)
482        }
483
484        /// primitive field - 'REQUIRED'
485        #[inline]
486        pub fn price(&self) -> i64 {
487            self.get_buf().get_i64_at(self.offset + 8)
488        }
489
490        /// primitive field - 'REQUIRED'
491        #[inline]
492        pub fn qty(&self) -> i64 {
493            self.get_buf().get_i64_at(self.offset + 16)
494        }
495
496        /// primitive field - 'REQUIRED'
497        #[inline]
498        pub fn quote_qty(&self) -> i64 {
499            self.get_buf().get_i64_at(self.offset + 24)
500        }
501
502        /// primitive field - 'REQUIRED'
503        #[inline]
504        pub fn time(&self) -> i64 {
505            self.get_buf().get_i64_at(self.offset + 32)
506        }
507
508        /// REQUIRED enum
509        #[inline]
510        pub fn is_buyer_maker(&self) -> bool_enum::BoolEnum {
511            self.get_buf().get_u8_at(self.offset + 40).into()
512        }
513
514        /// REQUIRED enum
515        #[inline]
516        pub fn is_best_match(&self) -> bool_enum::BoolEnum {
517            self.get_buf().get_u8_at(self.offset + 41).into()
518        }
519    }
520} // end decoder