pub trait Redact {
fn redact(&self, secrets: &[impl AsRef<str>]) -> String;
}
impl<T> Redact for T
where
T: AsRef<str> + ToString,
{
fn redact(&self, secrets: &[impl AsRef<str>]) -> String {
let mut value = self.to_string();
for s in secrets {
value = value.replace(s.as_ref(), "***");
}
value
}
}
#[cfg(test)]
mod tests {
use crate::util::redacted::Redact;
#[test]
fn redacted_string_hides_secret() {
fn assert_eq(left: &str, right: &str) {
assert_eq!(left, right);
}
let secrets = ["foo", "bar", "baz"];
assert_eq(
&"Hello, here are secrets values: foo".redact(&secrets),
"Hello, here are secrets values: ***",
);
assert_eq(&"bar".redact(&secrets), "***");
assert_eq(&"Baz is not secret".redact(&secrets), "Baz is not secret");
}
}