Skip to main content

nautilus_binance/spot/sbe/generated/
web_socket_session_subscriptions_response_codec.rs

1pub use decoder::WebSocketSessionSubscriptionsResponseDecoder;
2pub use encoder::WebSocketSessionSubscriptionsResponseEncoder;
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 = 54;
9
10pub mod encoder {
11    use message_header_codec::*;
12
13    use super::*;
14
15    #[derive(Debug, Default)]
16    pub struct WebSocketSessionSubscriptionsResponseEncoder<'a> {
17        buf: WriteBuf<'a>,
18        initial_offset: usize,
19        offset: usize,
20        limit: usize,
21    }
22
23    impl<'a> Writer<'a> for WebSocketSessionSubscriptionsResponseEncoder<'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 WebSocketSessionSubscriptionsResponseEncoder<'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> WebSocketSessionSubscriptionsResponseEncoder<'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 subscriptions_encoder(
69            self,
70            count: u32,
71            subscriptions_encoder: SubscriptionsEncoder<Self>,
72        ) -> SubscriptionsEncoder<Self> {
73            subscriptions_encoder.wrap(self, count)
74        }
75    }
76
77    #[derive(Debug, Default)]
78    pub struct SubscriptionsEncoder<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 SubscriptionsEncoder<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 SubscriptionsEncoder<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> SubscriptionsEncoder<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            10
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 'subscriptionId'
167        /// - min value: 0
168        /// - max value: 65534
169        /// - null value: 0xffff_u16
170        /// - characterEncoding: null
171        /// - semanticType: null
172        /// - encodedOffset: 0
173        /// - encodedLength: 2
174        /// - version: 0
175        #[inline]
176        pub fn subscription_id(&mut self, value: u16) {
177            let offset = self.offset;
178            self.get_buf_mut().put_u16_at(offset, value);
179        }
180
181        /// primitive field 'expirationTime'
182        /// - min value: -9223372036854775807
183        /// - max value: 9223372036854775807
184        /// - null value: -9223372036854775808_i64
185        /// - characterEncoding: null
186        /// - semanticType: null
187        /// - encodedOffset: 2
188        /// - encodedLength: 8
189        /// - version: 0
190        #[inline]
191        pub fn expiration_time(&mut self, value: i64) {
192            let offset = self.offset + 2;
193            self.get_buf_mut().put_i64_at(offset, value);
194        }
195    }
196} // end encoder
197
198pub mod decoder {
199    use message_header_codec::*;
200
201    use super::*;
202
203    #[derive(Clone, Copy, Debug, Default)]
204    pub struct WebSocketSessionSubscriptionsResponseDecoder<'a> {
205        buf: ReadBuf<'a>,
206        initial_offset: usize,
207        offset: usize,
208        limit: usize,
209        pub acting_block_length: u16,
210        pub acting_version: u16,
211    }
212
213    impl ActingVersion for WebSocketSessionSubscriptionsResponseDecoder<'_> {
214        #[inline]
215        fn acting_version(&self) -> u16 {
216            self.acting_version
217        }
218    }
219
220    impl<'a> Reader<'a> for WebSocketSessionSubscriptionsResponseDecoder<'a> {
221        #[inline]
222        fn get_buf(&self) -> &ReadBuf<'a> {
223            &self.buf
224        }
225    }
226
227    impl<'a> Decoder<'a> for WebSocketSessionSubscriptionsResponseDecoder<'a> {
228        #[inline]
229        fn get_limit(&self) -> usize {
230            self.limit
231        }
232
233        #[inline]
234        fn set_limit(&mut self, limit: usize) {
235            self.limit = limit;
236        }
237    }
238
239    impl<'a> WebSocketSessionSubscriptionsResponseDecoder<'a> {
240        pub fn wrap(
241            mut self,
242            buf: ReadBuf<'a>,
243            offset: usize,
244            acting_block_length: u16,
245            acting_version: u16,
246        ) -> Self {
247            let limit = offset + acting_block_length as usize;
248            self.buf = buf;
249            self.initial_offset = offset;
250            self.offset = offset;
251            self.limit = limit;
252            self.acting_block_length = acting_block_length;
253            self.acting_version = acting_version;
254            self
255        }
256
257        #[inline]
258        pub fn encoded_length(&self) -> usize {
259            self.limit - self.offset
260        }
261
262        pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
263            debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
264            let acting_block_length = header.block_length();
265            let acting_version = header.version();
266
267            self.wrap(
268                header.parent().unwrap(),
269                offset + message_header_codec::ENCODED_LENGTH,
270                acting_block_length,
271                acting_version,
272            )
273        }
274
275        /// GROUP DECODER (id=100)
276        #[inline]
277        pub fn subscriptions_decoder(self) -> SubscriptionsDecoder<Self> {
278            SubscriptionsDecoder::default().wrap(self)
279        }
280    }
281
282    #[derive(Debug, Default)]
283    pub struct SubscriptionsDecoder<P> {
284        parent: Option<P>,
285        block_length: u16,
286        count: u32,
287        index: usize,
288        offset: usize,
289    }
290
291    impl<'a, P> ActingVersion for SubscriptionsDecoder<P>
292    where
293        P: Reader<'a> + ActingVersion + Default,
294    {
295        #[inline]
296        fn acting_version(&self) -> u16 {
297            self.parent.as_ref().unwrap().acting_version()
298        }
299    }
300
301    impl<'a, P> Reader<'a> for SubscriptionsDecoder<P>
302    where
303        P: Reader<'a> + Default,
304    {
305        #[inline]
306        fn get_buf(&self) -> &ReadBuf<'a> {
307            self.parent.as_ref().expect("parent missing").get_buf()
308        }
309    }
310
311    impl<'a, P> Decoder<'a> for SubscriptionsDecoder<P>
312    where
313        P: Decoder<'a> + ActingVersion + Default,
314    {
315        #[inline]
316        fn get_limit(&self) -> usize {
317            self.parent.as_ref().expect("parent missing").get_limit()
318        }
319
320        #[inline]
321        fn set_limit(&mut self, limit: usize) {
322            self.parent
323                .as_mut()
324                .expect("parent missing")
325                .set_limit(limit);
326        }
327    }
328
329    impl<'a, P> SubscriptionsDecoder<P>
330    where
331        P: Decoder<'a> + ActingVersion + Default,
332    {
333        pub fn wrap(mut self, mut parent: P) -> Self {
334            let initial_offset = parent.get_limit();
335            let block_length = parent.get_buf().get_u16_at(initial_offset);
336            let count = parent.get_buf().get_u32_at(initial_offset + 2);
337            parent.set_limit(initial_offset + 6);
338            self.parent = Some(parent);
339            self.block_length = block_length;
340            self.count = count;
341            self.index = usize::MAX;
342            self.offset = 0;
343            self
344        }
345
346        /// group token - Token{signal=BEGIN_GROUP, name='subscriptions', referencedName='null', description='null', packageName='null', id=100, version=0, deprecated=0, encodedLength=10, offset=0, componentTokenCount=12, 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'}}
347        #[inline]
348        pub fn parent(&mut self) -> SbeResult<P> {
349            self.parent.take().ok_or(SbeErr::ParentNotSet)
350        }
351
352        #[inline]
353        pub fn acting_version(&mut self) -> u16 {
354            self.parent.as_ref().unwrap().acting_version()
355        }
356
357        #[inline]
358        pub fn count(&self) -> u32 {
359            self.count
360        }
361
362        /// will return Some(current index) when successful otherwise None
363        pub fn advance(&mut self) -> SbeResult<Option<usize>> {
364            let index = self.index.wrapping_add(1);
365            if index >= self.count as usize {
366                return Ok(None);
367            }
368
369            if let Some(parent) = self.parent.as_mut() {
370                self.offset = parent.get_limit();
371                parent.set_limit(self.offset + self.block_length as usize);
372                self.index = index;
373                Ok(Some(index))
374            } else {
375                Err(SbeErr::ParentNotSet)
376            }
377        }
378
379        /// primitive field - 'REQUIRED'
380        #[inline]
381        pub fn subscription_id(&self) -> u16 {
382            self.get_buf().get_u16_at(self.offset)
383        }
384
385        /// primitive field - 'OPTIONAL' { null_value: '-9223372036854775808_i64' }
386        #[inline]
387        pub fn expiration_time(&self) -> Option<i64> {
388            let value = self.get_buf().get_i64_at(self.offset + 2);
389            if value == -9223372036854775808_i64 {
390                None
391            } else {
392                Some(value)
393            }
394        }
395    }
396} // end decoder