1use super::{TypeCollector, SolidityTypeName, SolidityTupleType, sealed};2use crate::types::*;3use core::fmt;45impl sealed::CanBePlacedInVec for uint256 {}6impl sealed::CanBePlacedInVec for string {}7impl sealed::CanBePlacedInVec for address {}89macro_rules! solidity_type_name {10 ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {11 $(12 impl SolidityTypeName for $ty {13 fn solidity_name(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {14 write!(writer, $name)15 }16 fn is_simple() -> bool {17 $simple18 }19 fn solidity_default(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {20 write!(writer, $default)21 }22 }23 )*24 };25}2627solidity_type_name! {28 uint8 => "uint8" true = "0",29 uint32 => "uint32" true = "0",30 uint64 => "uint64" true = "0",31 uint128 => "uint128" true = "0",32 uint256 => "uint256" true = "0",33 bytes4 => "bytes4" true = "bytes4(0)",34 address => "address" true = "0x0000000000000000000000000000000000000000",35 string => "string" false = "\"\"",36 bytes => "bytes" false = "hex\"\"",37 bool => "bool" true = "false",38}3940impl SolidityTypeName for void {41 fn solidity_name(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {42 Ok(())43 }44 fn is_simple() -> bool {45 true46 }47 fn solidity_default(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {48 Ok(())49 }50 fn is_void() -> bool {51 true52 }53}5455impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {56 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {57 T::solidity_name(writer, tc)?;58 write!(writer, "[]")59 }60 fn is_simple() -> bool {61 false62 }63 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {64 write!(writer, "new ")?;65 T::solidity_name(writer, tc)?;66 write!(writer, "[](0)")67 }68}6970macro_rules! count {71 () => (0usize);72 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));73}7475macro_rules! impl_tuples {76 ($($ident:ident)+) => {77 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}78 impl<$($ident: SolidityTypeName + 'static),+> SolidityTupleType for ($($ident,)+) {79 fn names(tc: &TypeCollector) -> Vec<string> {80 let mut collected = Vec::with_capacity(Self::len());81 $({82 let mut out = string::new();83 $ident::solidity_name(&mut out, tc).expect("no fmt error");84 collected.push(out);85 })*;86 collected87 }8889 fn len() -> usize {90 count!($($ident)*)91 }92 }93 impl<$($ident: SolidityTypeName + 'static),+> SolidityTypeName for ($($ident,)+) {94 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {95 write!(writer, "{}", tc.collect_tuple::<Self>())96 }97 fn is_simple() -> bool {98 false99 }100 #[allow(unused_assignments)]101 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {102 write!(writer, "{}(", tc.collect_tuple::<Self>())?;103 let mut first = true;104 $(105 if !first {106 write!(writer, ",")?;107 } else {108 first = false;109 }110 <$ident>::solidity_default(writer, tc)?;111 )*112 write!(writer, ")")113 }114 }115 };116}117118impl_tuples! {A}119impl_tuples! {A B}120impl_tuples! {A B C}121impl_tuples! {A B C D}122impl_tuples! {A B C D E}123impl_tuples! {A B C D E F}124impl_tuples! {A B C D E F G}125impl_tuples! {A B C D E F G H}126impl_tuples! {A B C D E F G H I}127impl_tuples! {A B C D E F G H I J}128129impl sealed::CanBePlacedInVec for Property {}130impl StructCollect for Property {131 fn name() -> String {132 "Property".into()133 }134135 fn declaration() -> String {136 let mut str = String::new();137 writeln!(str, "/// @dev Property struct").unwrap();138 writeln!(str, "struct {} {{", Self::name()).unwrap();139 writeln!(str, "\tstring key;").unwrap();140 writeln!(str, "\tbytes value;").unwrap();141 writeln!(str, "}}").unwrap();142 str143 }144}145146impl SolidityTypeName for Property {147 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {148 write!(writer, "{}", tc.collect_struct::<Self>())149 }150151 fn is_simple() -> bool {152 false153 }154155 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {156 write!(writer, "{}(", tc.collect_struct::<Self>())?;157 address::solidity_default(writer, tc)?;158 write!(writer, ",")?;159 uint256::solidity_default(writer, tc)?;160 write!(writer, ")")161 }162}163164impl SolidityTupleType for Property {165 fn names(tc: &TypeCollector) -> Vec<string> {166 let mut collected = Vec::with_capacity(Self::len());167 {168 let mut out = string::new();169 string::solidity_name(&mut out, tc).expect("no fmt error");170 collected.push(out);171 }172 {173 let mut out = string::new();174 bytes::solidity_name(&mut out, tc).expect("no fmt error");175 collected.push(out);176 }177 collected178 }179180 fn len() -> usize {181 2182 }183}