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;
25
26/// A redactor that does not redact. The value to be redacted must implement both [`Display`] and
27/// [`Debug`].
28#[derive(Default, Clone)]
29pub struct NoRedactor;
30
31impl<T: Debug + Display> RedactionStrategy<T> for NoRedactor {
32    fn debug(&self, value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        Debug::fmt(value, f)
34    }
35    fn display(&self, value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        Display::fmt(value, f)
37    }
38}
39
40/// A redactor that just writes "\[REDACTED]". This strategy can redact values of any type.
41#[derive(Default, Clone)]
42pub struct SimpleRedactor;
43
44impl<T> RedactionStrategy<T> for SimpleRedactor {
45    fn display(&self, _value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "[REDACTED]")
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn display() {
56        let my_secret = "topsecret";
57        let redacted = crate::Redacted::<_, SimpleRedactor>::new(my_secret);
58
59        assert_eq!(redacted.to_string(), "[REDACTED]");
60        assert_eq!(
61            format!("Ha! the secret is: {redacted}!"),
62            "Ha! the secret is: [REDACTED]!"
63        );
64    }
65
66    #[test]
67    fn debug() {
68        let my_secret = "topsecret, even for debug";
69        let redacted = crate::Redacted::<_, SimpleRedactor>::new(my_secret);
70
71        assert_eq!(
72            format!("Ha! the secret is: {redacted:?}!"),
73            "Ha! the secret is: [REDACTED]!"
74        );
75    }
76}