From dc6afd82667fd0f4fd2e226984346d1f1605e878 Mon Sep 17 00:00:00 2001 From: Sam Andreae Date: Sat, 22 Jun 2024 15:18:39 +0100 Subject: [PATCH] Handle connection ids greater than 9 in peer Human impl --- aquadoggo/src/network/peers/peer.rs | 30 ++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/aquadoggo/src/network/peers/peer.rs b/aquadoggo/src/network/peers/peer.rs index 08696e221..ae2857675 100644 --- a/aquadoggo/src/network/peers/peer.rs +++ b/aquadoggo/src/network/peers/peer.rs @@ -59,7 +59,35 @@ impl PartialOrd for Peer { impl Human for Peer { fn display(&self) -> String { // Trick to nicely display `ConnectionId` struct - let connection_id = &format!("{:?}", self.1)[13..][..1]; + let connection_id = format!("{:?}", self.1); + let connection_id = connection_id[13..] + .strip_suffix(")") + .expect("ConnectionId format is as expected"); format!("{} ({})", self.0, connection_id) } } + +#[cfg(test)] +mod tests { + use libp2p::identity::Keypair; + use libp2p::swarm::ConnectionId; + use p2panda_rs::Human; + + use super::Peer; + + #[test] + fn peers_display() { + let peer_id = Keypair::generate_ed25519().public().to_peer_id(); + let peer_connection_2 = Peer::new(peer_id, ConnectionId::new_unchecked(2)); + assert_eq!(peer_connection_2.display(), format!("{} ({})", peer_id, 2)); + + let peer_connection_23 = Peer::new(peer_id, ConnectionId::new_unchecked(23)); + assert_eq!(peer_connection_23.display(), format!("{} ({})", peer_id, 23)); + + let peer_connection_999 = Peer::new(peer_id, ConnectionId::new_unchecked(999)); + assert_eq!( + peer_connection_999.display(), + format!("{} ({})", peer_id, 999) + ); + } +}