serde_rlp/
de.rs

1use std::collections::VecDeque;
2use std::ops::{AddAssign, MulAssign, Neg};
3
4use serde::de::{self, Deserialize, DeserializeSeed, SeqAccess, Visitor};
5
6use rlp::{self, ExpectedType};
7
8use error::{Error, Result};
9use std::str;
10
11pub struct Deserializer<'de> {
12    input: &'de [u8],
13    /// Stacked input slices for nested data
14    stack: VecDeque<&'de [u8]>,
15}
16
17impl<'de> Deserializer<'de> {
18    pub fn from_bytes(input: &'de [u8]) -> Self {
19        Deserializer {
20            input: input,
21            stack: VecDeque::new(),
22        }
23    }
24}
25
26pub fn from_bytes<'a, T>(s: &'a [u8]) -> Result<T>
27where
28    T: Deserialize<'a>,
29{
30    let mut deserializer = Deserializer::from_bytes(s);
31    let t = T::deserialize(&mut deserializer)?;
32    if deserializer.input.len() == 0 {
33        Ok(t)
34    } else {
35        Err(Error::TrailingBytes)
36    }
37}
38
39impl<'de> Deserializer<'de> {
40    // Parse the JSON identifier `true` or `false`.
41    fn parse_bool(&mut self) -> Result<bool> {
42        unimplemented!();
43    }
44
45    fn parse_unsigned<T>(&mut self) -> Result<T>
46    where
47        T: AddAssign<T> + MulAssign<T> + From<u8>,
48    {
49        unimplemented!();
50    }
51
52    fn parse_signed<T>(&mut self) -> Result<T>
53    where
54        T: Neg<Output = T> + AddAssign<T> + MulAssign<T> + From<i8>,
55    {
56        // Optional minus sign, delegate to `parse_unsigned`, negate if negative.
57        unimplemented!()
58    }
59
60    fn parse_string(&mut self) -> Result<&'de str> {
61        let res = rlp::decode_length(&self.input)?;
62        if res.expected_type == ExpectedType::StringType {
63            let s = str::from_utf8(&self.input[res.offset..res.offset + res.length])
64                .map_err(|_| Error::InvalidString)?;
65            self.input = &self.input[res.offset + res.length..];
66            Ok(s)
67        } else {
68            Err(Error::ExpectedString)
69        }
70    }
71
72    fn parse_bytes(&mut self) -> Result<&'de [u8]> {
73        let res = rlp::decode_length(&self.input)?;
74        if res.expected_type == ExpectedType::StringType {
75            let s = &self.input[res.offset..res.offset + res.length];
76            self.input = &self.input[res.offset + res.length..];
77            Ok(s)
78        } else {
79            Err(Error::ExpectedString)
80        }
81    }
82}
83
84impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
85    type Error = Error;
86
87    fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value>
88    where
89        V: Visitor<'de>,
90    {
91        unimplemented!();
92    }
93
94    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
95    where
96        V: Visitor<'de>,
97    {
98        visitor.visit_bool(self.parse_bool()?)
99    }
100
101    // The `parse_signed` function is generic over the integer type `T` so here
102    // it is invoked with `T=i8`. The next 8 methods are similar.
103    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
104    where
105        V: Visitor<'de>,
106    {
107        visitor.visit_i8(self.parse_signed()?)
108    }
109
110    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
111    where
112        V: Visitor<'de>,
113    {
114        visitor.visit_i16(self.parse_signed()?)
115    }
116
117    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
118    where
119        V: Visitor<'de>,
120    {
121        visitor.visit_i32(self.parse_signed()?)
122    }
123
124    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
125    where
126        V: Visitor<'de>,
127    {
128        visitor.visit_i64(self.parse_signed()?)
129    }
130
131    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
132    where
133        V: Visitor<'de>,
134    {
135        visitor.visit_u8(self.parse_unsigned()?)
136    }
137
138    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
139    where
140        V: Visitor<'de>,
141    {
142        visitor.visit_u16(self.parse_unsigned()?)
143    }
144
145    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
146    where
147        V: Visitor<'de>,
148    {
149        visitor.visit_u32(self.parse_unsigned()?)
150    }
151
152    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
153    where
154        V: Visitor<'de>,
155    {
156        visitor.visit_u64(self.parse_unsigned()?)
157    }
158
159    fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
160    where
161        V: Visitor<'de>,
162    {
163        unimplemented!()
164    }
165
166    // Float parsing is stupidly hard.
167    fn deserialize_f64<V>(self, _visitor: V) -> Result<V::Value>
168    where
169        V: Visitor<'de>,
170    {
171        unimplemented!()
172    }
173
174    fn deserialize_char<V>(self, _visitor: V) -> Result<V::Value>
175    where
176        V: Visitor<'de>,
177    {
178        // Parse a string, check that it is one character, call `visit_char`.
179        unimplemented!()
180    }
181
182    // Refer to the "Understanding deserializer lifetimes" page for information
183    // about the three deserialization flavors of strings in Serde.
184    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
185    where
186        V: Visitor<'de>,
187    {
188        visitor.visit_borrowed_str(self.parse_string()?)
189    }
190
191    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
192    where
193        V: Visitor<'de>,
194    {
195        self.deserialize_str(visitor)
196    }
197
198    // The `Serializer` implementation on the previous page serialized byte
199    // arrays as JSON arrays of bytes. Handle that representation here.
200    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
201    where
202        V: Visitor<'de>,
203    {
204        visitor.visit_borrowed_bytes(self.parse_bytes()?)
205    }
206
207    fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value>
208    where
209        V: Visitor<'de>,
210    {
211        unimplemented!()
212    }
213
214    fn deserialize_option<V>(self, _visitor: V) -> Result<V::Value>
215    where
216        V: Visitor<'de>,
217    {
218        unimplemented!();
219    }
220
221    fn deserialize_unit<V>(self, _visitor: V) -> Result<V::Value>
222    where
223        V: Visitor<'de>,
224    {
225        unimplemented!();
226    }
227
228    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
229    where
230        V: Visitor<'de>,
231    {
232        self.deserialize_unit(visitor)
233    }
234
235    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
236    where
237        V: Visitor<'de>,
238    {
239        visitor.visit_newtype_struct(self)
240    }
241
242    fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value>
243    where
244        V: Visitor<'de>,
245    {
246        let res = rlp::decode_length(&self.input)?;
247        if res.expected_type == ExpectedType::ListType {
248            let nested = &self.input[res.offset..res.offset + res.length];
249            self.stack.push_front(&self.input);
250            self.input = nested;
251            let value = visitor.visit_seq(RlpListDecoder::new(&mut self))?;
252            self.input = self.stack.pop_front().unwrap();
253            self.input = &self.input[res.offset + res.length..];
254            Ok(value)
255        } else {
256            Err(Error::ExpectedList)
257        }
258    }
259
260    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
261    where
262        V: Visitor<'de>,
263    {
264        self.deserialize_seq(visitor)
265    }
266
267    fn deserialize_tuple_struct<V>(
268        self,
269        _name: &'static str,
270        _len: usize,
271        visitor: V,
272    ) -> Result<V::Value>
273    where
274        V: Visitor<'de>,
275    {
276        self.deserialize_seq(visitor)
277    }
278
279    fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value>
280    where
281        V: Visitor<'de>,
282    {
283        unimplemented!();
284    }
285
286    fn deserialize_struct<V>(
287        self,
288        _name: &'static str,
289        _fields: &'static [&'static str],
290        visitor: V,
291    ) -> Result<V::Value>
292    where
293        V: Visitor<'de>,
294    {
295        self.deserialize_map(visitor)
296    }
297
298    fn deserialize_enum<V>(
299        self,
300        _name: &'static str,
301        _variants: &'static [&'static str],
302        _visitor: V,
303    ) -> Result<V::Value>
304    where
305        V: Visitor<'de>,
306    {
307        unimplemented!();
308    }
309
310    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
311    where
312        V: Visitor<'de>,
313    {
314        self.deserialize_str(visitor)
315    }
316
317    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
318    where
319        V: Visitor<'de>,
320    {
321        self.deserialize_any(visitor)
322    }
323}
324
325// In order to handle commas correctly when deserializing a JSON array or map,
326// we need to track whether we are on the first element or past the first
327// element.
328struct RlpListDecoder<'a, 'de: 'a> {
329    de: &'a mut Deserializer<'de>,
330}
331
332impl<'a, 'de> RlpListDecoder<'a, 'de> {
333    fn new(de: &'a mut Deserializer<'de>) -> Self {
334        RlpListDecoder { de }
335    }
336}
337
338// `SeqAccess` is provided to the `Visitor` to give it the ability to iterate
339// through elements of the sequence.
340impl<'de, 'a> SeqAccess<'de> for RlpListDecoder<'a, 'de> {
341    type Error = Error;
342
343    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
344    where
345        T: DeserializeSeed<'de>,
346    {
347        if self.de.input.len() == 0 {
348            // No more elements
349            return Ok(None);
350        }
351        match rlp::decode_length(&self.de.input)?.expected_type {
352            ExpectedType::StringType => {
353                let result = seed.deserialize(&mut *self.de);
354                result.map(Some)
355            }
356            ExpectedType::ListType => {
357                // Here we don't consume boundaries of a list, and let the deserializer create new deserializer for this sequence.
358                let result = seed.deserialize(&mut *self.de);
359                result.map(Some)
360            }
361        }
362    }
363}
364
365////////////////////////////////////////////////////////////////////////////////
366
367#[test]
368fn deserialize_short_string() {
369    let foo: String = from_bytes(&[0x61u8]).unwrap();
370    assert_eq!(foo, "a");
371}
372
373#[test]
374fn deserialize_longer_string() {
375    let foo: String = from_bytes(&[0x83, 0x61, 0x62, 0x63]).unwrap();
376    assert_eq!(foo, "abc");
377}
378
379#[test]
380fn deserialize_short_array() {
381    let foo: Vec<String> =
382        from_bytes(&[0xc8, 0x83, 0x61, 0x62, 0x63, 0x83, 0x64, 0x65, 0x66]).unwrap();
383    assert_eq!(foo, vec!["abc", "def"]);
384}
385
386#[test]
387fn deserialize_short_array_into_tuple() {
388    let (abc, def): (String, String) =
389        from_bytes(&[0xc8, 0x83, 0x61, 0x62, 0x63, 0x83, 0x64, 0x65, 0x66]).unwrap();
390    assert_eq!(abc, "abc");
391    assert_eq!(def, "def");
392}
393
394#[test]
395fn deserialize_nested_sequence_of_string_seq() {
396    let foo: Vec<Vec<String>> =
397        from_bytes(&[0xc9, 0xc8, 0x83, 0x61, 0x62, 0x63, 0x83, 0x64, 0x65, 0x66]).unwrap();
398    assert_eq!(foo, vec![vec!["abc", "def"]]);
399}
400
401#[test]
402fn deserialize_set_representation_of_three() {
403    //
404    let foo = from_bytes(&[0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]);
405    assert_eq!(
406        foo,
407        Ok(vec![
408            vec![],
409            vec![vec![]],
410            vec![vec![], vec![Vec::<u8>::new()]],
411        ])
412    );
413}
414
415#[test]
416fn deserialize_three_levels() {
417    let foo: Vec<Vec<Vec<String>>> = from_bytes(&[
418        0xca, 0xc9, 0xc8, 0x83, 0x61, 0x62, 0x63, 0x83, 0x64, 0x65, 0x66,
419    ]).unwrap();
420    assert_eq!(foo, [[["abc", "def"]]]);
421}
422
423#[test]
424#[should_panic]
425fn simple_invalid() {
426    let _foo: String = from_bytes(&[0x83, 0x61, 0x62, 0x63, /* excess */ 0xff]).unwrap();
427}
428
429fn get_bytes(b: &str) -> Option<Vec<u8>> {
430    b.as_bytes()
431        .chunks(2)
432        .map(|ch| {
433            str::from_utf8(&ch)
434                .ok()
435                .and_then(|res| u8::from_str_radix(&res, 16).ok())
436        }).collect()
437}
438
439#[test]
440fn invalid_complex() {
441    use serde_bytes::Bytes;
442    let data = get_bytes("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c00804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3").unwrap();
443
444    assert_eq!(
445        from_bytes::<Vec<Bytes>>(&data).unwrap_err(),
446        Error::WrongPrefix
447    );
448}
449
450#[test]
451fn lorem_ipsum() {
452    let data: String = from_bytes(&vec![
453        0xb8, 0x38, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64,
454        0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c, 0x20,
455        0x63, 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69,
456        0x70, 0x69, 0x73, 0x69, 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74,
457    ]).unwrap();
458    assert_eq!(
459        data,
460        "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
461    );
462}
463
464#[test]
465fn unsigned_eth_transaction() {
466    use serde_bytes::Bytes;
467    let data = get_bytes("f83f8085e8d4a510008227108080af6025515b525b600a37f260003556601b596020356000355760015b525b54602052f260255860005b525b54602052f2808080").unwrap();
468    let decoded: Vec<Bytes> = from_bytes(&data).unwrap();
469
470    assert_eq!(
471        decoded,
472        vec![
473            Bytes::new(&[]),
474            Bytes::new(&[232, 212, 165, 16, 0]),
475            Bytes::new(&[39, 16]),
476            Bytes::new(&[]),
477            Bytes::new(&[]),
478            Bytes::new(&[
479                96, 37, 81, 91, 82, 91, 96, 10, 55, 242, 96, 0, 53, 86, 96, 27, 89, 96, 32, 53, 96,
480                0, 53, 87, 96, 1, 91, 82, 91, 84, 96, 32, 82, 242, 96, 37, 88, 96, 0, 91, 82, 91,
481                84, 96, 32, 82, 242,
482            ]),
483            Bytes::new(&[]),
484            Bytes::new(&[]),
485            Bytes::new(&[]),
486        ]
487    );
488}