sol_abi/
arg.rs

1//! Arg of solidity ABI.
2
3use core::{convert::Infallible, fmt, str::FromStr};
4
5#[cfg(not(feature = "std"))]
6use crate::std::{String, ToString};
7
8/// Arg of solidity ABI.
9#[derive(Clone, Debug, Default)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Arg {
12    /// Name of the input.
13    pub name: String,
14    /// Type of the input.
15    #[cfg_attr(feature = "serde", serde(rename = "type"))]
16    pub ty: Param,
17}
18
19/// The canonical type of the parameter.
20#[derive(Clone, Debug, Default)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
23pub enum Param {
24    /// A 8-bit integer.
25    Int8,
26    /// A 16-bit integer.
27    Int16,
28    /// A 32-bit integer.
29    Int32,
30    /// A 64-bit integer.
31    Int64,
32    /// A 8-bit unsigned integer.
33    UInt8,
34    /// A 16-bit unsigned integer.
35    UInt16,
36    /// A 32-bit unsigned integer.
37    UInt32,
38    /// A 64-bit unsigned integer.
39    UInt64,
40    /// A 256-bit unsigned integer.
41    UInt256,
42    // /// A 256-bit unsigned integer.
43    // UInt256,
44    /// A boolean type.
45    Bool,
46    /// An EVM address.
47    Address,
48    /// A byte array.
49    #[default]
50    Bytes,
51    /// A string type.
52    String,
53    /// An unknown type.
54    Unknown(String),
55}
56
57impl From<&str> for Param {
58    fn from(s: &str) -> Self {
59        match s {
60            "i8" | "int8" => Param::Int8,
61            "u8" | "uint8" => Param::UInt8,
62            "i32" | "int32" => Param::Int32,
63            "i64" | "int64" => Param::Int64,
64            "u16" | "uint16" => Param::UInt16,
65            "u32" | "uint32" => Param::UInt32,
66            "u64" | "uint64" => Param::UInt64,
67            "U256" | "u256" | "uint256" => Param::UInt256,
68            "bool" => Param::Bool,
69            "address" | "Address" => Param::Address,
70            "String" | "String32" => Param::String,
71            "Vec<u8>" => Param::Bytes,
72            bytes if bytes.starts_with("Bytes") => Param::Bytes,
73            _ => Param::Unknown(s.to_string()),
74        }
75    }
76}
77
78impl FromStr for Param {
79    type Err = Infallible;
80
81    fn from_str(s: &str) -> Result<Self, Self::Err> {
82        Ok(Self::from(s))
83    }
84}
85
86impl AsRef<str> for Param {
87    fn as_ref(&self) -> &str {
88        match self {
89            Param::Int8 => "int8",
90            Param::Int16 => "int16",
91            Param::Int32 => "int32",
92            Param::Int64 => "int64",
93            Param::UInt8 => "uint8",
94            Param::UInt16 => "uint16",
95            Param::UInt32 => "uint32",
96            Param::UInt64 => "uint64",
97            Param::UInt256 => "uint256",
98            Param::Address => "address",
99            Param::Bool => "boolean",
100            Param::Bytes => "bytes",
101            Param::String => "string",
102            Param::Unknown(ty) => ty.as_ref(),
103        }
104    }
105}
106
107impl fmt::Display for Param {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        let p: &str = self.as_ref();
110        write!(f, "{p}")
111    }
112}
113
114#[cfg(feature = "syn")]
115impl From<&Box<syn::Type>> for Param {
116    fn from(ty: &Box<syn::Type>) -> Self {
117        use quote::ToTokens;
118
119        let ident = ty.into_token_stream().to_string();
120        Self::from(ident.as_str())
121    }
122}