Skip to content

Commit

Permalink
serde(with = "SerHex::<StrictPfx>") NodeId (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML authored Jun 13, 2023
1 parent 9ca806c commit 50871ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = [".gitignore", ".github/*"]
[dependencies]
base64 = "0.21.0"
bytes = "1"
hex = "0.4.2"
hex = {version = "0.4.2", features = ["serde"]}
log = "0.4.8"
rand = "0.8"
rlp = "0.5"
Expand Down
12 changes: 11 additions & 1 deletion src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use serde::{Deserialize, Serialize};

type RawNodeId = [u8; 32];

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
/// The `NodeId` of an ENR (a 32 byte identifier).
pub struct NodeId {
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))]
raw: RawNodeId,
}

Expand Down Expand Up @@ -106,6 +107,7 @@ impl std::fmt::Debug for NodeId {
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;

#[cfg(feature = "serde")]
use serde_json;
Expand All @@ -125,4 +127,12 @@ mod tests {
let json_string = serde_json::to_string(&node).unwrap();
assert_eq!(node, serde_json::from_str::<NodeId>(&json_string).unwrap());
}

#[cfg(feature = "serde")]
#[test]
fn test_serde_as_hashmap_key() {
let mut responses: HashMap<NodeId, u8> = Default::default();
responses.insert(NodeId::random(), 1);
let _ = serde_json::json!(responses);
}
}

0 comments on commit 50871ee

Please sign in to comment.