1macro_rules! impl_u8_discriminant {
6 ($ty:ty, $error:ident) => {
7 impl From<$ty> for u8 {
8 fn from(value: $ty) -> Self {
9 value as Self
10 }
11 }
12
13 impl TryFrom<u8> for $ty {
14 type Error = crate::Eip8141Error;
15
16 fn try_from(value: u8) -> Result<Self, Self::Error> {
17 Self::try_from_u8(value).ok_or(crate::Eip8141Error::$error(value))
18 }
19 }
20
21 #[cfg(feature = "serde")]
22 impl serde::Serialize for $ty {
23 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
24 crate::serde_utils::quantity_u8::serialize(&u8::from(*self), serializer)
25 }
26 }
27
28 #[cfg(feature = "serde")]
29 impl<'de> serde::Deserialize<'de> for $ty {
30 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
31 let value = crate::serde_utils::quantity_u8::deserialize(deserializer)?;
32 Self::try_from(value).map_err(serde::de::Error::custom)
33 }
34 }
35 };
36 ($ty:ty, $error:ident, $rlp_error:literal) => {
37 impl_u8_discriminant!($ty, $error);
38
39 impl alloy_rlp::Encodable for $ty {
40 fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
41 alloy_rlp::Encodable::encode(&u8::from(*self), out);
42 }
43
44 fn length(&self) -> usize {
45 alloy_rlp::Encodable::length(&u8::from(*self))
46 }
47 }
48
49 impl alloy_rlp::Decodable for $ty {
50 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
51 Self::try_from_u8(<u8 as alloy_rlp::Decodable>::decode(buf)?)
52 .ok_or(alloy_rlp::Error::Custom($rlp_error))
53 }
54 }
55 };
56}