Skip to main content

nautilus_binance/spot/sbe/generated/
web_socket_response_codec.rs

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