use std::fmt::{
self,
Debug,
Formatter,
};
use crate::{
RedactValue,
RedactionPolicy,
};
pub trait RedactMapValue<K: ?Sized, V: ?Sized> {
#[doc(hidden)]
fn fmt_redacted_map(
&self,
policy: &RedactionPolicy,
formatter: &mut Formatter<'_>,
) -> fmt::Result;
}
impl<M: ?Sized, K: ?Sized, V: ?Sized> RedactMapValue<K, V> for M
where
for<'a> &'a M: IntoIterator<Item = (&'a K, &'a V)>,
K: AsRef<str> + Debug,
V: RedactValue + Debug,
{
#[inline]
fn fmt_redacted_map(
&self,
policy: &RedactionPolicy,
formatter: &mut Formatter<'_>,
) -> fmt::Result {
let mut map = formatter.debug_map();
for (key, value) in self {
if let Some(level) = policy.sensitivity_for(key.as_ref()) {
let redacted = value.redact_value(level, policy.masking());
map.entry(&key, &redacted);
} else {
map.entry(&key, &value);
}
}
map.finish()
}
}