nautilus_binance/common/sbe/spot/
depth_response_codec.rs1pub use decoder::DepthResponseDecoder;
2pub use encoder::DepthResponseEncoder;
3
4use super::*;
5pub use super::{SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_SEMANTIC_VERSION};
6
7pub const SBE_BLOCK_LENGTH: u16 = 10;
8pub const SBE_TEMPLATE_ID: u16 = 200;
9
10pub mod encoder {
11 use message_header_codec::*;
12
13 use super::*;
14
15 #[derive(Debug, Default)]
16 pub struct DepthResponseEncoder<'a> {
17 buf: WriteBuf<'a>,
18 initial_offset: usize,
19 offset: usize,
20 limit: usize,
21 }
22
23 impl<'a> Writer<'a> for DepthResponseEncoder<'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 DepthResponseEncoder<'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> DepthResponseEncoder<'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 #[inline]
76 pub fn last_update_id(&mut self, value: i64) {
77 let offset = self.offset;
78 self.get_buf_mut().put_i64_at(offset, value);
79 }
80
81 #[inline]
91 pub fn price_exponent(&mut self, value: i8) {
92 let offset = self.offset + 8;
93 self.get_buf_mut().put_i8_at(offset, value);
94 }
95
96 #[inline]
106 pub fn qty_exponent(&mut self, value: i8) {
107 let offset = self.offset + 9;
108 self.get_buf_mut().put_i8_at(offset, value);
109 }
110
111 #[inline]
113 pub fn bids_encoder(
114 self,
115 count: u32,
116 bids_encoder: BidsEncoder<Self>,
117 ) -> BidsEncoder<Self> {
118 bids_encoder.wrap(self, count)
119 }
120
121 #[inline]
123 pub fn asks_encoder(
124 self,
125 count: u32,
126 asks_encoder: AsksEncoder<Self>,
127 ) -> AsksEncoder<Self> {
128 asks_encoder.wrap(self, count)
129 }
130 }
131
132 #[derive(Debug, Default)]
133 pub struct BidsEncoder<P> {
134 parent: Option<P>,
135 count: u32,
136 index: usize,
137 offset: usize,
138 initial_limit: usize,
139 }
140
141 impl<'a, P> Writer<'a> for BidsEncoder<P>
142 where
143 P: Writer<'a> + Default,
144 {
145 #[inline]
146 fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
147 if let Some(parent) = self.parent.as_mut() {
148 parent.get_buf_mut()
149 } else {
150 panic!("parent was None")
151 }
152 }
153 }
154
155 impl<'a, P> Encoder<'a> for BidsEncoder<P>
156 where
157 P: Encoder<'a> + Default,
158 {
159 #[inline]
160 fn get_limit(&self) -> usize {
161 self.parent.as_ref().expect("parent missing").get_limit()
162 }
163
164 #[inline]
165 fn set_limit(&mut self, limit: usize) {
166 self.parent
167 .as_mut()
168 .expect("parent missing")
169 .set_limit(limit);
170 }
171 }
172
173 impl<'a, P> BidsEncoder<P>
174 where
175 P: Encoder<'a> + Default,
176 {
177 #[inline]
178 pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
179 let initial_limit = parent.get_limit();
180 parent.set_limit(initial_limit + 6);
181 parent
182 .get_buf_mut()
183 .put_u16_at(initial_limit, Self::block_length());
184 parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
185 self.parent = Some(parent);
186 self.count = count;
187 self.index = usize::MAX;
188 self.offset = usize::MAX;
189 self.initial_limit = initial_limit;
190 self
191 }
192
193 #[inline]
194 pub fn block_length() -> u16 {
195 16
196 }
197
198 #[inline]
199 pub fn parent(&mut self) -> SbeResult<P> {
200 self.parent.take().ok_or(SbeErr::ParentNotSet)
201 }
202
203 #[inline]
205 pub fn advance(&mut self) -> SbeResult<Option<usize>> {
206 let index = self.index.wrapping_add(1);
207 if index >= self.count as usize {
208 return Ok(None);
209 }
210 if let Some(parent) = self.parent.as_mut() {
211 self.offset = parent.get_limit();
212 parent.set_limit(self.offset + Self::block_length() as usize);
213 self.index = index;
214 Ok(Some(index))
215 } else {
216 Err(SbeErr::ParentNotSet)
217 }
218 }
219
220 #[inline]
230 pub fn price(&mut self, value: i64) {
231 let offset = self.offset;
232 self.get_buf_mut().put_i64_at(offset, value);
233 }
234
235 #[inline]
245 pub fn qty(&mut self, value: i64) {
246 let offset = self.offset + 8;
247 self.get_buf_mut().put_i64_at(offset, value);
248 }
249 }
250
251 #[derive(Debug, Default)]
252 pub struct AsksEncoder<P> {
253 parent: Option<P>,
254 count: u32,
255 index: usize,
256 offset: usize,
257 initial_limit: usize,
258 }
259
260 impl<'a, P> Writer<'a> for AsksEncoder<P>
261 where
262 P: Writer<'a> + Default,
263 {
264 #[inline]
265 fn get_buf_mut(&mut self) -> &mut WriteBuf<'a> {
266 if let Some(parent) = self.parent.as_mut() {
267 parent.get_buf_mut()
268 } else {
269 panic!("parent was None")
270 }
271 }
272 }
273
274 impl<'a, P> Encoder<'a> for AsksEncoder<P>
275 where
276 P: Encoder<'a> + Default,
277 {
278 #[inline]
279 fn get_limit(&self) -> usize {
280 self.parent.as_ref().expect("parent missing").get_limit()
281 }
282
283 #[inline]
284 fn set_limit(&mut self, limit: usize) {
285 self.parent
286 .as_mut()
287 .expect("parent missing")
288 .set_limit(limit);
289 }
290 }
291
292 impl<'a, P> AsksEncoder<P>
293 where
294 P: Encoder<'a> + Default,
295 {
296 #[inline]
297 pub fn wrap(mut self, mut parent: P, count: u32) -> Self {
298 let initial_limit = parent.get_limit();
299 parent.set_limit(initial_limit + 6);
300 parent
301 .get_buf_mut()
302 .put_u16_at(initial_limit, Self::block_length());
303 parent.get_buf_mut().put_u32_at(initial_limit + 2, count);
304 self.parent = Some(parent);
305 self.count = count;
306 self.index = usize::MAX;
307 self.offset = usize::MAX;
308 self.initial_limit = initial_limit;
309 self
310 }
311
312 #[inline]
313 pub fn block_length() -> u16 {
314 16
315 }
316
317 #[inline]
318 pub fn parent(&mut self) -> SbeResult<P> {
319 self.parent.take().ok_or(SbeErr::ParentNotSet)
320 }
321
322 #[inline]
324 pub fn advance(&mut self) -> SbeResult<Option<usize>> {
325 let index = self.index.wrapping_add(1);
326 if index >= self.count as usize {
327 return Ok(None);
328 }
329 if let Some(parent) = self.parent.as_mut() {
330 self.offset = parent.get_limit();
331 parent.set_limit(self.offset + Self::block_length() as usize);
332 self.index = index;
333 Ok(Some(index))
334 } else {
335 Err(SbeErr::ParentNotSet)
336 }
337 }
338
339 #[inline]
349 pub fn price(&mut self, value: i64) {
350 let offset = self.offset;
351 self.get_buf_mut().put_i64_at(offset, value);
352 }
353
354 #[inline]
364 pub fn qty(&mut self, value: i64) {
365 let offset = self.offset + 8;
366 self.get_buf_mut().put_i64_at(offset, value);
367 }
368 }
369} pub mod decoder {
372 use message_header_codec::*;
373
374 use super::*;
375
376 #[derive(Clone, Copy, Debug, Default)]
377 pub struct DepthResponseDecoder<'a> {
378 buf: ReadBuf<'a>,
379 initial_offset: usize,
380 offset: usize,
381 limit: usize,
382 pub acting_block_length: u16,
383 pub acting_version: u16,
384 }
385
386 impl ActingVersion for DepthResponseDecoder<'_> {
387 #[inline]
388 fn acting_version(&self) -> u16 {
389 self.acting_version
390 }
391 }
392
393 impl<'a> Reader<'a> for DepthResponseDecoder<'a> {
394 #[inline]
395 fn get_buf(&self) -> &ReadBuf<'a> {
396 &self.buf
397 }
398 }
399
400 impl<'a> Decoder<'a> for DepthResponseDecoder<'a> {
401 #[inline]
402 fn get_limit(&self) -> usize {
403 self.limit
404 }
405
406 #[inline]
407 fn set_limit(&mut self, limit: usize) {
408 self.limit = limit;
409 }
410 }
411
412 impl<'a> DepthResponseDecoder<'a> {
413 pub fn wrap(
414 mut self,
415 buf: ReadBuf<'a>,
416 offset: usize,
417 acting_block_length: u16,
418 acting_version: u16,
419 ) -> Self {
420 let limit = offset + acting_block_length as usize;
421 self.buf = buf;
422 self.initial_offset = offset;
423 self.offset = offset;
424 self.limit = limit;
425 self.acting_block_length = acting_block_length;
426 self.acting_version = acting_version;
427 self
428 }
429
430 #[inline]
431 pub fn encoded_length(&self) -> usize {
432 self.limit - self.offset
433 }
434
435 pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
436 debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
437 let acting_block_length = header.block_length();
438 let acting_version = header.version();
439
440 self.wrap(
441 header.parent().unwrap(),
442 offset + message_header_codec::ENCODED_LENGTH,
443 acting_block_length,
444 acting_version,
445 )
446 }
447
448 #[inline]
450 pub fn last_update_id(&self) -> i64 {
451 self.get_buf().get_i64_at(self.offset)
452 }
453
454 #[inline]
456 pub fn price_exponent(&self) -> i8 {
457 self.get_buf().get_i8_at(self.offset + 8)
458 }
459
460 #[inline]
462 pub fn qty_exponent(&self) -> i8 {
463 self.get_buf().get_i8_at(self.offset + 9)
464 }
465
466 #[inline]
468 pub fn bids_decoder(self) -> BidsDecoder<Self> {
469 BidsDecoder::default().wrap(self)
470 }
471
472 #[inline]
474 pub fn asks_decoder(self) -> AsksDecoder<Self> {
475 AsksDecoder::default().wrap(self)
476 }
477 }
478
479 #[derive(Debug, Default)]
480 pub struct BidsDecoder<P> {
481 parent: Option<P>,
482 block_length: u16,
483 count: u32,
484 index: usize,
485 offset: usize,
486 }
487
488 impl<'a, P> ActingVersion for BidsDecoder<P>
489 where
490 P: Reader<'a> + ActingVersion + Default,
491 {
492 #[inline]
493 fn acting_version(&self) -> u16 {
494 self.parent.as_ref().unwrap().acting_version()
495 }
496 }
497
498 impl<'a, P> Reader<'a> for BidsDecoder<P>
499 where
500 P: Reader<'a> + Default,
501 {
502 #[inline]
503 fn get_buf(&self) -> &ReadBuf<'a> {
504 self.parent.as_ref().expect("parent missing").get_buf()
505 }
506 }
507
508 impl<'a, P> Decoder<'a> for BidsDecoder<P>
509 where
510 P: Decoder<'a> + ActingVersion + Default,
511 {
512 #[inline]
513 fn get_limit(&self) -> usize {
514 self.parent.as_ref().expect("parent missing").get_limit()
515 }
516
517 #[inline]
518 fn set_limit(&mut self, limit: usize) {
519 self.parent
520 .as_mut()
521 .expect("parent missing")
522 .set_limit(limit);
523 }
524 }
525
526 impl<'a, P> BidsDecoder<P>
527 where
528 P: Decoder<'a> + ActingVersion + Default,
529 {
530 pub fn wrap(mut self, mut parent: P) -> Self {
531 let initial_offset = parent.get_limit();
532 let block_length = parent.get_buf().get_u16_at(initial_offset);
533 let count = parent.get_buf().get_u32_at(initial_offset + 2);
534 parent.set_limit(initial_offset + 6);
535 self.parent = Some(parent);
536 self.block_length = block_length;
537 self.count = count;
538 self.index = usize::MAX;
539 self.offset = 0;
540 self
541 }
542
543 #[inline]
545 pub fn parent(&mut self) -> SbeResult<P> {
546 self.parent.take().ok_or(SbeErr::ParentNotSet)
547 }
548
549 #[inline]
550 pub fn acting_version(&mut self) -> u16 {
551 self.parent.as_ref().unwrap().acting_version()
552 }
553
554 #[inline]
555 pub fn count(&self) -> u32 {
556 self.count
557 }
558
559 pub fn advance(&mut self) -> SbeResult<Option<usize>> {
561 let index = self.index.wrapping_add(1);
562 if index >= self.count as usize {
563 return Ok(None);
564 }
565 if let Some(parent) = self.parent.as_mut() {
566 self.offset = parent.get_limit();
567 parent.set_limit(self.offset + self.block_length as usize);
568 self.index = index;
569 Ok(Some(index))
570 } else {
571 Err(SbeErr::ParentNotSet)
572 }
573 }
574
575 #[inline]
577 pub fn price(&self) -> i64 {
578 self.get_buf().get_i64_at(self.offset)
579 }
580
581 #[inline]
583 pub fn qty(&self) -> i64 {
584 self.get_buf().get_i64_at(self.offset + 8)
585 }
586 }
587
588 #[derive(Debug, Default)]
589 pub struct AsksDecoder<P> {
590 parent: Option<P>,
591 block_length: u16,
592 count: u32,
593 index: usize,
594 offset: usize,
595 }
596
597 impl<'a, P> ActingVersion for AsksDecoder<P>
598 where
599 P: Reader<'a> + ActingVersion + Default,
600 {
601 #[inline]
602 fn acting_version(&self) -> u16 {
603 self.parent.as_ref().unwrap().acting_version()
604 }
605 }
606
607 impl<'a, P> Reader<'a> for AsksDecoder<P>
608 where
609 P: Reader<'a> + Default,
610 {
611 #[inline]
612 fn get_buf(&self) -> &ReadBuf<'a> {
613 self.parent.as_ref().expect("parent missing").get_buf()
614 }
615 }
616
617 impl<'a, P> Decoder<'a> for AsksDecoder<P>
618 where
619 P: Decoder<'a> + ActingVersion + Default,
620 {
621 #[inline]
622 fn get_limit(&self) -> usize {
623 self.parent.as_ref().expect("parent missing").get_limit()
624 }
625
626 #[inline]
627 fn set_limit(&mut self, limit: usize) {
628 self.parent
629 .as_mut()
630 .expect("parent missing")
631 .set_limit(limit);
632 }
633 }
634
635 impl<'a, P> AsksDecoder<P>
636 where
637 P: Decoder<'a> + ActingVersion + Default,
638 {
639 pub fn wrap(mut self, mut parent: P) -> Self {
640 let initial_offset = parent.get_limit();
641 let block_length = parent.get_buf().get_u16_at(initial_offset);
642 let count = parent.get_buf().get_u32_at(initial_offset + 2);
643 parent.set_limit(initial_offset + 6);
644 self.parent = Some(parent);
645 self.block_length = block_length;
646 self.count = count;
647 self.index = usize::MAX;
648 self.offset = 0;
649 self
650 }
651
652 #[inline]
654 pub fn parent(&mut self) -> SbeResult<P> {
655 self.parent.take().ok_or(SbeErr::ParentNotSet)
656 }
657
658 #[inline]
659 pub fn acting_version(&mut self) -> u16 {
660 self.parent.as_ref().unwrap().acting_version()
661 }
662
663 #[inline]
664 pub fn count(&self) -> u32 {
665 self.count
666 }
667
668 pub fn advance(&mut self) -> SbeResult<Option<usize>> {
670 let index = self.index.wrapping_add(1);
671 if index >= self.count as usize {
672 return Ok(None);
673 }
674 if let Some(parent) = self.parent.as_mut() {
675 self.offset = parent.get_limit();
676 parent.set_limit(self.offset + self.block_length as usize);
677 self.index = index;
678 Ok(Some(index))
679 } else {
680 Err(SbeErr::ParentNotSet)
681 }
682 }
683
684 #[inline]
686 pub fn price(&self) -> i64 {
687 self.get_buf().get_i64_at(self.offset)
688 }
689
690 #[inline]
692 pub fn qty(&self) -> i64 {
693 self.get_buf().get_i64_at(self.offset + 8)
694 }
695 }
696}