1use core::{convert::Infallible, fmt, str::FromStr};
4
5#[cfg(not(feature = "std"))]
6use crate::std::{String, ToString};
7
8#[derive(Clone, Debug, Default)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Arg {
12 pub name: String,
14 #[cfg_attr(feature = "serde", serde(rename = "type"))]
16 pub ty: Param,
17}
18
19#[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 Int8,
26 Int16,
28 Int32,
30 Int64,
32 UInt8,
34 UInt16,
36 UInt32,
38 UInt64,
40 UInt256,
42 Bool,
46 Address,
48 #[default]
50 Bytes,
51 String,
53 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}