use std::borrow::Cow;
#[cfg(doc)]
use crate::misc::redacted::Redacted;
pub struct Fully;
impl RedactionConfig for Fully {
const VALUE: RedactionConfigValue = RedactionConfigValue {
debug: PartToRedact::Everything,
display: PartToRedact::Everything,
serialize: PartToRedact::Nothing,
tracing_serialize: PartToRedact::Everything,
};
}
pub struct After<const N: usize>;
impl<const N: usize> RedactionConfig for After<N> {
const VALUE: RedactionConfigValue = RedactionConfigValue {
debug: PartToRedact::End { after: N },
display: PartToRedact::End { after: N },
serialize: PartToRedact::Nothing,
tracing_serialize: PartToRedact::End { after: N },
};
}
pub trait RedactionConfig {
const VALUE: RedactionConfigValue;
}
pub struct RedactionConfigValue {
pub debug: PartToRedact,
pub display: PartToRedact,
pub serialize: PartToRedact,
pub tracing_serialize: PartToRedact,
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum PartToRedact {
Nothing,
Everything,
End {
after: usize,
},
Middle {
after: usize,
before: usize,
},
}
impl PartToRedact {
pub fn redact<'a>(&self, string: &'a str) -> Cow<'a, str> {
match self {
PartToRedact::Nothing => Cow::Borrowed(string),
PartToRedact::Everything => Cow::Borrowed("-redacted-"),
PartToRedact::End { after } => Cow::Owned(format!(
"{}-redacted-",
&string[..string.floor_char_boundary(*after)]
)),
PartToRedact::Middle { after, before } => Cow::Owned(format!(
"{}-redacted-{}",
&string[..string.floor_char_boundary(*after)],
&string[string.ceil_char_boundary(*before)..]
)),
}
}
}