ethabi_next/param_type/
writer.rs1use crate::ParamType;
10
11pub struct Writer;
13
14impl Writer {
15 pub fn write(param: &ParamType) -> String {
17 match *param {
18 ParamType::Address => "address".to_owned(),
19 ParamType::Bytes => "bytes".to_owned(),
20 ParamType::FixedBytes(len) => format!("bytes{}", len),
21 ParamType::Int(len) => format!("int{}", len),
22 ParamType::Uint(len) => format!("uint{}", len),
23 ParamType::Bool => "bool".to_owned(),
24 ParamType::String => "string".to_owned(),
25 ParamType::FixedArray(ref param, len) => format!("{}[{}]", Writer::write(param), len),
26 ParamType::Array(ref param) => format!("{}[]", Writer::write(param)),
27 ParamType::Tuple(ref params) => {
28 format!("({})", params.iter().map(|ref t| format!("{}", t)).collect::<Vec<String>>().join(","))
29 }
30 }
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::Writer;
37 use crate::ParamType;
38
39 #[test]
40 fn test_write_param() {
41 assert_eq!(Writer::write(&ParamType::Address), "address".to_owned());
42 assert_eq!(Writer::write(&ParamType::Bytes), "bytes".to_owned());
43 assert_eq!(Writer::write(&ParamType::FixedBytes(32)), "bytes32".to_owned());
44 assert_eq!(Writer::write(&ParamType::Uint(256)), "uint256".to_owned());
45 assert_eq!(Writer::write(&ParamType::Int(64)), "int64".to_owned());
46 assert_eq!(Writer::write(&ParamType::Bool), "bool".to_owned());
47 assert_eq!(Writer::write(&ParamType::String), "string".to_owned());
48 assert_eq!(Writer::write(&ParamType::Array(Box::new(ParamType::Bool))), "bool[]".to_owned());
49 assert_eq!(Writer::write(&ParamType::FixedArray(Box::new(ParamType::String), 2)), "string[2]".to_owned());
50 assert_eq!(
51 Writer::write(&ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 2)),
52 "bool[][2]".to_owned()
53 );
54 assert_eq!(
55 Writer::write(&ParamType::Array(Box::new(ParamType::Tuple(vec![
56 ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Int(256), ParamType::Uint(256)]))),
57 ParamType::FixedBytes(32),
58 ])))),
59 "((int256,uint256)[],bytes32)[]".to_owned()
60 );
61 }
62}