• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use serde::de::{Deserializer, Error, SeqAccess, Visitor};
2 use std::fmt;
3 
deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> where D: Deserializer<'de>,4 pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
5 where
6     D: Deserializer<'de>,
7 {
8     deserializer.deserialize_byte_buf(ByteBufVisitor)
9 }
10 
11 struct ByteBufVisitor;
12 
13 impl<'de> Visitor<'de> for ByteBufVisitor {
14     type Value = Vec<u8>;
15 
expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result16     fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
17         formatter.write_str("byte array")
18     }
19 
visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: SeqAccess<'de>,20     fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
21     where
22         V: SeqAccess<'de>,
23     {
24         let mut values = Vec::new();
25         while let Some(value) = visitor.next_element()? {
26             values.push(value);
27         }
28         Ok(values)
29     }
30 
visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: Error,31     fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
32     where
33         E: Error,
34     {
35         Ok(v.to_vec())
36     }
37 
visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> where E: Error,38     fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
39     where
40         E: Error,
41     {
42         Ok(v)
43     }
44 
visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: Error,45     fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
46     where
47         E: Error,
48     {
49         Ok(v.as_bytes().to_vec())
50     }
51 
visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: Error,52     fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
53     where
54         E: Error,
55     {
56         Ok(v.into_bytes())
57     }
58 }
59