use std::borrow::Cow;
pub use crate::pass_through_reason::PassThroughReason;
use crate::{
LogSafeText,
RedactedText,
Sensitivity,
};
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldRedaction<'a> {
Masked {
value: RedactedText<'a>,
sensitivity: Sensitivity,
},
PassedThrough {
value: &'a str,
reason: PassThroughReason,
},
}
impl<'a> FieldRedaction<'a> {
#[inline]
pub fn as_str(&self) -> &str {
match self {
Self::Masked { value, .. } => value.as_str(),
Self::PassedThrough { value, .. } => value,
}
}
#[inline]
pub const fn is_masked(&self) -> bool {
matches!(self, Self::Masked { .. })
}
#[inline]
pub const fn sensitivity(&self) -> Option<Sensitivity> {
match self {
Self::Masked { sensitivity, .. } => Some(*sensitivity),
Self::PassedThrough { .. } => None,
}
}
#[inline]
pub const fn pass_through_reason(&self) -> Option<PassThroughReason> {
match self {
Self::Masked { .. } => None,
Self::PassedThrough { reason, .. } => Some(*reason),
}
}
#[inline]
pub fn into_owned(self) -> String {
match self {
Self::Masked { value, .. } => value.into_owned(),
Self::PassedThrough { value, .. } => value.to_owned(),
}
}
#[inline]
pub fn escape_for_log(self) -> LogSafeText<'a> {
match self {
Self::Masked { value, .. } => value.escape_for_log(),
Self::PassedThrough { value, .. } => {
RedactedText::new(Cow::Borrowed(value)).escape_for_log()
}
}
}
}