Skip to main content

apollo_redaction/strategy/
full.rs

1use crate::RedactionStrategy;
2use std::fmt;
3
4/// Redact the entire value with asterisks, preserving length.
5///
6/// The output will be at least 6 asterisks to avoid leaking information about very short values.
7///
8/// ```rust
9/// use apollo_redaction::Redacted;
10/// use apollo_redaction::strategy::FullRedactor;
11///
12/// fn full(input: &str) -> String {
13///     Redacted::<_, FullRedactor>::new(input).to_string()
14/// }
15///
16/// assert_eq!(full("secret"), "******");
17/// assert_eq!(full("verylongsecret"), "**************");
18/// assert_eq!(full("abc"), "******"); // minimum 6 asterisks
19/// ```
20#[derive(Default, Clone)]
21pub struct FullRedactor;
22
23impl<T: AsRef<str>> RedactionStrategy<T> for FullRedactor {
24    fn display(&self, value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        let len = value.as_ref().chars().count().max(6);
26        for _ in 0..len {
27            write!(f, "*")?;
28        }
29        Ok(())
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use crate::Redacted;
37
38    fn full(input: &str) -> String {
39        Redacted::<_, FullRedactor>::new(input).to_string()
40    }
41
42    #[test]
43    fn test_full_redaction() {
44        assert_eq!(full("secret"), "******");
45        assert_eq!(full("verylongsecret"), "**************");
46    }
47
48    #[test]
49    fn test_minimum_length() {
50        assert_eq!(full("abc"), "******");
51        assert_eq!(full("a"), "******");
52        assert_eq!(full(""), "******");
53    }
54
55    #[test]
56    fn test_unicode() {
57        // Unicode characters should count as 1 each
58        assert_eq!(full("안녕하세요한글"), "*******");
59    }
60}