Skip to content

Commit

Permalink
Handle connection ids greater than 9 in peer Human impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Jun 22, 2024
1 parent 35303b1 commit dc6afd8
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion aquadoggo/src/network/peers/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit dc6afd8

Please sign in to comment.