Skip to main content

apollo_redaction/strategy/
mod.rs

1use crate::RedactionStrategy;
2use std::fmt;
3use std::fmt::Debug;
4use std::fmt::Display;
5
6mod card;
7mod email;
8mod first4;
9mod fixed;
10mod full;
11mod ip;
12mod last4;
13mod password;
14mod url;
15
16pub use card::CardRedactor;
17pub use email::EmailRedactor;
18pub use first4::First4Redactor;
19pub use fixed::FixedRedactor;
20pub use full::FullRedactor;
21pub use ip::IpRedactor;
22pub use last4::Last4Redactor;
23pub use password::PasswordRedactor;
24pub use url::UrlHostRedactor;
25pub use url::UrlPasswordRedactor;
26
27/// A redactor that does not redact. The value to be redacted must implement both [`Display`] and
28/// [`Debug`].
29#[derive(Default, Clone)]
30pub struct NoRedactor;
31
32impl<T: Debug + Display> RedactionStrategy<T> for NoRedactor {
33    fn debug(&self, value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        Debug::fmt(value, f)
35    }
36    fn display(&self, value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        Display::fmt(value, f)
38    }
39}
40
41/// A redactor that just writes "\[REDACTED]". This strategy can redact values of any type.
42#[derive(Default, Clone)]
43pub struct SimpleRedactor;
44
45impl<T> RedactionStrategy<T> for SimpleRedactor {
46    fn display(&self, _value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "[REDACTED]")
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn display() {
57        let my_secret = "topsecret";
58        let redacted = crate::Redacted::<_, SimpleRedactor>::new(my_secret);
59
60        assert_eq!(redacted.to_string(), "[REDACTED]");
61        assert_eq!(
62            format!("Ha! the secret is: {redacted}!"),
63            "Ha! the secret is: [REDACTED]!"
64        );
65    }
66
67    #[test]
68    fn debug() {
69        let my_secret = "topsecret, even for debug";
70        let redacted = crate::Redacted::<_, SimpleRedactor>::new(my_secret);
71
72        assert_eq!(
73            format!("Ha! the secret is: {redacted:?}!"),
74            "Ha! the secret is: [REDACTED]!"
75        );
76    }
77}