fallow_output/
json_paths.rs1pub fn strip_root_prefix(value: &mut serde_json::Value, prefix: &str) {
9 match value {
10 serde_json::Value::String(s) => strip_root_prefix_from_string(s, prefix),
11 serde_json::Value::Array(items) => {
12 for item in items {
13 strip_root_prefix(item, prefix);
14 }
15 }
16 serde_json::Value::Object(map) => {
17 for (key, value) in map.iter_mut() {
18 if key == VERBATIM_KEY {
19 continue;
20 }
21 strip_root_prefix(value, prefix);
22 }
23 }
24 _ => {}
25 }
26}
27
28const VERBATIM_KEY: &str = "request_outcomes";
36
37fn strip_root_prefix_from_string(value: &mut String, prefix: &str) {
38 if let Some(rest) = value.strip_prefix(prefix) {
39 *value = rest.to_string();
40 return;
41 }
42
43 let normalized = normalize_uri(value);
44 let normalized_prefix = normalize_uri(prefix);
45 if let Some(rest) = normalized.strip_prefix(&normalized_prefix) {
46 *value = rest.to_string();
47 } else if let Some(stripped) = strip_embedded_root_prefixes(&normalized, &normalized_prefix) {
48 *value = stripped;
49 }
50}
51
52#[must_use]
59pub fn normalize_uri(path: &str) -> String {
60 path.replace('\\', "/")
61 .replace('[', "%5B")
62 .replace(']', "%5D")
63}
64
65fn strip_embedded_root_prefixes(value: &str, prefix: &str) -> Option<String> {
66 let mut output = String::with_capacity(value.len());
67 let mut changed = false;
68 let mut last = 0;
69 let mut search_from = 0;
70
71 while let Some(offset) = value[search_from..].find(prefix) {
72 let index = search_from + offset;
73 let can_strip = index > 0
74 && value[..index]
75 .chars()
76 .next_back()
77 .is_some_and(is_embedded_path_boundary);
78
79 if can_strip {
80 output.push_str(&value[last..index]);
81 last = index + prefix.len();
82 changed = true;
83 }
84
85 search_from = index + prefix.len();
86 }
87
88 if changed {
89 output.push_str(&value[last..]);
90 Some(output)
91 } else {
92 None
93 }
94}
95
96fn is_embedded_path_boundary(c: char) -> bool {
97 c.is_whitespace() || matches!(c, '"' | '\'' | '`' | '(' | '[' | '{' | ':' | '=')
98}
99
100#[cfg(test)]
101mod tests {
102 use serde_json::json;
103
104 use super::*;
105
106 #[test]
107 fn strips_root_from_nested_strings() {
108 let mut value = json!({
109 "path": "/project/src/index.ts",
110 "items": ["/project/src/a.ts", { "path": "/project/src/b.ts" }]
111 });
112
113 strip_root_prefix(&mut value, "/project/");
114
115 assert_eq!(value["path"], "src/index.ts");
116 assert_eq!(value["items"][0], "src/a.ts");
117 assert_eq!(value["items"][1]["path"], "src/b.ts");
118 }
119
120 #[test]
121 fn normalizes_windows_separators_before_stripping() {
122 let mut value = json!("C:\\repo\\src\\index.ts");
123
124 strip_root_prefix(&mut value, "C:/repo/");
125
126 assert_eq!(value, json!("src/index.ts"));
127 }
128
129 #[test]
130 fn rewrites_embedded_path_strings() {
131 let mut value = json!("See /project/src/a.ts and /project/src/b.ts");
132
133 strip_root_prefix(&mut value, "/project/");
134
135 assert_eq!(value, json!("See src/a.ts and src/b.ts"));
136 }
137
138 #[test]
139 fn leaves_request_outcomes_verbatim_while_stripping_its_siblings() {
140 let mut value = json!({
141 "path": "/project/src/index.ts",
142 "request_outcomes": {
143 "sarif-file": {
144 "requested": "/project/out/results.sarif",
145 "message": "failed to write SARIF file '/project/out/results.sarif'"
146 }
147 }
148 });
149
150 strip_root_prefix(&mut value, "/project/");
151
152 assert_eq!(value["path"], "src/index.ts");
153 let entry = &value["request_outcomes"]["sarif-file"];
154 assert_eq!(entry["requested"], "/project/out/results.sarif");
155 assert_eq!(
156 entry["message"],
157 "failed to write SARIF file '/project/out/results.sarif'"
158 );
159 }
160
161 #[test]
162 fn leaves_non_matching_strings_unchanged() {
163 let mut value = json!("src/index.ts");
164
165 strip_root_prefix(&mut value, "/project/");
166
167 assert_eq!(value, json!("src/index.ts"));
168 }
169
170 #[test]
171 fn normalize_uri_rewrites_backslashes_and_brackets() {
172 assert_eq!(
173 normalize_uri("app\\[lang]\\posts\\[id].tsx"),
174 "app/%5Blang%5D/posts/%5Bid%5D.tsx"
175 );
176 }
177}