1use core::ops::{Deref, DerefMut};
6
7#[derive(Clone, Debug, Default)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct Abi(sol_abi::Abi);
11
12impl Deref for Abi {
13 type Target = sol_abi::Abi;
14
15 fn deref(&self) -> &Self::Target {
16 &self.0
17 }
18}
19
20impl DerefMut for Abi {
21 fn deref_mut(&mut self) -> &mut Self::Target {
22 &mut self.0
23 }
24}
25
26#[cfg(feature = "bytes")]
27impl Abi {
28 pub fn to_bytes(&self) -> postcard::Result<Vec<u8>> {
30 postcard::to_stdvec(self)
31 }
32
33 pub fn from_bytes(bytes: impl AsRef<[u8]>) -> postcard::Result<Self> {
35 postcard::from_bytes(bytes.as_ref())
36 }
37}
38
39#[cfg(feature = "hex")]
40mod hex_impl {
41 use crate::{result::Result, Abi};
42 use core::fmt;
43
44 impl Abi {
45 pub fn to_hex(&self) -> Result<String> {
47 Ok("0x".to_string() + &hex::encode(self.to_bytes()?))
48 }
49
50 pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> {
52 Self::from_bytes(hex::decode(hex.as_ref().trim_start_matches("0x"))?)
53 .map_err(Into::into)
54 }
55 }
56
57 impl fmt::Display for Abi {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 write!(f, "{}", self.to_hex().unwrap_or_default())
60 }
61 }
62
63 impl core::str::FromStr for Abi {
64 type Err = crate::result::Error;
65
66 fn from_str(hex: &str) -> Result<Self> {
67 Self::from_hex(hex)
68 }
69 }
70}
71
72#[cfg(feature = "syn")]
73impl From<&syn::Signature> for Abi {
74 fn from(sig: &syn::Signature) -> Self {
75 Self(sol_abi::Abi::from(sig))
76 }
77}