ethers_abi/param_type/
deserialize.rs1use super::{ParamType, Reader};
9#[cfg(not(feature = "std"))]
10use crate::no_std_prelude::*;
11use core::fmt;
12use serde::{
13 de::{Error as SerdeError, Visitor},
14 Deserialize, Deserializer,
15};
16
17impl<'a> Deserialize<'a> for ParamType {
18 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
19 where
20 D: Deserializer<'a>,
21 {
22 deserializer.deserialize_identifier(ParamTypeVisitor)
23 }
24}
25
26struct ParamTypeVisitor;
27
28impl<'a> Visitor<'a> for ParamTypeVisitor {
29 type Value = ParamType;
30
31 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
32 write!(formatter, "a correct name of abi-encodable parameter type")
33 }
34
35 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
36 where
37 E: SerdeError,
38 {
39 Reader::read(value).map_err(|e| SerdeError::custom(format!("{e:?}").as_str()))
40 }
41
42 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
43 where
44 E: SerdeError,
45 {
46 self.visit_str(value.as_str())
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 #[cfg(not(feature = "std"))]
53 use crate::no_std_prelude::*;
54 use crate::ParamType;
55
56 #[test]
57 fn param_type_deserialization() {
58 let s = r#"["address", "bytes", "bytes32", "bool", "string", "int", "uint", "address[]", "uint[3]", "bool[][5]", "tuple[]"]"#;
59 let deserialized: Vec<ParamType> = serde_json::from_str(s).unwrap();
60 assert_eq!(
61 deserialized,
62 vec![
63 ParamType::Address,
64 ParamType::Bytes,
65 ParamType::FixedBytes(32),
66 ParamType::Bool,
67 ParamType::String,
68 ParamType::Int(256),
69 ParamType::Uint(256),
70 ParamType::Array(Box::new(ParamType::Address)),
71 ParamType::FixedArray(Box::new(ParamType::Uint(256)), 3),
72 ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 5),
73 ParamType::Array(Box::new(ParamType::Tuple(vec![]))),
74 ]
75 );
76 }
77}