#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::ParamType;
pub struct Writer;
impl Writer {
pub fn write(param: &ParamType) -> String {
Writer::write_for_abi(param, true)
}
pub fn write_for_abi(param: &ParamType, serialize_tuple_contents: bool) -> String {
match *param {
ParamType::Address => "address".to_owned(),
ParamType::Bytes => "bytes".to_owned(),
ParamType::FixedBytes(len) => format!("bytes{len}"),
ParamType::Int(len) => format!("int{len}"),
ParamType::Uint(len) => format!("uint{len}"),
ParamType::Bool => "bool".to_owned(),
ParamType::String => "string".to_owned(),
ParamType::FixedArray(ref param, len) => {
format!("{}[{len}]", Writer::write_for_abi(param, serialize_tuple_contents))
}
ParamType::Array(ref param) => {
format!("{}[]", Writer::write_for_abi(param, serialize_tuple_contents))
}
ParamType::Tuple(ref params) => {
if serialize_tuple_contents {
let formatted = params
.iter()
.map(|t| Writer::write_for_abi(t, serialize_tuple_contents))
.collect::<Vec<String>>()
.join(",");
format!("({formatted})")
} else {
"tuple".to_owned()
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::Writer;
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::ParamType;
#[test]
fn test_write_param() {
assert_eq!(Writer::write(&ParamType::Address), "address".to_owned());
assert_eq!(Writer::write(&ParamType::Bytes), "bytes".to_owned());
assert_eq!(Writer::write(&ParamType::FixedBytes(32)), "bytes32".to_owned());
assert_eq!(Writer::write(&ParamType::Uint(256)), "uint256".to_owned());
assert_eq!(Writer::write(&ParamType::Int(64)), "int64".to_owned());
assert_eq!(Writer::write(&ParamType::Bool), "bool".to_owned());
assert_eq!(Writer::write(&ParamType::String), "string".to_owned());
assert_eq!(Writer::write(&ParamType::Array(Box::new(ParamType::Bool))), "bool[]".to_owned());
assert_eq!(Writer::write(&ParamType::FixedArray(Box::new(ParamType::String), 2)), "string[2]".to_owned());
assert_eq!(
Writer::write(&ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 2)),
"bool[][2]".to_owned()
);
assert_eq!(
Writer::write(&ParamType::Array(Box::new(ParamType::Tuple(vec![
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Int(256), ParamType::Uint(256)]))),
ParamType::FixedBytes(32),
])))),
"((int256,uint256)[],bytes32)[]".to_owned()
);
assert_eq!(
Writer::write_for_abi(
&ParamType::Array(Box::new(ParamType::Tuple(vec![
ParamType::Array(Box::new(ParamType::Int(256))),
ParamType::FixedBytes(32),
]))),
false
),
"tuple[]".to_owned()
);
}
}