1
//! Implement `Redactable` for various types.
2

            
3
use super::Redactable;
4
use std::fmt::{self, Formatter};
5

            
6
// Network types.
7

            
8
impl Redactable for std::net::Ipv4Addr {
9
16
    fn display_redacted(&self, f: &mut Formatter<'_>) -> fmt::Result {
10
16
        write!(f, "{}.x.x.x", self.octets()[0])
11
16
    }
12
}
13

            
14
impl Redactable for std::net::Ipv6Addr {
15
8
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16
8
        write!(f, "{:x}:x:x:…", self.segments()[0])
17
8
    }
18
}
19

            
20
impl Redactable for std::net::IpAddr {
21
8
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22
8
        match self {
23
4
            std::net::IpAddr::V4(v4) => v4.display_redacted(f),
24
4
            std::net::IpAddr::V6(v6) => v6.display_redacted(f),
25
        }
26
8
    }
27
}
28

            
29
impl Redactable for std::net::SocketAddrV4 {
30
4
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31
4
        write!(f, "{}:{}", self.ip().redacted(), self.port())
32
4
    }
33
}
34

            
35
impl Redactable for std::net::SocketAddrV6 {
36
4
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37
4
        write!(f, "[{}]:{}", self.ip().redacted(), self.port())
38
4
    }
39
}
40

            
41
impl Redactable for std::net::SocketAddr {
42
8
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43
8
        match self {
44
4
            std::net::SocketAddr::V4(v4) => v4.display_redacted(f),
45
4
            std::net::SocketAddr::V6(v6) => v6.display_redacted(f),
46
        }
47
8
    }
48
}
49

            
50
#[cfg(test)]
51
mod 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
}