From 921006d9321948b29df30c3785b16a81f622f451 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Wed, 3 Jun 2026 11:30:31 +0300 Subject: Make normalize_to_root return best-effort The previous verion failed if recursive `root.parent()` reached an error, but thinking about it, if the two paths have different starting points (like Windows drives), then the desirable function would be to return the absolute target path as-is. Naturally, the current funtion gives a wrong output if the supplied arguments are not absolute, but that shall be enforced using a future test. --- src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 9e9afcf..a78a767 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,13 +26,18 @@ pub fn normalize_path(root: &Path, origin: &Path, input: &Path) -> Result PathBuf { - match target_path.strip_prefix(root) { - Ok(normalized_path) => normalized_path.to_path_buf(), - Err(_) => { - let root_parent = root.parent().expect("failed cuz target_path is not absolute"); - PathBuf::from("..").join(normalize_to_root(target_path, root_parent)) - }, +fn normalize_to_root(target: &Path, mut root: &Path) -> PathBuf { + let mut prefix = PathBuf::new(); + loop { + if let Ok(suffix) = target.strip_prefix(root) { + return prefix.join(suffix); + } + if let Some(new_root) = root.parent() { + prefix.push(".."); + root = new_root; + } else { + return target.to_path_buf(); + } } } -- cgit v1.3.1