safelog/
impls.rs

1//! Implement `Redactable` for various types.
2
3use super::Redactable;
4use std::fmt::{self, Formatter};
5
6// Network types.
7
8impl 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    // @@ begin test lint list maintained by maint/add_warning @@
53    #![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_duration_subtraction)]
62    #![allow(clippy::useless_vec)]
63    #![allow(clippy::needless_pass_by_value)]
64    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
65
66    use std::{
67        net::{IpAddr, SocketAddr},
68        str::FromStr,
69    };
70
71    use crate::Redactable;
72    use serial_test::serial;
73
74    #[test]
75    #[serial]
76    fn ip() {
77        let r = |s| IpAddr::from_str(s).unwrap().redacted().to_string();
78
79        assert_eq!(&r("127.0.0.1"), "127.x.x.x");
80        assert_eq!(&r("::1"), "0:x:x:…");
81        assert_eq!(&r("192.0.2.55"), "192.x.x.x");
82        assert_eq!(&r("2001:db8::f00d"), "2001:x:x:…");
83    }
84
85    #[test]
86    #[serial]
87    fn sockaddr() {
88        let r = |s| SocketAddr::from_str(s).unwrap().redacted().to_string();
89
90        assert_eq!(&r("127.0.0.1:55"), "127.x.x.x:55");
91        assert_eq!(&r("[::1]:443"), "[0:x:x:…]:443");
92        assert_eq!(&r("192.0.2.55:80"), "192.x.x.x:80");
93        assert_eq!(&r("[2001:db8::f00d]:9001"), "[2001:x:x:…]:9001");
94    }
95}