use crate::redactor::Redactor;
#[derive(Debug)]
pub struct RedactorBuilder {
fields: Vec<String>,
mask: String,
#[cfg(feature = "regex")]
field_patterns: Vec<regex::Regex>,
#[cfg(feature = "regex")]
value_patterns: Vec<(regex::Regex, String)>,
}
impl RedactorBuilder {
pub(crate) fn new() -> Self {
RedactorBuilder {
fields: Vec::new(),
mask: "******".to_string(),
#[cfg(feature = "regex")]
field_patterns: Vec::new(),
#[cfg(feature = "regex")]
value_patterns: Vec::new(),
}
}
pub fn field(mut self, name: impl Into<String>) -> Self {
self.fields.push(name.into());
self
}
pub fn fields<I, S>(mut self, names: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.fields.extend(names.into_iter().map(Into::into));
self
}
pub fn mask(mut self, mask: impl Into<String>) -> Self {
self.mask = mask.into();
self
}
pub fn build(self) -> Redactor {
Redactor {
fields: self.fields,
mask: self.mask,
#[cfg(feature = "regex")]
field_patterns: self.field_patterns,
#[cfg(feature = "regex")]
value_patterns: self.value_patterns,
}
}
#[cfg(feature = "regex")]
pub fn field_pattern(mut self, pattern: &str) -> Result<Self, crate::Error> {
let re = regex::Regex::new(pattern).map_err(|source| crate::Error::InvalidRegex {
pattern: pattern.to_string(),
source,
})?;
self.field_patterns.push(re);
Ok(self)
}
#[cfg(feature = "regex")]
pub fn value_pattern(mut self, pattern: &str, replacement: &str) -> Result<Self, crate::Error> {
let re = regex::Regex::new(pattern).map_err(|source| crate::Error::InvalidRegex {
pattern: pattern.to_string(),
source,
})?;
self.value_patterns.push((re, replacement.to_string()));
Ok(self)
}
}