Skip to content

Commit

Permalink
feat: rlp
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 14, 2023
1 parent 49536d0 commit fb5cf98
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ strum = { version = "0.25", default-features = false, features = ["derive"] }
# serde
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }

# rlp
alloy-rlp = { version = "0.3", default-features = false, features = ["derive"], optional = true }

# arbitrary
arbitrary = { version = "1.3.2", optional = true }
proptest = { version = "1.4", optional = true }
arbitrary = { version = "1.3.2", default-features = false, optional = true }
proptest = { version = "1.4", default-features = false, features = ["alloc"], optional = true }

[dev-dependencies]
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
Expand All @@ -27,4 +30,5 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
default = ["std"]
std = ["strum/std", "serde?/std"]
serde = ["dep:serde"]
rlp = ["dep:alloy-rlp"]
arbitrary = ["dep:arbitrary", "dep:proptest"]
31 changes: 11 additions & 20 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,24 @@ impl fmt::Display for Chain {
}
}

#[cfg(TODO)]
#[cfg(feature = "rlp")]
impl alloy_rlp::Encodable for Chain {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
match self {
Self::Named(chain) => u64::from(*chain).encode(out),
Self::Named(chain) => chain.encode(out),
Self::Id(id) => id.encode(out),
}
}

fn length(&self) -> usize {
match self {
Self::Named(chain) => u64::from(*chain).length(),
Self::Named(chain) => chain.length(),
Self::Id(id) => id.length(),
}
}
}

#[cfg(TODO)]
#[cfg(feature = "rlp")]
impl alloy_rlp::Decodable for Chain {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(u64::decode(buf)?.into())
Expand Down Expand Up @@ -236,66 +236,57 @@ mod tests {

#[test]
fn test_id() {
let chain = Chain::Id(1234);
assert_eq!(chain.id(), 1234);
assert_eq!(Chain::Id(1234).id(), 1234);
}

#[test]
fn test_named_id() {
let chain = Chain::Named(NamedChain::Goerli);
assert_eq!(chain.id(), 5);
assert_eq!(Chain::Named(NamedChain::Goerli).id(), 5);
}

#[test]
fn test_display_named_chain() {
let chain = Chain::Named(NamedChain::Mainnet);
assert_eq!(format!("{chain}"), "mainnet");
assert_eq!(Chain::Named(NamedChain::Mainnet).to_string(), "mainnet");
}

#[test]
fn test_display_id_chain() {
let chain = Chain::Id(1234);
assert_eq!(format!("{chain}"), "1234");
assert_eq!(Chain::Id(1234).to_string(), "1234");
}

#[test]
fn test_from_str_named_chain() {
let result = Chain::from_str("mainnet");
let expected = Chain::Named(NamedChain::Mainnet);

assert!(result.is_ok());
assert_eq!(result.unwrap(), expected);
}

#[test]
fn test_from_str_named_chain_error() {
let result = Chain::from_str("chain");

assert!(result.is_err());
}

#[test]
fn test_from_str_id_chain() {
let result = Chain::from_str("1234");
let expected = Chain::Id(1234);

assert!(result.is_ok());
assert_eq!(result.unwrap(), expected);
}

#[test]
fn test_default() {
let default = Chain::default();
let expected = Chain::Named(NamedChain::Mainnet);

assert_eq!(default, expected);
}

#[cfg(TODO)]
#[cfg(feature = "rlp")]
#[test]
fn test_id_chain_encodable_length() {
let chain = Chain::Id(1234);
use alloy_rlp::Encodable;

let chain = Chain::Id(1234);
assert_eq!(chain.length(), 3);
}

Expand Down
22 changes: 22 additions & 0 deletions src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ impl serde::Serialize for NamedChain {
}
}

#[cfg(feature = "rlp")]
impl alloy_rlp::Encodable for NamedChain {
#[inline]
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
(*self as u64).encode(out)
}

#[inline]
fn length(&self) -> usize {
(*self as u64).length()
}
}

#[cfg(feature = "rlp")]
impl alloy_rlp::Decodable for NamedChain {
#[inline]
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let n = u64::decode(buf)?;
Self::try_from(n).map_err(|_| alloy_rlp::Error::Overflow)
}
}

// NB: all utility functions *should* be explicitly exhaustive (not use `_` matcher) so we don't
// forget to update them when adding a new `NamedChain` variant.
#[allow(clippy::match_like_matches_macro)]
Expand Down

0 comments on commit fb5cf98

Please sign in to comment.