Skip to content

Commit

Permalink
refactor: swap Enr Debug and Display impl and enhance Debug impl (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 9, 2023
1 parent e59dcb4 commit 31088be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
46 changes: 35 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl<K: EnrKey> Enr<K> {
#[must_use]
pub fn to_base64(&self) -> String {
let hex = base64::encode_config(&rlp::encode(self), base64::URL_SAFE_NO_PAD);
format!("enr:{}", hex)
format!("enr:{hex}")
}

/// Returns the current size of the ENR.
Expand Down Expand Up @@ -851,19 +851,43 @@ impl<K: EnrKey> Hash for Enr<K> {

impl<K: EnrKey> std::fmt::Display for Enr<K> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"ENR: NodeId: {}, IpV4 Socket: {:?} IpV6 Socket: {:?}",
self.node_id(),
self.udp4_socket(),
self.udp6_socket()
)
write!(f, "{}", self.to_base64())
}
}

impl<K: EnrKey> std::fmt::Debug for Enr<K> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_base64())
struct OtherPairs<'a>(&'a BTreeMap<Key, Bytes>);

impl<'a> std::fmt::Debug for OtherPairs<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_list()
.entries(
self.0
.iter()
.filter(|(key, _)| {
// skip all pairs already covered as fields
!["id", "ip", "ip6", "udp", "udp6", "tcp", "tcp6"]
.iter()
.any(|k| k.as_bytes() == key.as_slice())
})
.map(|(key, val)| (String::from_utf8_lossy(key), hex::encode(val))),
)
.finish()
}
}

f.debug_struct("Enr")
.field("id", &self.id())
.field("seq", &self.seq())
.field("NodeId", &self.node_id())
.field("signature", &hex::encode(&self.signature))
.field("IpV4 UDP Socket", &self.udp4_socket())
.field("IpV6 UDP Socket", &self.udp6_socket())
.field("IpV4 TCP Socket", &self.tcp4_socket())
.field("IpV6 TCP Socket", &self.tcp6_socket())
.field("Other Pairs", &OtherPairs(&self.content))
.finish()
}
}

Expand All @@ -883,8 +907,8 @@ impl<K: EnrKey> FromStr for Enr<K> {
.ok_or_else(|| "Invalid ENR string".to_string())?;
}
let bytes = base64::decode_config(decode_string, base64::URL_SAFE_NO_PAD)
.map_err(|e| format!("Invalid base64 encoding: {:?}", e))?;
rlp::decode(&bytes).map_err(|e| format!("Invalid ENR: {:?}", e))
.map_err(|e| format!("Invalid base64 encoding: {e:?}"))?;
rlp::decode(&bytes).map_err(|e| format!("Invalid ENR: {e:?}"))
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{digest, keys::EnrPublicKey, Enr, EnrKey};

type RawNodeId = [u8; 32];

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
/// The `NodeId` of an ENR (a 32 byte identifier).
pub struct NodeId {
raw: RawNodeId,
Expand Down Expand Up @@ -94,6 +94,12 @@ impl std::fmt::Display for NodeId {
}
}

impl std::fmt::Debug for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "0x{}", hex::encode(self.raw))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 31088be

Please sign in to comment.