apollo-redaction 0.1.0

Redaction utility for sensitive data in error messages, debug logs, etc.
Documentation
use crate::RedactionStrategy;
use std::fmt;

/// Redact passwords with a fixed-length output.
///
/// Always outputs `********` regardless of the actual password length.
/// This prevents leaking information about password length.
///
/// ```rust
/// use apollo_redaction::Redacted;
/// use apollo_redaction::strategy::PasswordRedactor;
///
/// fn password(input: &str) -> String {
///     Redacted::<_, PasswordRedactor>::new(input).to_string()
/// }
///
/// assert_eq!(password("short"), "********");
/// assert_eq!(password("a_very_long_password_123!"), "********");
/// assert_eq!(password("x"), "********");
/// ```
#[derive(Default, Clone)]
pub struct PasswordRedactor;

impl<T> RedactionStrategy<T> for PasswordRedactor {
    fn display(&self, _value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "********")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Redacted;

    fn password(input: &str) -> String {
        Redacted::<_, PasswordRedactor>::new(input).to_string()
    }

    #[test]
    fn test_fixed_length() {
        assert_eq!(password("short"), "********");
        assert_eq!(password("a_very_long_password_123!"), "********");
        assert_eq!(password("x"), "********");
        assert_eq!(password(""), "********");
    }
}