1use super::Redactable;
4use std::fmt::{self, Formatter};
5
6impl Redactable for std::net::Ipv4Addr {
9 fn display_redacted(&self, f: &mut Formatter<'_>) -> fmt::Result {
10 write!(f, "{}.x.x.x", self.octets()[0])
11 }
12}
13
14impl Redactable for std::net::Ipv6Addr {
15 fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 write!(f, "{:x}:x:x:…", self.segments()[0])
17 }
18}
19
20impl Redactable for std::net::IpAddr {
21 fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 match self {
23 std::net::IpAddr::V4(v4) => v4.display_redacted(f),
24 std::net::IpAddr::V6(v6) => v6.display_redacted(f),
25 }
26 }
27}
28
29impl Redactable for std::net::SocketAddrV4 {
30 fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 write!(f, "{}:{}", self.ip().redacted(), self.port())
32 }
33}
34
35impl Redactable for std::net::SocketAddrV6 {
36 fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 write!(f, "[{}]:{}", self.ip().redacted(), self.port())
38 }
39}
40
41impl Redactable for std::net::SocketAddr {
42 fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 match self {
44 std::net::SocketAddr::V4(v4) => v4.display_redacted(f),
45 std::net::SocketAddr::V6(v6) => v6.display_redacted(f),
46 }
47 }
48}
49
50#[cfg(test)]
51mod test {
52 #![allow(clippy::bool_assert_comparison)]
54 #![allow(clippy::clone_on_copy)]
55 #![allow(clippy::dbg_macro)]
56 #![allow(clippy::mixed_attributes_style)]
57 #![allow(clippy::print_stderr)]
58 #![allow(clippy::print_stdout)]
59 #![allow(clippy::single_char_pattern)]
60 #![allow(clippy::unwrap_used)]
61 #![allow(clippy::unchecked_time_subtraction)]
62 #![allow(clippy::useless_vec)]
63 #![allow(clippy::needless_pass_by_value)]
64 #![allow(clippy::string_slice)] use std::{
68 net::{IpAddr, SocketAddr},
69 str::FromStr,
70 };
71
72 use crate::Redactable;
73 use serial_test::serial;
74
75 #[test]
76 #[serial]
77 fn ip() {
78 let r = |s| IpAddr::from_str(s).unwrap().redacted().to_string();
79
80 assert_eq!(&r("127.0.0.1"), "127.x.x.x");
81 assert_eq!(&r("::1"), "0:x:x:…");
82 assert_eq!(&r("192.0.2.55"), "192.x.x.x");
83 assert_eq!(&r("2001:db8::f00d"), "2001:x:x:…");
84 }
85
86 #[test]
87 #[serial]
88 fn sockaddr() {
89 let r = |s| SocketAddr::from_str(s).unwrap().redacted().to_string();
90
91 assert_eq!(&r("127.0.0.1:55"), "127.x.x.x:55");
92 assert_eq!(&r("[::1]:443"), "[0:x:x:…]:443");
93 assert_eq!(&r("192.0.2.55:80"), "192.x.x.x:80");
94 assert_eq!(&r("[2001:db8::f00d]:9001"), "[2001:x:x:…]:9001");
95 }
96}