From a6ce079d4add585266e308fda0abed841a467f5e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Jun 2023 15:53:12 -0400 Subject: [PATCH] linkspec: implement Display/Redacted on RelayIds --- crates/tor-linkspec/src/owned.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/tor-linkspec/src/owned.rs b/crates/tor-linkspec/src/owned.rs index a05d3a0e2..52b48dfb6 100644 --- a/crates/tor-linkspec/src/owned.rs +++ b/crates/tor-linkspec/src/owned.rs @@ -78,6 +78,17 @@ impl RelayIds { } } +impl std::fmt::Display for RelayIds { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.display_relay_ids()) + } +} +impl Redactable for RelayIds { + fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.display_relay_ids().redacted()) + } +} + /// OwnedChanTarget is a summary of a [`ChanTarget`] that owns all of its /// members. #[derive(Debug, Clone, derive_builder::Builder)] @@ -323,4 +334,15 @@ mod test { assert_eq!(format!("{:?}", ct), format!("{:?}", ct2)); assert_eq!(format!("{:?}", ct), format!("{:?}", ct.clone())); } + + #[test] + fn format_relay_ids() { + let mut builder = RelayIds::builder(); + builder + .ed_identity([42; 32].into()) + .rsa_identity([45; 20].into()); + let ids = builder.build().unwrap(); + assert_eq!(format!("{}", ids), "ed25519:KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKio $2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d"); + assert_eq!(format!("{}", ids.redacted()), "Ki…"); + } }