Skip to main content

nautilus_binance/spot/sbe/generated/
price_ticker_response_codec.rs

1pub use decoder::PriceTickerResponseDecoder;
2pub use encoder::PriceTickerResponseEncoder;
3
4use super::*;
5pub use super::{SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_SEMANTIC_VERSION};
6
7pub const SBE_BLOCK_LENGTH: u16 = 0;
8pub const SBE_TEMPLATE_ID: u16 = 210;
9
10pub mod encoder {
11    use message_header_codec::*;
12
13    use super::*;
14
15    #[derive(Debug, Default)]
16    pub struct PriceTickerResponseEncoder<'a> {
17        buf: WriteBuf<'a>,
18        initial_offset: usize,
19        offset: usize,
20        limit: usize,
21    }
22
23    impl<'a> Writer<'a> for PriceTickerResponseEncoder<'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 PriceTickerResponseEncoder<'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> PriceTickerResponseEncoder<'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        /// GROUP ENCODER (id=100)
67        #[inline]
68        pub fn tickers_encoder(
69            self,
70            count: u32,
71            tickers_encoder: TickersEncoder<Self>,
72        ) -> TickersEncoder<Self> {
73            tickers_encoder.wrap(self, count)
74        }
75    }
76
77    #[derive(Debug, Default)]
78    pub struct TickersEncoder<P> {
79        parent: Option<P>,
80        count: u32,
81        index: usize,
82        offset: usize,
83        initial_limit: usize,
84    }
85
86    impl<'a, P> Writer<'a> for TickersEncoder<P>
87    where
88        P: Writer<'a> + Default,
89    {
90        #[inline]
91        fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
92            if let Some(parent) = self.parent.as_mut() {
93                parent.get_buf_mut()
94            } else {
95                panic!("parent was None")
96            }
97        }
98    }
99
100    impl<'a, P> Encoder<'a> for TickersEncoder<P>
101    where
102        P: Encoder<'a> + Default,
103    {
104        #[inline]
105        fn get_limit(&self) -> usize {
106            self.parent.as_ref().expect("parent missing").get_limit()
107        }
108
109        #[inline]
110        fn set_limit(&mut self, limit: usize) {
111            self.parent
112                .as_mut()
113                .expect("parent missing")
114                .set_limit(limit);
115        }
116    }
117
118    impl<'a, P> TickersEncoder<P>
119    where
120        P: Encoder<'a> + Default,
121    {
122        #[inline]
123        pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
124            let initial_limit = parent.get_limit();
125            parent.set_limit(initial_limit + 6);
126            parent
127                .get_buf_mut()
128                .put_u16_at(initial_limit, Self::block_length());
129            parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
130            self.parent = Some(parent);
131            self.count = count;
132            self.index = usize::MAX;
133            self.offset = usize::MAX;
134            self.initial_limit = initial_limit;
135            self
136        }
137
138        #[inline]
139        pub fn block_length() -> u16 {
140            9
141        }
142
143        #[inline]
144        pub fn parent(&mut self) -> SbeResult<P> {
145            self.parent.take().ok_or(SbeErr::ParentNotSet)
146        }
147
148        /// will return Some(current index) when successful otherwise None
149        #[inline]
150        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
151            let index = self.index.wrapping_add(1);
152            if index >= self.count as usize {
153                return Ok(None);
154            }
155
156            if let Some(parent) = self.parent.as_mut() {
157                self.offset = parent.get_limit();
158                parent.set_limit(self.offset + Self::block_length() as usize);
159                self.index = index;
160                Ok(Some(index))
161            } else {
162                Err(SbeErr::ParentNotSet)
163            }
164        }
165
166        /// primitive field 'priceExponent'
167        /// - min value: -127
168        /// - max value: 127
169        /// - null value: -128_i8
170        /// - characterEncoding: null
171        /// - semanticType: null
172        /// - encodedOffset: 0
173        /// - encodedLength: 1
174        /// - version: 0
175        #[inline]
176        pub fn price_exponent(&mut self, value: i8) {
177            let offset = self.offset;
178            self.get_buf_mut().put_i8_at(offset, value);
179        }
180
181        /// primitive field 'price'
182        /// - min value: -9223372036854775807
183        /// - max value: 9223372036854775807
184        /// - null value: -9223372036854775808_i64
185        /// - characterEncoding: null
186        /// - semanticType: null
187        /// - encodedOffset: 1
188        /// - encodedLength: 8
189        /// - version: 0
190        #[inline]
191        pub fn price(&mut self, value: i64) {
192            let offset = self.offset + 1;
193            self.get_buf_mut().put_i64_at(offset, value);
194        }
195
196        /// VAR_DATA ENCODER - character encoding: 'UTF-8'
197        #[inline]
198        pub fn symbol(&mut self, value: &str) {
199            let limit = self.get_limit();
200            let data_length = value.len();
201            self.set_limit(limit + 1 + data_length);
202            self.get_buf_mut().put_u8_at(limit, data_length as u8);
203            self.get_buf_mut().put_slice_at(limit + 1, value.as_bytes());
204        }
205    }
206} // end encoder
207
208pub mod decoder {
209    use message_header_codec::*;
210
211    use super::*;
212
213    #[derive(Clone, Copy, Debug, Default)]
214    pub struct PriceTickerResponseDecoder<'a> {
215        buf: ReadBuf<'a>,
216        initial_offset: usize,
217        offset: usize,
218        limit: usize,
219        pub acting_block_length: u16,
220        pub acting_version: u16,
221    }
222
223    impl ActingVersion for PriceTickerResponseDecoder<'_> {
224        #[inline]
225        fn acting_version(&self) -> u16 {
226            self.acting_version
227        }
228    }
229
230    impl<'a> Reader<'a> for PriceTickerResponseDecoder<'a> {
231        #[inline]
232        fn get_buf(&self) -> &ReadBuf<'a> {
233            &self.buf
234        }
235    }
236
237    impl<'a> Decoder<'a> for PriceTickerResponseDecoder<'a> {
238        #[inline]
239        fn get_limit(&self) -> usize {
240            self.limit
241        }
242
243        #[inline]
244        fn set_limit(&mut self, limit: usize) {
245            self.limit = limit;
246        }
247    }
248
249    impl<'a> PriceTickerResponseDecoder<'a> {
250        pub fn wrap(
251            mut self,
252            buf: ReadBuf<'a>,
253            offset: usize,
254            acting_block_length: u16,
255            acting_version: u16,
256        ) -> Self {
257            let limit = offset + acting_block_length as usize;
258            self.buf = buf;
259            self.initial_offset = offset;
260            self.offset = offset;
261            self.limit = limit;
262            self.acting_block_length = acting_block_length;
263            self.acting_version = acting_version;
264            self
265        }
266
267        #[inline]
268        pub fn encoded_length(&self) -> usize {
269            self.limit - self.offset
270        }
271
272        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
273            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
274            let acting_block_length = header.block_length();
275            let acting_version = header.version();
276
277            self.wrap(
278                header.parent().unwrap(),
279                offset + message_header_codec::ENCODED_LENGTH,
280                acting_block_length,
281                acting_version,
282            )
283        }
284
285        /// GROUP DECODER (id=100)
286        #[inline]
287        pub fn tickers_decoder(self) -> TickersDecoder<Self> {
288            TickersDecoder::default().wrap(self)
289        }
290    }
291
292    #[derive(Debug, Default)]
293    pub struct TickersDecoder<P> {
294        parent: Option<P>,
295        block_length: u16,
296        count: u32,
297        index: usize,
298        offset: usize,
299    }
300
301    impl<'a, P> ActingVersion for TickersDecoder<P>
302    where
303        P: Reader<'a> + ActingVersion + Default,
304    {
305        #[inline]
306        fn acting_version(&self) -> u16 {
307            self.parent.as_ref().unwrap().acting_version()
308        }
309    }
310
311    impl<'a, P> Reader<'a> for TickersDecoder<P>
312    where
313        P: Reader<'a> + Default,
314    {
315        #[inline]
316        fn get_buf(&self) -> &ReadBuf<'a> {
317            self.parent.as_ref().expect("parent missing").get_buf()
318        }
319    }
320
321    impl<'a, P> Decoder<'a> for TickersDecoder<P>
322    where
323        P: Decoder<'a> + ActingVersion + Default,
324    {
325        #[inline]
326        fn get_limit(&self) -> usize {
327            self.parent.as_ref().expect("parent missing").get_limit()
328        }
329
330        #[inline]
331        fn set_limit(&mut self, limit: usize) {
332            self.parent
333                .as_mut()
334                .expect("parent missing")
335                .set_limit(limit);
336        }
337    }
338
339    impl<'a, P> TickersDecoder<P>
340    where
341        P: Decoder<'a> + ActingVersion + Default,
342    {
343        pub fn wrap(mut self, mut parent: P) -> Self {
344            let initial_offset = parent.get_limit();
345            let block_length = parent.get_buf().get_u16_at(initial_offset);
346            let count = parent.get_buf().get_u32_at(initial_offset + 2);
347            parent.set_limit(initial_offset + 6);
348            self.parent = Some(parent);
349            self.block_length = block_length;
350            self.count = count;
351            self.index = usize::MAX;
352            self.offset = 0;
353            self
354        }
355
356        /// group token - Token{signal=BEGIN_GROUP, name='tickers', referencedName='null', description='null', packageName='null', id=100, version=0, deprecated=0, encodedLength=9, offset=0, componentTokenCount=18, 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'}}
357        #[inline]
358        pub fn parent(&mut self) -> SbeResult<P> {
359            self.parent.take().ok_or(SbeErr::ParentNotSet)
360        }
361
362        #[inline]
363        pub fn acting_version(&mut self) -> u16 {
364            self.parent.as_ref().unwrap().acting_version()
365        }
366
367        #[inline]
368        pub fn count(&self) -> u32 {
369            self.count
370        }
371
372        /// will return Some(current index) when successful otherwise None
373        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
374            let index = self.index.wrapping_add(1);
375            if index >= self.count as usize {
376                return Ok(None);
377            }
378
379            if let Some(parent) = self.parent.as_mut() {
380                self.offset = parent.get_limit();
381                parent.set_limit(self.offset + self.block_length as usize);
382                self.index = index;
383                Ok(Some(index))
384            } else {
385                Err(SbeErr::ParentNotSet)
386            }
387        }
388
389        /// primitive field - 'REQUIRED'
390        #[inline]
391        pub fn price_exponent(&self) -> i8 {
392            self.get_buf().get_i8_at(self.offset)
393        }
394
395        /// primitive field - 'OPTIONAL' { null_value: '-9223372036854775808_i64' }
396        #[inline]
397        pub fn price(&self) -> Option<i64> {
398            let value = self.get_buf().get_i64_at(self.offset + 1);
399            if value == -9223372036854775808_i64 {
400                None
401            } else {
402                Some(value)
403            }
404        }
405
406        /// VAR_DATA DECODER - character encoding: 'UTF-8'
407        #[inline]
408        pub fn symbol_decoder(&mut self) -> (usize, usize) {
409            let offset = self.parent.as_ref().expect("parent missing").get_limit();
410            let data_length = self.get_buf().get_u8_at(offset) as usize;
411            self.parent
412                .as_mut()
413                .unwrap()
414                .set_limit(offset + 1 + data_length);
415            (offset + 1, data_length)
416        }
417
418        #[inline]
419        pub fn symbol_slice(&'a self, coordinates: (usize, usize)) -> &'a [u8] {
420            debug_assert!(self.get_limit() >= coordinates.0 + coordinates.1);
421            self.get_buf().get_slice_at(coordinates.0, coordinates.1)
422        }
423    }
424} // end decoder