substrate_data/wire/binary/decode/
decoder_straight.rs1use bumpalo::Bump;
6
7use super::decoder::{F32_BYTES, F64_BYTES, MAP_ENTRY_MIN};
8use super::decoder_value::inline_int;
9use super::shape::Walk;
10use crate::binary::{Decoder, tag, unzigzag};
11use crate::model::number::int_to_f64;
12
13impl<'b> Decoder<'b> {
14 fn next_is(&mut self, t: u8) -> Option<()> {
16 (self.b.get(self.i) == Some(&t)).then(|| self.i += 1)
17 }
18
19 pub fn null(&mut self) -> bool {
21 self.next_is(tag::NULL).is_some()
22 }
23
24 pub fn bool(&mut self) -> Option<bool> {
26 match *self.b.get(self.i)? {
27 tag::FALSE | tag::TRUE => {
28 self.i += 1;
29 Some(self.b[self.i - 1] == tag::TRUE)
30 }
31 _ => None,
32 }
33 }
34
35 pub fn int(&mut self) -> Option<i64> {
37 let t = *self.b.get(self.i)?;
38 if self.is_inline_int(t) {
39 self.i += 1;
40 return Some(inline_int(t));
41 }
42 self.next_is(tag::INT)?;
43 self.varint().ok().map(unzigzag)
44 }
45
46 pub fn float(&mut self) -> Option<f64> {
48 match *self.b.get(self.i)? {
49 tag::FLOAT => {
50 self.i += 1;
51 self.fixed().ok().map(f64::from_le_bytes)
52 }
53 tag::FLOAT32 => {
54 self.i += 1;
55 self.fixed().ok().map(|b| f64::from(f32::from_le_bytes(b)))
56 }
57 t if t == tag::INT || self.is_inline_int(t) => self.int().map(int_to_f64),
58 _ => None,
59 }
60 }
61
62 pub fn str(&mut self) -> Option<&'b str> {
64 let t = *self.b.get(self.i)?;
65 if self.is_indexed(t) {
66 self.i += 1;
67 return self.indexed(t).ok();
68 }
69 self.next_is(tag::STR)?;
70 self.string().ok()
71 }
72
73 pub fn bytes(&mut self) -> Option<&'b [u8]> {
75 self.next_is(tag::BYTES)?;
76 self.length_bytes().ok()
77 }
78
79 pub fn list(&mut self) -> Option<usize> {
81 self.next_is(tag::LIST)?;
82 let n = self.count(1).ok()?;
83 self.deeper().ok()?;
84 Some(n)
85 }
86
87 pub fn ints(&mut self) -> Option<usize> {
89 self.next_is(tag::INTS)?;
90 self.count(1).ok()
91 }
92
93 pub fn stream(&mut self) -> Option<&'b [u8]> {
95 self.next_is(tag::STREAM)?;
96 self.stream_body().ok()
97 }
98
99 pub(crate) fn offset(&self) -> usize {
101 self.i
102 }
103
104 pub(crate) fn rest(&self) -> &'b [u8] {
106 &self.b[self.i..]
107 }
108
109 pub(crate) fn stream_count(&mut self) -> Option<usize> {
111 self.count(1).ok()
112 }
113
114 pub fn floats(&mut self) -> Option<Vec<f64>> {
117 if self.next_is(tag::FLOATS32).is_some() {
118 return Some(crate::frame::floats::widen(
119 self.packed_floats::<F32_BYTES>()
120 .ok()?
121 .as_slice()
122 .as_flattened(),
123 ));
124 }
125 self.next_is(tag::FLOATS)?;
126 Some(
127 self.packed_floats::<F64_BYTES>()
128 .ok()?
129 .map(|f| f64::from_le_bytes(*f))
130 .collect(),
131 )
132 }
133
134 pub fn floats32(&mut self) -> Option<Vec<f32>> {
136 self.next_is(tag::FLOATS32)?;
137 Some(
138 self.packed_floats::<F32_BYTES>()
139 .ok()?
140 .map(|f| f32::from_le_bytes(*f))
141 .collect(),
142 )
143 }
144
145 pub fn packed_int(&mut self) -> Option<i64> {
147 self.varint().ok().map(unzigzag)
148 }
149
150 pub fn map(&mut self) -> Option<usize> {
153 match *self.b.get(self.i)? {
154 tag::MAP => {
155 self.i += 1;
156 let n = self.count(MAP_ENTRY_MIN).ok()?;
157 self.deeper().ok()?;
158 Some(n)
159 }
160 tag::MAP_SHAPE if self.v2 => {
161 self.i += 1;
162 let shape = self.shape_spec().ok()?;
163 let n = self.shape_width(shape).ok()?;
164 self.deeper().ok()?;
165 self.frames.push(Walk {
166 depth: self.depth,
167 shape,
168 next: 0,
169 });
170 Some(n)
171 }
172 _ => None,
173 }
174 }
175
176 pub fn leave(&mut self) {
178 self.depth = self.depth.saturating_sub(1);
179 if self.frames.last().is_some_and(|f| f.depth > self.depth) {
180 self.frames.pop();
181 }
182 }
183
184 pub fn key(&mut self) -> Option<&'b str> {
186 if let Some(frame) = self.frames.last_mut()
187 && frame.depth == self.depth
188 {
189 let string = self.shapes.keys_of(frame.shape).get(frame.next).copied()?;
190 frame.next += 1;
191 return Some(self.strings[string]);
192 }
193 self.string().ok()
194 }
195
196 pub fn skip(&mut self) -> Option<()> {
198 let arena = Bump::new();
199 self.value_ref(&arena).ok().map(drop)
200 }
201}