stratus/eth/primitives/
bytes.rs

1use std::fmt::Display;
2use std::ops::Deref;
3use std::ops::DerefMut;
4
5use display_json::DebugAsJson;
6
7use crate::alias::RevmBytes;
8use crate::alias::RevmOutput;
9
10#[derive(DebugAsJson, Clone, Default, Eq, PartialEq, fake::Dummy)]
11pub struct Bytes(pub Vec<u8>);
12
13impl Display for Bytes {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        if self.len() <= 256 {
16            write!(f, "{}", const_hex::encode_prefixed(&self.0))
17        } else {
18            write!(f, "too long")
19        }
20    }
21}
22
23// -----------------------------------------------------------------------------
24// Serialization / Deserialization
25// -----------------------------------------------------------------------------
26impl serde::Serialize for Bytes {
27    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
28    where
29        S: serde::Serializer,
30    {
31        serializer.serialize_str(&const_hex::encode_prefixed(&self.0))
32    }
33}
34
35impl<'de> serde::Deserialize<'de> for Bytes {
36    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
37    where
38        D: serde::Deserializer<'de>,
39    {
40        let value = String::deserialize(deserializer)?;
41        match const_hex::decode(value) {
42            Ok(value) => Ok(Self(value)),
43            Err(e) => {
44                tracing::warn!(reason = ?e, "failed to parse hex bytes");
45                Err(serde::de::Error::custom(e))
46            }
47        }
48    }
49}
50
51// -----------------------------------------------------------------------------
52// Conversions: Other -> Self
53// -----------------------------------------------------------------------------
54
55impl From<Vec<u8>> for Bytes {
56    fn from(value: Vec<u8>) -> Self {
57        Self(value)
58    }
59}
60
61impl From<&[u8]> for Bytes {
62    fn from(value: &[u8]) -> Self {
63        Self(value.to_vec())
64    }
65}
66
67impl From<[u8; 32]> for Bytes {
68    fn from(value: [u8; 32]) -> Self {
69        Self(value.to_vec())
70    }
71}
72
73impl From<RevmBytes> for Bytes {
74    fn from(value: RevmBytes) -> Self {
75        Self(value.0.into())
76    }
77}
78
79impl From<RevmOutput> for Bytes {
80    fn from(value: RevmOutput) -> Self {
81        match value {
82            RevmOutput::Call(bytes) => Self(bytes.0.into()),
83            RevmOutput::Create(bytes, _) => Self(bytes.0.into()),
84        }
85    }
86}
87
88// -----------------------------------------------------------------------------
89// Conversions: Self -> Other
90// -----------------------------------------------------------------------------
91impl AsRef<[u8]> for Bytes {
92    fn as_ref(&self) -> &[u8] {
93        &self.0
94    }
95}
96
97impl Deref for Bytes {
98    type Target = Vec<u8>;
99
100    fn deref(&self) -> &Self::Target {
101        &self.0
102    }
103}
104
105impl DerefMut for Bytes {
106    fn deref_mut(&mut self) -> &mut Self::Target {
107        &mut self.0
108    }
109}
110
111impl From<Bytes> for RevmBytes {
112    fn from(value: Bytes) -> Self {
113        value.0.into()
114    }
115}