1use crate::{
2 format::Format,
3 helpers::{take_byte, take_byte_iter, take_num, take_num_iter},
4 Error, Unpackable,
5};
6
7impl Unpackable for u8 {
8 type Error = Error;
9
10 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
11 let format = take_byte(&mut buf)?;
12 match format {
13 0x00..=Format::POSITIVE_FIXINT => Ok((1, format)),
14 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v)),
15 _ => Err(Error::UnexpectedFormatTag),
16 }
17 }
18
19 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
20 where
21 I: IntoIterator<Item = u8>,
22 {
23 let mut bytes = bytes.into_iter();
24 let format = take_byte_iter(bytes.by_ref())?;
25 match format {
26 0x00..=Format::POSITIVE_FIXINT => Ok((1, format)),
27 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v)),
28 _ => Err(Error::UnexpectedFormatTag),
29 }
30 }
31}
32
33impl Unpackable for u16 {
34 type Error = Error;
35
36 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
37 let format = take_byte(&mut buf)?;
38 match format {
39 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u16)),
40 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v as u16)),
41 Format::UINT16 => take_num(&mut buf, u16::from_be_bytes).map(|v| (3, v)),
42 _ => Err(Error::UnexpectedFormatTag),
43 }
44 }
45
46 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
47 where
48 I: IntoIterator<Item = u8>,
49 {
50 let mut bytes = bytes.into_iter();
51 let format = take_byte_iter(bytes.by_ref())?;
52 match format {
53 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u16)),
54 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v as u16)),
55 Format::UINT16 => take_num_iter(bytes, u16::from_be_bytes).map(|v| (3, v)),
56 _ => Err(Error::UnexpectedFormatTag),
57 }
58 }
59}
60
61impl Unpackable for u32 {
62 type Error = Error;
63
64 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
65 let format = take_byte(&mut buf)?;
66 match format {
67 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u32)),
68 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v as u32)),
69 Format::UINT16 => take_num(&mut buf, u16::from_be_bytes).map(|v| (3, v as u32)),
70 Format::UINT32 => take_num(&mut buf, u32::from_be_bytes).map(|v| (5, v)),
71 _ => Err(Error::UnexpectedFormatTag),
72 }
73 }
74
75 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
76 where
77 I: IntoIterator<Item = u8>,
78 {
79 let mut bytes = bytes.into_iter();
80 let format = take_byte_iter(bytes.by_ref())?;
81 match format {
82 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u32)),
83 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v as u32)),
84 Format::UINT16 => take_num_iter(bytes, u16::from_be_bytes).map(|v| (3, v as u32)),
85 Format::UINT32 => take_num_iter(bytes, u32::from_be_bytes).map(|v| (5, v)),
86 _ => Err(Error::UnexpectedFormatTag),
87 }
88 }
89}
90
91impl Unpackable for u64 {
92 type Error = Error;
93
94 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
95 let format = take_byte(&mut buf)?;
96 match format {
97 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u64)),
98 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v as u64)),
99 Format::UINT16 => take_num(&mut buf, u16::from_be_bytes).map(|v| (3, v as u64)),
100 Format::UINT32 => take_num(&mut buf, u32::from_be_bytes).map(|v| (5, v as u64)),
101 Format::UINT64 => take_num(&mut buf, u64::from_be_bytes).map(|v| (9, v)),
102 _ => Err(Error::UnexpectedFormatTag),
103 }
104 }
105
106 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
107 where
108 I: IntoIterator<Item = u8>,
109 {
110 let mut bytes = bytes.into_iter();
111 let format = take_byte_iter(bytes.by_ref())?;
112 match format {
113 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u64)),
114 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v as u64)),
115 Format::UINT16 => take_num_iter(bytes, u16::from_be_bytes).map(|v| (3, v as u64)),
116 Format::UINT32 => take_num_iter(bytes, u32::from_be_bytes).map(|v| (5, v as u64)),
117 Format::UINT64 => take_num_iter(bytes, u64::from_be_bytes).map(|v| (9, v)),
118 _ => Err(Error::UnexpectedFormatTag),
119 }
120 }
121}
122
123impl Unpackable for u128 {
124 type Error = Error;
125
126 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
127 let format = take_byte(&mut buf)?;
128 match format {
129 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u128)),
130 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v as u128)),
131 Format::UINT16 => take_num(&mut buf, u16::from_be_bytes).map(|v| (3, v as u128)),
132 Format::UINT32 => take_num(&mut buf, u32::from_be_bytes).map(|v| (5, v as u128)),
133 Format::UINT64 => take_num(&mut buf, u64::from_be_bytes).map(|v| (9, v as u128)),
134 Format::BIN8 => {
135 if take_byte(&mut buf)? != 16 {
136 return Err(Error::UnexpectedBinLength);
137 }
138 take_num(&mut buf, u128::from_be_bytes).map(|v| (18, v))
139 }
140 _ => Err(Error::UnexpectedFormatTag),
141 }
142 }
143
144 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
145 where
146 I: IntoIterator<Item = u8>,
147 {
148 let mut bytes = bytes.into_iter();
149 let format = take_byte_iter(bytes.by_ref())?;
150 match format {
151 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as u128)),
152 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v as u128)),
153 Format::UINT16 => take_num_iter(bytes, u16::from_be_bytes).map(|v| (3, v as u128)),
154 Format::UINT32 => take_num_iter(bytes, u32::from_be_bytes).map(|v| (5, v as u128)),
155 Format::UINT64 => take_num_iter(bytes, u64::from_be_bytes).map(|v| (9, v as u128)),
156 Format::BIN8 => {
157 if take_byte_iter(bytes.by_ref())? != 16 {
158 return Err(Error::UnexpectedBinLength);
159 }
160 take_num_iter(bytes, u128::from_be_bytes).map(|v| (18, v))
161 }
162 _ => Err(Error::UnexpectedFormatTag),
163 }
164 }
165}
166
167impl Unpackable for usize {
168 type Error = Error;
169
170 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
171 let format = take_byte(&mut buf)?;
172 match format {
173 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as usize)),
174 Format::UINT8 => take_byte(&mut buf).map(|v| (2, v as usize)),
175 Format::UINT16 => take_num(&mut buf, u16::from_be_bytes).map(|v| (3, v as usize)),
176 Format::UINT32 => take_num(&mut buf, u32::from_be_bytes).map(|v| (5, v as usize)),
177 Format::UINT64 => take_num(&mut buf, u64::from_be_bytes).map(|v| (9, v as usize)),
178 _ => Err(Error::UnexpectedFormatTag),
179 }
180 }
181
182 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
183 where
184 I: IntoIterator<Item = u8>,
185 {
186 let mut bytes = bytes.into_iter();
187 let format = take_byte_iter(bytes.by_ref())?;
188 match format {
189 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as usize)),
190 Format::UINT8 => take_byte_iter(bytes).map(|v| (2, v as usize)),
191 Format::UINT16 => take_num_iter(bytes, u16::from_be_bytes).map(|v| (3, v as usize)),
192 Format::UINT32 => take_num_iter(bytes, u32::from_be_bytes).map(|v| (5, v as usize)),
193 Format::UINT64 => take_num_iter(bytes, usize::from_be_bytes).map(|v| (9, v)),
194 _ => Err(Error::UnexpectedFormatTag),
195 }
196 }
197}
198
199impl Unpackable for i8 {
200 type Error = Error;
201
202 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
203 let format = take_byte(&mut buf)?;
204 match format {
205 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as i8)),
206 0xe0.. => Ok((1, format as i8)),
207 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8)),
208 _ => Err(Error::UnexpectedFormatTag),
209 }
210 }
211
212 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
213 where
214 I: IntoIterator<Item = u8>,
215 {
216 let mut bytes = bytes.into_iter();
217 let format = take_byte_iter(bytes.by_ref())?;
218 match format {
219 0x00..=Format::POSITIVE_FIXINT => Ok((1, format as i8)),
220 0xe0.. => Ok((1, format as i8)),
221 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8)),
222 _ => Err(Error::UnexpectedFormatTag),
223 }
224 }
225}
226
227impl Unpackable for i16 {
228 type Error = Error;
229
230 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
231 let format = take_byte(&mut buf)?;
232 match format {
233 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i16)),
234 0xe0.. => Ok((1, (format as i8) as i16)),
235 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8 as i16)),
236 Format::INT16 => take_num(&mut buf, i16::from_be_bytes).map(|v| (3, v)),
237 _ => Err(Error::UnexpectedFormatTag),
238 }
239 }
240
241 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
242 where
243 I: IntoIterator<Item = u8>,
244 {
245 let mut bytes = bytes.into_iter();
246 let format = take_byte_iter(bytes.by_ref())?;
247 match format {
248 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i16)),
249 0xe0.. => Ok((1, (format as i8) as i16)),
250 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8 as i16)),
251 Format::INT16 => take_num_iter(bytes, i16::from_be_bytes).map(|v| (3, v)),
252 _ => Err(Error::UnexpectedFormatTag),
253 }
254 }
255}
256
257impl Unpackable for i32 {
258 type Error = Error;
259
260 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
261 let format = take_byte(&mut buf)?;
262 match format {
263 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i32)),
264 0xe0.. => Ok((1, (format as i8) as i32)),
265 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8 as i32)),
266 Format::INT16 => take_num(&mut buf, i16::from_be_bytes).map(|v| (3, v as i32)),
267 Format::INT32 => take_num(&mut buf, i32::from_be_bytes).map(|v| (5, v)),
268 _ => Err(Error::UnexpectedFormatTag),
269 }
270 }
271
272 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
273 where
274 I: IntoIterator<Item = u8>,
275 {
276 let mut bytes = bytes.into_iter();
277 let format = take_byte_iter(bytes.by_ref())?;
278 match format {
279 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i32)),
280 0xe0.. => Ok((1, (format as i8) as i32)),
281 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8 as i32)),
282 Format::INT16 => take_num_iter(bytes, i16::from_be_bytes).map(|v| (3, v as i32)),
283 Format::INT32 => take_num_iter(bytes, i32::from_be_bytes).map(|v| (5, v)),
284 _ => Err(Error::UnexpectedFormatTag),
285 }
286 }
287}
288
289impl Unpackable for i64 {
290 type Error = Error;
291
292 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
293 let format = take_byte(&mut buf)?;
294 match format {
295 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i64)),
296 0xe0.. => Ok((1, (format as i8) as i64)),
297 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8 as i64)),
298 Format::INT16 => take_num(&mut buf, i16::from_be_bytes).map(|v| (3, v as i64)),
299 Format::INT32 => take_num(&mut buf, i32::from_be_bytes).map(|v| (5, v as i64)),
300 Format::INT64 => take_num(&mut buf, i64::from_be_bytes).map(|v| (9, v)),
301 _ => Err(Error::UnexpectedFormatTag),
302 }
303 }
304
305 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
306 where
307 I: IntoIterator<Item = u8>,
308 {
309 let mut bytes = bytes.into_iter();
310 let format = take_byte_iter(bytes.by_ref())?;
311 match format {
312 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i64)),
313 0xe0.. => Ok((1, (format as i8) as i64)),
314 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8 as i64)),
315 Format::INT16 => take_num_iter(bytes, i16::from_be_bytes).map(|v| (3, v as i64)),
316 Format::INT32 => take_num_iter(bytes, i32::from_be_bytes).map(|v| (5, v as i64)),
317 Format::INT64 => take_num_iter(bytes, i64::from_be_bytes).map(|v| (9, v)),
318 _ => Err(Error::UnexpectedFormatTag),
319 }
320 }
321}
322
323impl Unpackable for i128 {
324 type Error = Error;
325
326 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
327 let format = take_byte(&mut buf)?;
328 match format {
329 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i128)),
330 0xe0.. => Ok((1, (format as i8) as i128)),
331 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8 as i128)),
332 Format::INT16 => take_num(&mut buf, i16::from_be_bytes).map(|v| (3, v as i128)),
333 Format::INT32 => take_num(&mut buf, i32::from_be_bytes).map(|v| (5, v as i128)),
334 Format::INT64 => take_num(&mut buf, i64::from_be_bytes).map(|v| (9, v as i128)),
335 Format::BIN8 => {
336 if take_byte(&mut buf)? != 16 {
337 return Err(Error::UnexpectedBinLength);
338 }
339 take_num(&mut buf, i128::from_be_bytes).map(|v| (18, v))
340 }
341 _ => Err(Error::UnexpectedFormatTag),
342 }
343 }
344
345 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
346 where
347 I: IntoIterator<Item = u8>,
348 {
349 let mut bytes = bytes.into_iter();
350 let format = take_byte_iter(bytes.by_ref())?;
351 match format {
352 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as i128)),
353 0xe0.. => Ok((1, (format as i8) as i128)),
354 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8 as i128)),
355 Format::INT16 => take_num_iter(bytes, i16::from_be_bytes).map(|v| (3, v as i128)),
356 Format::INT32 => take_num_iter(bytes, i32::from_be_bytes).map(|v| (5, v as i128)),
357 Format::INT64 => take_num_iter(bytes, i64::from_be_bytes).map(|v| (9, v as i128)),
358 Format::BIN8 => {
359 if take_byte_iter(bytes.by_ref())? != 16 {
360 return Err(Error::UnexpectedBinLength);
361 }
362 take_num_iter(bytes, i128::from_be_bytes).map(|v| (18, v))
363 }
364 _ => Err(Error::UnexpectedFormatTag),
365 }
366 }
367}
368
369impl Unpackable for isize {
370 type Error = Error;
371
372 fn unpack_with_ofs(mut buf: &[u8]) -> Result<(usize, Self), Self::Error> {
373 let format = take_byte(&mut buf)?;
374 match format {
375 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as isize)),
376 0xe0.. => Ok((1, (format as i8) as isize)),
377 Format::INT8 => take_byte(&mut buf).map(|v| (2, v as i8 as isize)),
378 Format::INT16 => take_num(&mut buf, i16::from_be_bytes).map(|v| (3, v as isize)),
379 Format::INT32 => take_num(&mut buf, i32::from_be_bytes).map(|v| (5, v as isize)),
380 Format::INT64 => take_num(&mut buf, i64::from_be_bytes).map(|v| (9, v as isize)),
381 _ => Err(Error::UnexpectedFormatTag),
382 }
383 }
384
385 fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
386 where
387 I: IntoIterator<Item = u8>,
388 {
389 let mut bytes = bytes.into_iter();
390 let format = take_byte_iter(bytes.by_ref())?;
391 match format {
392 0x00..=Format::POSITIVE_FIXINT => Ok((1, (format as i8) as isize)),
393 0xe0.. => Ok((1, (format as i8) as isize)),
394 Format::INT8 => take_byte_iter(bytes).map(|v| (2, v as i8 as isize)),
395 Format::INT16 => take_num_iter(bytes, i16::from_be_bytes).map(|v| (3, v as isize)),
396 Format::INT32 => take_num_iter(bytes, i32::from_be_bytes).map(|v| (5, v as isize)),
397 Format::INT64 => take_num_iter(bytes, i64::from_be_bytes).map(|v| (9, v as isize)),
398 _ => Err(Error::UnexpectedFormatTag),
399 }
400 }
401}