use serde::{Deserialize, Serialize};
fn default_replace_text() -> String {
"[Filtered]".into()
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ReplaceRedaction {
#[serde(default = "default_replace_text")]
pub text: String,
}
impl From<String> for ReplaceRedaction {
fn from(text: String) -> ReplaceRedaction {
ReplaceRedaction { text }
}
}
impl Default for ReplaceRedaction {
fn default() -> Self {
ReplaceRedaction {
text: default_replace_text(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Default)]
#[serde(tag = "method", rename_all = "snake_case")]
pub enum Redaction {
#[default]
Default,
Remove,
Replace(ReplaceRedaction),
Mask,
Hash,
#[serde(other, skip_serializing)]
Other,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_redaction_deser_method() {
let json = r#"{"method": "replace", "text": "[filter]"}"#;
let deser: Redaction = serde_json::from_str(json).unwrap();
let redaction = Redaction::Replace(ReplaceRedaction {
text: "[filter]".to_string(),
});
assert!(deser == redaction);
}
#[test]
fn test_redaction_deser_other() {
let json = r#"{"method": "foo", "text": "[filter]"}"#;
let deser: Redaction = serde_json::from_str(json).unwrap();
assert!(matches!(deser, Redaction::Other));
}
}