sol_abi/
arg.rs

1//! Arg of solidity ABI.
2
3use core::{convert::Infallible, 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 32-bit integer.
25    Int32,
26    /// A 64-bit integer.
27    Int64,
28    /// A 32-bit unsigned integer.
29    UInt32,
30    /// A 64-bit unsigned integer.
31    UInt64,
32    /// An unknown type.
33    #[default]
34    Unknown,
35}
36
37impl From<&str> for Param {
38    fn from(s: &str) -> Self {
39        match s {
40            "i32" | "int32" => Param::Int32,
41            "i64" | "int64" => Param::Int64,
42            "u32" | "uint32" => Param::UInt32,
43            "usize" | "u64" | "uint64" => Param::UInt64,
44            _ => Param::Unknown,
45        }
46    }
47}
48
49impl FromStr for Param {
50    type Err = Infallible;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Ok(Self::from(s))
54    }
55}
56
57impl AsRef<str> for Param {
58    fn as_ref(&self) -> &str {
59        match self {
60            Param::Int32 => "int32",
61            Param::Int64 => "int64",
62            Param::UInt32 => "uint32",
63            Param::UInt64 => "uint64",
64            Param::Unknown => "unknown",
65        }
66    }
67}
68
69impl ToString for Param {
70    fn to_string(&self) -> String {
71        self.as_ref().to_string()
72    }
73}
74
75#[cfg(feature = "syn")]
76impl From<&Box<syn::Type>> for Param {
77    fn from(ty: &Box<syn::Type>) -> Self {
78        use quote::ToTokens;
79
80        let ident = ty.into_token_stream().to_string();
81        Self::from(ident.as_str())
82    }
83}