msgpacker/unpack/
int.rs

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