sol_abi/
arg.rs

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Arg of solidity ABI.

use core::{convert::Infallible, fmt, str::FromStr};

#[cfg(not(feature = "std"))]
use crate::std::{String, ToString};

/// Arg of solidity ABI.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Arg {
    /// Name of the input.
    pub name: String,
    /// Type of the input.
    #[cfg_attr(feature = "serde", serde(rename = "type"))]
    pub ty: Param,
}

/// The canonical type of the parameter.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Param {
    /// A 8-bit integer.
    Int8,
    /// A 16-bit integer.
    Int16,
    /// A 32-bit integer.
    Int32,
    /// A 64-bit integer.
    Int64,
    /// A 8-bit unsigned integer.
    UInt8,
    /// A 16-bit unsigned integer.
    UInt16,
    /// A 32-bit unsigned integer.
    UInt32,
    /// A 64-bit unsigned integer.
    UInt64,
    /// A 256-bit unsigned integer.
    UInt256,
    // /// A 256-bit unsigned integer.
    // UInt256,
    /// A boolean type.
    Bool,
    /// An EVM address.
    Address,
    /// A byte array.
    #[default]
    Bytes,
    /// A string type.
    String,
    /// An unknown type.
    Unknown(String),
}

impl From<&str> for Param {
    fn from(s: &str) -> Self {
        match s {
            "i8" | "int8" => Param::Int8,
            "u8" | "uint8" => Param::UInt8,
            "i32" | "int32" => Param::Int32,
            "i64" | "int64" => Param::Int64,
            "u16" | "uint16" => Param::UInt16,
            "u32" | "uint32" => Param::UInt32,
            "u64" | "uint64" => Param::UInt64,
            "U256" | "u256" | "uint256" => Param::UInt256,
            "bool" => Param::Bool,
            "address" | "Address" => Param::Address,
            "String" | "String32" => Param::String,
            "Vec<u8>" => Param::Bytes,
            bytes if bytes.starts_with("Bytes") => Param::Bytes,
            _ => Param::Unknown(s.to_string()),
        }
    }
}

impl FromStr for Param {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(s))
    }
}

impl AsRef<str> for Param {
    fn as_ref(&self) -> &str {
        match self {
            Param::Int8 => "int8",
            Param::Int16 => "int16",
            Param::Int32 => "int32",
            Param::Int64 => "int64",
            Param::UInt8 => "uint8",
            Param::UInt16 => "uint16",
            Param::UInt32 => "uint32",
            Param::UInt64 => "uint64",
            Param::UInt256 => "uint256",
            Param::Address => "address",
            Param::Bool => "boolean",
            Param::Bytes => "bytes",
            Param::String => "string",
            Param::Unknown(ty) => ty.as_ref(),
        }
    }
}

impl fmt::Display for Param {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let p: &str = self.as_ref();
        write!(f, "{p}")
    }
}

#[cfg(feature = "syn")]
impl From<&Box<syn::Type>> for Param {
    fn from(ty: &Box<syn::Type>) -> Self {
        use quote::ToTokens;

        let ident = ty.into_token_stream().to_string();
        Self::from(ident.as_str())
    }
}