1#[derive(Debug, Clone, Copy)]
7pub struct RedactionPolicy {
8 pub redact_error_messages: bool,
9}
10
11impl RedactionPolicy {
12 pub fn none() -> Self {
13 Self {
14 redact_error_messages: false,
15 }
16 }
17
18 pub fn all() -> Self {
19 Self {
20 redact_error_messages: true,
21 }
22 }
23
24 pub fn scrub_message(&self, message: &str) -> String {
25 match (self.redact_error_messages, message.is_empty()) {
26 (_, true) => String::new(),
27 (true, false) => "[REDACTED]".to_string(),
28 (false, false) => message.to_string(),
29 }
30 }
31}
32
33impl Default for RedactionPolicy {
34 fn default() -> Self {
35 Self::all()
36 }
37}
38
39pub fn sanitize_path(path: &str) -> String {
47 if let Some(rest) = path
49 .strip_prefix("/Users/")
50 .or_else(|| path.strip_prefix("/home/"))
51 {
52 if let Some(pos) = rest.find('/') {
54 return format!("~{}", &rest[pos..]);
55 }
56 return "~".to_string();
58 }
59
60 let bytes = path.as_bytes();
63 if bytes.len() >= 3 && bytes[1] == b':' && (bytes[2] == b'\\' || bytes[2] == b'/') {
64 let prefix = &bytes[0..2]; let rest = &bytes[3..]; if rest.len() >= 6
67 && (rest[0..6].eq_ignore_ascii_case(b"Users\\")
68 || rest[0..6].eq_ignore_ascii_case(b"Users/"))
69 {
70 let after_users = &rest[6..]; if let Some(pos) = after_users.iter().position(|&b| b == b'\\' || b == b'/') {
72 let remaining = std::str::from_utf8(&after_users[pos..]).unwrap_or("");
73 return format!(
74 "{prefix}~{remaining}",
75 prefix = std::str::from_utf8(prefix).unwrap_or("C:")
76 );
77 }
78 return format!("{}~", std::str::from_utf8(prefix).unwrap_or("C:"));
79 }
80 }
81
82 path.to_string()
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn sanitize_users_path() {
91 assert_eq!(
92 sanitize_path("/Users/xbits/project/file.rs"),
93 "~/project/file.rs"
94 );
95 }
96
97 #[test]
98 fn sanitize_home_path() {
99 assert_eq!(
100 sanitize_path("/home/alice/.config/app.toml"),
101 "~/.config/app.toml"
102 );
103 }
104
105 #[test]
106 fn sanitize_nested_users_path() {
107 assert_eq!(
108 sanitize_path("/Users/bob/Documents/work/notes.txt"),
109 "~/Documents/work/notes.txt"
110 );
111 }
112
113 #[test]
114 fn sanitize_windows_path() {
115 let result = sanitize_path("C:\\Users\\bob\\Documents\\notes.txt");
117 assert_eq!(result, "C:~\\Documents\\notes.txt");
118 }
119
120 #[test]
121 fn sanitize_non_home_path_unchanged() {
122 let path = "/opt/app/config.json";
123 assert_eq!(sanitize_path(path), path);
124 }
125
126 #[test]
127 fn sanitize_relative_path_unchanged() {
128 let path = "./src/main.rs";
129 assert_eq!(sanitize_path(path), path);
130 }
131
132 #[test]
133 fn sanitize_empty_string_unchanged() {
134 assert_eq!(sanitize_path(""), "");
135 }
136
137 #[test]
138 fn a_message_is_replaced_whole_or_passed_through() {
139 let message = "mail ops@example.com about /Users/me/x";
140 assert_eq!(RedactionPolicy::all().scrub_message(message), "[REDACTED]");
141 assert_eq!(RedactionPolicy::none().scrub_message(message), message);
142 assert_eq!(RedactionPolicy::all().scrub_message(""), "");
143 }
144}