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