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 9ccc782
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 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"]
27 changes: 9 additions & 18 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ 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 {
Expand All @@ -193,7 +193,7 @@ impl alloy_rlp::Encodable for Chain {
}
}

#[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

0 comments on commit 9ccc782

Please sign in to comment.