miden_debug_engine/
source_path.rs1use alloc::{borrow::ToOwned, string::String, vec::Vec};
2
3use miden_debug_types::Uri;
4
5pub fn normalize_source_path(path: &str) -> String {
7 let path = path.trim();
8 let path = Uri::new(path)
9 .to_path()
10 .map(|path| path.to_string_lossy().into_owned())
11 .unwrap_or_else(|| path.to_owned());
12 let mut path = path.replace('\\', "/");
13 if path
14 .as_bytes()
15 .get(0..2)
16 .is_some_and(|bytes| bytes[0].is_ascii_alphabetic() && bytes[1] == b':')
17 {
18 path.replace_range(..1, &path[..1].to_ascii_lowercase());
19 }
20
21 let is_absolute = path.starts_with('/');
22 let mut parts = Vec::new();
23 for part in path.split('/') {
24 match part {
25 "" | "." => {}
26 ".." => {
27 if parts.last().is_some_and(|last| *last != "..") {
28 parts.pop();
29 } else {
30 parts.push(part);
31 }
32 }
33 _ => parts.push(part),
34 }
35 }
36
37 let normalized = parts.join("/");
38 if is_absolute && !normalized.is_empty() {
39 format!("/{normalized}")
40 } else {
41 normalized
42 }
43}
44
45#[cfg(test)]
46mod tests;