nautilus_binance/common/sbe/spot/
klines_response_codec.rs1pub use decoder::KlinesResponseDecoder;
2pub use encoder::KlinesResponseEncoder;
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 = 203;
9
10pub mod encoder {
11 use message_header_codec::*;
12
13 use super::*;
14
15 #[derive(Debug, Default)]
16 pub struct KlinesResponseEncoder<'a> {
17 buf: WriteBuf<'a>,
18 initial_offset: usize,
19 offset: usize,
20 limit: usize,
21 }
22
23 impl<'a> Writer<'a> for KlinesResponseEncoder<'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 KlinesResponseEncoder<'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> KlinesResponseEncoder<'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 price_exponent(&mut self, value: i8) {
77 let offset = self.offset;
78 self.get_buf_mut().put_i8_at(offset, value);
79 }
80
81 #[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 #[inline]
98 pub fn klines_encoder(
99 self,
100 count: u32,
101 klines_encoder: KlinesEncoder<Self>,
102 ) -> KlinesEncoder<Self> {
103 klines_encoder.wrap(self, count)
104 }
105 }
106
107 #[derive(Debug, Default)]
108 pub struct KlinesEncoder<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 KlinesEncoder<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 KlinesEncoder<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> KlinesEncoder<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 120
171 }
172
173 #[inline]
174 pub fn parent(&mut self) -> SbeResult<P> {
175 self.parent.take().ok_or(SbeErr::ParentNotSet)
176 }
177
178 #[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 #[inline]
205 pub fn open_time(&mut self, value: i64) {
206 let offset = self.offset;
207 self.get_buf_mut().put_i64_at(offset, value);
208 }
209
210 #[inline]
220 pub fn open_price(&mut self, value: i64) {
221 let offset = self.offset + 8;
222 self.get_buf_mut().put_i64_at(offset, value);
223 }
224
225 #[inline]
235 pub fn high_price(&mut self, value: i64) {
236 let offset = self.offset + 16;
237 self.get_buf_mut().put_i64_at(offset, value);
238 }
239
240 #[inline]
250 pub fn low_price(&mut self, value: i64) {
251 let offset = self.offset + 24;
252 self.get_buf_mut().put_i64_at(offset, value);
253 }
254
255 #[inline]
265 pub fn close_price(&mut self, value: i64) {
266 let offset = self.offset + 32;
267 self.get_buf_mut().put_i64_at(offset, value);
268 }
269
270 #[inline]
271 pub fn volume_at(&mut self, index: usize, value: u8) {
272 let offset = self.offset + 40;
273 let buf = self.get_buf_mut();
274 buf.put_u8_at(offset + index, value);
275 }
276
277 #[inline]
287 pub fn volume(&mut self, value: &[u8]) {
288 debug_assert_eq!(16, value.len());
289 let offset = self.offset + 40;
290 let buf = self.get_buf_mut();
291 buf.put_slice_at(offset, value);
292 }
293
294 #[inline]
304 pub fn volume_from_iter(&mut self, iter: impl Iterator<Item = u8>) {
305 let offset = self.offset + 40;
306 let buf = self.get_buf_mut();
307 for (i, v) in iter.enumerate() {
308 buf.put_u8_at(offset + i, v);
309 }
310 }
311
312 #[inline]
322 pub fn volume_zero_padded(&mut self, value: &[u8]) {
323 let iter = value
324 .iter()
325 .copied()
326 .chain(std::iter::repeat(0_u8))
327 .take(16);
328 self.volume_from_iter(iter);
329 }
330
331 #[inline]
341 pub fn close_time(&mut self, value: i64) {
342 let offset = self.offset + 56;
343 self.get_buf_mut().put_i64_at(offset, value);
344 }
345
346 #[inline]
347 pub fn quote_volume_at(&mut self, index: usize, value: u8) {
348 let offset = self.offset + 64;
349 let buf = self.get_buf_mut();
350 buf.put_u8_at(offset + index, value);
351 }
352
353 #[inline]
363 pub fn quote_volume(&mut self, value: &[u8]) {
364 debug_assert_eq!(16, value.len());
365 let offset = self.offset + 64;
366 let buf = self.get_buf_mut();
367 buf.put_slice_at(offset, value);
368 }
369
370 #[inline]
380 pub fn quote_volume_from_iter(&mut self, iter: impl Iterator<Item = u8>) {
381 let offset = self.offset + 64;
382 let buf = self.get_buf_mut();
383 for (i, v) in iter.enumerate() {
384 buf.put_u8_at(offset + i, v);
385 }
386 }
387
388 #[inline]
398 pub fn quote_volume_zero_padded(&mut self, value: &[u8]) {
399 let iter = value
400 .iter()
401 .copied()
402 .chain(std::iter::repeat(0_u8))
403 .take(16);
404 self.quote_volume_from_iter(iter);
405 }
406
407 #[inline]
417 pub fn num_trades(&mut self, value: i64) {
418 let offset = self.offset + 80;
419 self.get_buf_mut().put_i64_at(offset, value);
420 }
421
422 #[inline]
423 pub fn taker_buy_base_volume_at(&mut self, index: usize, value: u8) {
424 let offset = self.offset + 88;
425 let buf = self.get_buf_mut();
426 buf.put_u8_at(offset + index, value);
427 }
428
429 #[inline]
439 pub fn taker_buy_base_volume(&mut self, value: &[u8]) {
440 debug_assert_eq!(16, value.len());
441 let offset = self.offset + 88;
442 let buf = self.get_buf_mut();
443 buf.put_slice_at(offset, value);
444 }
445
446 #[inline]
456 pub fn taker_buy_base_volume_from_iter(&mut self, iter: impl Iterator<Item = u8>) {
457 let offset = self.offset + 88;
458 let buf = self.get_buf_mut();
459 for (i, v) in iter.enumerate() {
460 buf.put_u8_at(offset + i, v);
461 }
462 }
463
464 #[inline]
474 pub fn taker_buy_base_volume_zero_padded(&mut self, value: &[u8]) {
475 let iter = value
476 .iter()
477 .copied()
478 .chain(std::iter::repeat(0_u8))
479 .take(16);
480 self.taker_buy_base_volume_from_iter(iter);
481 }
482
483 #[inline]
484 pub fn taker_buy_quote_volume_at(&mut self, index: usize, value: u8) {
485 let offset = self.offset + 104;
486 let buf = self.get_buf_mut();
487 buf.put_u8_at(offset + index, value);
488 }
489
490 #[inline]
500 pub fn taker_buy_quote_volume(&mut self, value: &[u8]) {
501 debug_assert_eq!(16, value.len());
502 let offset = self.offset + 104;
503 let buf = self.get_buf_mut();
504 buf.put_slice_at(offset, value);
505 }
506
507 #[inline]
517 pub fn taker_buy_quote_volume_from_iter(&mut self, iter: impl Iterator<Item = u8>) {
518 let offset = self.offset + 104;
519 let buf = self.get_buf_mut();
520 for (i, v) in iter.enumerate() {
521 buf.put_u8_at(offset + i, v);
522 }
523 }
524
525 #[inline]
535 pub fn taker_buy_quote_volume_zero_padded(&mut self, value: &[u8]) {
536 let iter = value
537 .iter()
538 .copied()
539 .chain(std::iter::repeat(0_u8))
540 .take(16);
541 self.taker_buy_quote_volume_from_iter(iter);
542 }
543 }
544} pub mod decoder {
547 use message_header_codec::*;
548
549 use super::*;
550
551 #[derive(Clone, Copy, Debug, Default)]
552 pub struct KlinesResponseDecoder<'a> {
553 buf: ReadBuf<'a>,
554 initial_offset: usize,
555 offset: usize,
556 limit: usize,
557 pub acting_block_length: u16,
558 pub acting_version: u16,
559 }
560
561 impl ActingVersion for KlinesResponseDecoder<'_> {
562 #[inline]
563 fn acting_version(&self) -> u16 {
564 self.acting_version
565 }
566 }
567
568 impl<'a> Reader<'a> for KlinesResponseDecoder<'a> {
569 #[inline]
570 fn get_buf(&self) -> &ReadBuf<'a> {
571 &self.buf
572 }
573 }
574
575 impl<'a> Decoder<'a> for KlinesResponseDecoder<'a> {
576 #[inline]
577 fn get_limit(&self) -> usize {
578 self.limit
579 }
580
581 #[inline]
582 fn set_limit(&mut self, limit: usize) {
583 self.limit = limit;
584 }
585 }
586
587 impl<'a> KlinesResponseDecoder<'a> {
588 pub fn wrap(
589 mut self,
590 buf: ReadBuf<'a>,
591 offset: usize,
592 acting_block_length: u16,
593 acting_version: u16,
594 ) -> Self {
595 let limit = offset + acting_block_length as usize;
596 self.buf = buf;
597 self.initial_offset = offset;
598 self.offset = offset;
599 self.limit = limit;
600 self.acting_block_length = acting_block_length;
601 self.acting_version = acting_version;
602 self
603 }
604
605 #[inline]
606 pub fn encoded_length(&self) -> usize {
607 self.limit - self.offset
608 }
609
610 pub fn header(self, mut header: MessageHeaderDecoder<ReadBuf<'a>>, offset: usize) -> Self {
611 debug_assert_eq!(SBE_TEMPLATE_ID, header.template_id());
612 let acting_block_length = header.block_length();
613 let acting_version = header.version();
614
615 self.wrap(
616 header.parent().unwrap(),
617 offset + message_header_codec::ENCODED_LENGTH,
618 acting_block_length,
619 acting_version,
620 )
621 }
622
623 #[inline]
625 pub fn price_exponent(&self) -> i8 {
626 self.get_buf().get_i8_at(self.offset)
627 }
628
629 #[inline]
631 pub fn qty_exponent(&self) -> i8 {
632 self.get_buf().get_i8_at(self.offset + 1)
633 }
634
635 #[inline]
637 pub fn klines_decoder(self) -> KlinesDecoder<Self> {
638 KlinesDecoder::default().wrap(self)
639 }
640 }
641
642 #[derive(Debug, Default)]
643 pub struct KlinesDecoder<P> {
644 parent: Option<P>,
645 block_length: u16,
646 count: u32,
647 index: usize,
648 offset: usize,
649 }
650
651 impl<'a, P> ActingVersion for KlinesDecoder<P>
652 where
653 P: Reader<'a> + ActingVersion + Default,
654 {
655 #[inline]
656 fn acting_version(&self) -> u16 {
657 self.parent.as_ref().unwrap().acting_version()
658 }
659 }
660
661 impl<'a, P> Reader<'a> for KlinesDecoder<P>
662 where
663 P: Reader<'a> + Default,
664 {
665 #[inline]
666 fn get_buf(&self) -> &ReadBuf<'a> {
667 self.parent.as_ref().expect("parent missing").get_buf()
668 }
669 }
670
671 impl<'a, P> Decoder<'a> for KlinesDecoder<P>
672 where
673 P: Decoder<'a> + ActingVersion + Default,
674 {
675 #[inline]
676 fn get_limit(&self) -> usize {
677 self.parent.as_ref().expect("parent missing").get_limit()
678 }
679
680 #[inline]
681 fn set_limit(&mut self, limit: usize) {
682 self.parent
683 .as_mut()
684 .expect("parent missing")
685 .set_limit(limit);
686 }
687 }
688
689 impl<'a, P> KlinesDecoder<P>
690 where
691 P: Decoder<'a> + ActingVersion + Default,
692 {
693 pub fn wrap(mut self, mut parent: P) -> Self {
694 let initial_offset = parent.get_limit();
695 let block_length = parent.get_buf().get_u16_at(initial_offset);
696 let count = parent.get_buf().get_u32_at(initial_offset + 2);
697 parent.set_limit(initial_offset + 6);
698 self.parent = Some(parent);
699 self.block_length = block_length;
700 self.count = count;
701 self.index = usize::MAX;
702 self.offset = 0;
703 self
704 }
705
706 #[inline]
708 pub fn parent(&mut self) -> SbeResult<P> {
709 self.parent.take().ok_or(SbeErr::ParentNotSet)
710 }
711
712 #[inline]
713 pub fn acting_version(&mut self) -> u16 {
714 self.parent.as_ref().unwrap().acting_version()
715 }
716
717 #[inline]
718 pub fn count(&self) -> u32 {
719 self.count
720 }
721
722 pub fn advance(&mut self) -> SbeResult<Option<usize>> {
724 let index = self.index.wrapping_add(1);
725 if index >= self.count as usize {
726 return Ok(None);
727 }
728 if let Some(parent) = self.parent.as_mut() {
729 self.offset = parent.get_limit();
730 parent.set_limit(self.offset + self.block_length as usize);
731 self.index = index;
732 Ok(Some(index))
733 } else {
734 Err(SbeErr::ParentNotSet)
735 }
736 }
737
738 #[inline]
740 pub fn open_time(&self) -> i64 {
741 self.get_buf().get_i64_at(self.offset)
742 }
743
744 #[inline]
746 pub fn open_price(&self) -> i64 {
747 self.get_buf().get_i64_at(self.offset + 8)
748 }
749
750 #[inline]
752 pub fn high_price(&self) -> i64 {
753 self.get_buf().get_i64_at(self.offset + 16)
754 }
755
756 #[inline]
758 pub fn low_price(&self) -> i64 {
759 self.get_buf().get_i64_at(self.offset + 24)
760 }
761
762 #[inline]
764 pub fn close_price(&self) -> i64 {
765 self.get_buf().get_i64_at(self.offset + 32)
766 }
767
768 #[inline]
769 pub fn volume(&self) -> [u8; 16] {
770 let buf = self.get_buf();
771 ReadBuf::get_bytes_at(buf.data, self.offset + 40)
772 }
773
774 #[inline]
776 pub fn close_time(&self) -> i64 {
777 self.get_buf().get_i64_at(self.offset + 56)
778 }
779
780 #[inline]
781 pub fn quote_volume(&self) -> [u8; 16] {
782 let buf = self.get_buf();
783 ReadBuf::get_bytes_at(buf.data, self.offset + 64)
784 }
785
786 #[inline]
788 pub fn num_trades(&self) -> i64 {
789 self.get_buf().get_i64_at(self.offset + 80)
790 }
791
792 #[inline]
793 pub fn taker_buy_base_volume(&self) -> [u8; 16] {
794 let buf = self.get_buf();
795 ReadBuf::get_bytes_at(buf.data, self.offset + 88)
796 }
797
798 #[inline]
799 pub fn taker_buy_quote_volume(&self) -> [u8; 16] {
800 let buf = self.get_buf();
801 ReadBuf::get_bytes_at(buf.data, self.offset + 104)
802 }
803 }
804}