1use serde::{Deserialize, Serialize};
3
4fn default_replace_text() -> String {
5 "[Filtered]".into()
6}
7
8#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
10#[serde(rename_all = "camelCase")]
11pub struct ReplaceRedaction {
12 #[serde(default = "default_replace_text")]
14 pub text: String,
15}
16
17impl From<String> for ReplaceRedaction {
18 fn from(text: String) -> ReplaceRedaction {
19 ReplaceRedaction { text }
20 }
21}
22
23impl Default for ReplaceRedaction {
24 fn default() -> Self {
25 ReplaceRedaction {
26 text: default_replace_text(),
27 }
28 }
29}
30
31#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Default)]
33#[serde(tag = "method", rename_all = "snake_case")]
34pub enum Redaction {
35 #[default]
41 Default,
42 Remove,
44 Replace(ReplaceRedaction),
46 Mask,
48 Hash,
50 #[serde(other, skip_serializing)]
52 Other,
53}
54
55#[cfg(test)]
56mod tests {
57
58 use super::*;
59
60 #[test]
61 fn test_redaction_deser_method() {
62 let json = r#"{"method": "replace", "text": "[filter]"}"#;
63
64 let deser: Redaction = serde_json::from_str(json).unwrap();
65 let redaction = Redaction::Replace(ReplaceRedaction {
66 text: "[filter]".to_string(),
67 });
68 assert!(deser == redaction);
69 }
70
71 #[test]
72 fn test_redaction_deser_other() {
73 let json = r#"{"method": "foo", "text": "[filter]"}"#;
74
75 let deser: Redaction = serde_json::from_str(json).unwrap();
76 assert!(matches!(deser, Redaction::Other));
77 }
78}