1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use super::Writer;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum ParamType {
Address,
Bytes,
Int(usize),
Uint(usize),
Bool,
String,
Array(Box<ParamType>),
FixedBytes(usize),
FixedArray(Box<ParamType>, usize),
Tuple(Vec<ParamType>),
}
impl fmt::Display for ParamType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Writer::write(self))
}
}
impl ParamType {
pub fn is_empty_bytes_valid_encoding(&self) -> bool {
match self {
ParamType::FixedBytes(len) => *len == 0,
ParamType::FixedArray(_, len) => *len == 0,
_ => false,
}
}
pub fn is_dynamic(&self) -> bool {
match self {
ParamType::Bytes | ParamType::String | ParamType::Array(_) => true,
ParamType::FixedArray(elem_type, _) => elem_type.is_dynamic(),
ParamType::Tuple(params) => params.iter().any(|param| param.is_dynamic()),
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use crate::ParamType;
#[test]
fn test_param_type_display() {
assert_eq!(format!("{}", ParamType::Address), "address".to_owned());
assert_eq!(format!("{}", ParamType::Bytes), "bytes".to_owned());
assert_eq!(format!("{}", ParamType::FixedBytes(32)), "bytes32".to_owned());
assert_eq!(format!("{}", ParamType::Uint(256)), "uint256".to_owned());
assert_eq!(format!("{}", ParamType::Int(64)), "int64".to_owned());
assert_eq!(format!("{}", ParamType::Bool), "bool".to_owned());
assert_eq!(format!("{}", ParamType::String), "string".to_owned());
assert_eq!(format!("{}", ParamType::Array(Box::new(ParamType::Bool))), "bool[]".to_owned());
assert_eq!(format!("{}", ParamType::FixedArray(Box::new(ParamType::Uint(256)), 2)), "uint256[2]".to_owned());
assert_eq!(format!("{}", ParamType::FixedArray(Box::new(ParamType::String), 2)), "string[2]".to_owned());
assert_eq!(
format!("{}", ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 2)),
"bool[][2]".to_owned()
);
}
#[test]
fn test_is_dynamic() {
assert_eq!(ParamType::Address.is_dynamic(), false);
assert_eq!(ParamType::Bytes.is_dynamic(), true);
assert_eq!(ParamType::FixedBytes(32).is_dynamic(), false);
assert_eq!(ParamType::Uint(256).is_dynamic(), false);
assert_eq!(ParamType::Int(64).is_dynamic(), false);
assert_eq!(ParamType::Bool.is_dynamic(), false);
assert_eq!(ParamType::String.is_dynamic(), true);
assert_eq!(ParamType::Array(Box::new(ParamType::Bool)).is_dynamic(), true);
assert_eq!(ParamType::FixedArray(Box::new(ParamType::Uint(256)), 2).is_dynamic(), false);
assert_eq!(ParamType::FixedArray(Box::new(ParamType::String), 2).is_dynamic(), true);
assert_eq!(ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 2).is_dynamic(), true);
}
}