git.delta.rocks / unique-network / refs/commits / 69b68ba15862

difftreelog

source

crates/evm-coder/src/solidity/impls.rs4.6 KiBsourcehistory
1use super::{TypeCollector, SolidityTypeName, SolidityTupleType};2use crate::{sealed, types::*};3use core::fmt;45macro_rules! solidity_type_name {6    ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {7        $(8            impl SolidityTypeName for $ty {9                fn solidity_name(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {10                    write!(writer, $name)11                }12				fn is_simple() -> bool {13					$simple14				}15				fn solidity_default(writer: &mut impl core::fmt::Write, _tc: &TypeCollector) -> core::fmt::Result {16					write!(writer, $default)17				}18            }19        )*20    };21}2223solidity_type_name! {24	uint8 => "uint8" true = "0",25	uint32 => "uint32" true = "0",26	uint64 => "uint64" true = "0",27	uint128 => "uint128" true = "0",28	uint256 => "uint256" true = "0",29	bytes4 => "bytes4" true = "bytes4(0)",30	address => "address" true = "0x0000000000000000000000000000000000000000",31	string => "string" false = "\"\"",32	bytes => "bytes" false = "hex\"\"",33	bool => "bool" true = "false",34}3536impl SolidityTypeName for void {37	fn solidity_name(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {38		Ok(())39	}40	fn is_simple() -> bool {41		true42	}43	fn solidity_default(_writer: &mut impl fmt::Write, _tc: &TypeCollector) -> fmt::Result {44		Ok(())45	}46	fn is_void() -> bool {47		true48	}49}5051impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {52	fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {53		T::solidity_name(writer, tc)?;54		write!(writer, "[]")55	}56	fn is_simple() -> bool {57		false58	}59	fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {60		write!(writer, "new ")?;61		T::solidity_name(writer, tc)?;62		write!(writer, "[](0)")63	}64}6566macro_rules! count {67    () => (0usize);68    ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));69}7071macro_rules! impl_tuples {72	($($ident:ident)+) => {73		impl<$($ident: SolidityTypeName + 'static),+> SolidityTupleType for ($($ident,)+) {74			fn names(tc: &TypeCollector) -> Vec<string> {75				let mut collected = Vec::with_capacity(Self::len());76				$({77					let mut out = string::new();78					$ident::solidity_name(&mut out, tc).expect("no fmt error");79					collected.push(out);80				})*;81				collected82			}8384			fn len() -> usize {85				count!($($ident)*)86			}87		}88		impl<$($ident: SolidityTypeName + 'static),+> SolidityTypeName for ($($ident,)+) {89			fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {90				write!(writer, "{}", tc.collect_tuple::<Self>())91			}92			fn is_simple() -> bool {93				false94			}95			#[allow(unused_assignments)]96			fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {97				write!(writer, "{}(", tc.collect_tuple::<Self>())?;98				let mut first = true;99				$(100					if !first {101						write!(writer, ",")?;102					} else {103						first = false;104					}105					<$ident>::solidity_default(writer, tc)?;106				)*107				write!(writer, ")")108			}109		}110	};111}112113impl_tuples! {A}114impl_tuples! {A B}115impl_tuples! {A B C}116impl_tuples! {A B C D}117impl_tuples! {A B C D E}118impl_tuples! {A B C D E F}119impl_tuples! {A B C D E F G}120impl_tuples! {A B C D E F G H}121impl_tuples! {A B C D E F G H I}122impl_tuples! {A B C D E F G H I J}123124impl sealed::CanBePlacedInVec for Property {}125impl StructCollect for Property {126	fn name() -> String {127		"Property".into()128	}129130	fn declaration() -> String {131		let mut str = String::new();132		writeln!(str, "/// @dev Property struct").unwrap();133		writeln!(str, "struct {} {{", Self::name()).unwrap();134		writeln!(str, "\tstring key;").unwrap();135		writeln!(str, "\tbytes value;").unwrap();136		writeln!(str, "}}").unwrap();137		str138	}139}140141impl SolidityTypeName for Property {142	fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {143		write!(writer, "{}", tc.collect_struct::<Self>())144	}145146	fn is_simple() -> bool {147		false148	}149150	fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {151		write!(writer, "{}(", tc.collect_struct::<Self>())?;152		address::solidity_default(writer, tc)?;153		write!(writer, ",")?;154		uint256::solidity_default(writer, tc)?;155		write!(writer, ")")156	}157}158159impl SolidityTupleType for Property {160	fn names(tc: &TypeCollector) -> Vec<string> {161		let mut collected = Vec::with_capacity(Self::len());162		{163			let mut out = string::new();164			string::solidity_name(&mut out, tc).expect("no fmt error");165			collected.push(out);166		}167		{168			let mut out = string::new();169			bytes::solidity_name(&mut out, tc).expect("no fmt error");170			collected.push(out);171		}172		collected173	}174175	fn len() -> usize {176		2177	}178}