|
1 | 1 | use crate::native::{types::FileData, utils::normalize_trait::Normalize}; |
2 | | -use std::path::{Path, PathBuf}; |
| 2 | +use std::path::{Component, Path, PathBuf}; |
3 | 3 |
|
4 | 4 | impl Normalize for Path { |
5 | 5 | fn to_normalized_string(&self) -> String { |
@@ -37,6 +37,83 @@ pub fn get_child_files<P: AsRef<Path>>(directory: P, files: Vec<FileData>) -> Ve |
37 | 37 | .collect() |
38 | 38 | } |
39 | 39 |
|
| 40 | +pub fn join_paths(base: &Path, relative: &str) -> PathBuf { |
| 41 | + let mut path = base.to_path_buf(); |
| 42 | + |
| 43 | + let relative_path = Path::new(relative); |
| 44 | + |
| 45 | + if relative_path.is_absolute() { |
| 46 | + return relative_path.to_path_buf(); |
| 47 | + } |
| 48 | + |
| 49 | + for component in relative_path.components() { |
| 50 | + match component { |
| 51 | + Component::ParentDir => { |
| 52 | + path.pop(); |
| 53 | + } |
| 54 | + Component::CurDir => {} |
| 55 | + Component::Normal(c) => path.push(c), |
| 56 | + _ => {} |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + path |
| 61 | +} |
| 62 | + |
| 63 | +/// Get the relative path from `from` to `to`. |
| 64 | +/// |
| 65 | +/// # Example |
| 66 | +/// ``` |
| 67 | +/// // From /a/b/c to /a/d/e returns "../../d/e" |
| 68 | +/// // From /a/b to /a/b/c returns "c" |
| 69 | +/// // From /a/b/c to /a/b/c returns "." |
| 70 | +/// ``` |
| 71 | +pub fn get_relative_path<P: AsRef<Path>>(from: P, to: P) -> String { |
| 72 | + let from_path = from.as_ref(); |
| 73 | + let to_path = to.as_ref(); |
| 74 | + |
| 75 | + // Normalize both paths |
| 76 | + let from_components: Vec<&std::ffi::OsStr> = from_path |
| 77 | + .components() |
| 78 | + .filter_map(|c| match c { |
| 79 | + Component::Normal(n) => Some(n.as_ref()), |
| 80 | + _ => None, |
| 81 | + }) |
| 82 | + .collect(); |
| 83 | + |
| 84 | + let to_components: Vec<&std::ffi::OsStr> = to_path |
| 85 | + .components() |
| 86 | + .filter_map(|c| match c { |
| 87 | + Component::Normal(n) => Some(n.as_ref()), |
| 88 | + _ => None, |
| 89 | + }) |
| 90 | + .collect(); |
| 91 | + |
| 92 | + // Find the common prefix length |
| 93 | + let common_len = from_components |
| 94 | + .iter() |
| 95 | + .zip(to_components.iter()) |
| 96 | + .take_while(|(a, b)| a == b) |
| 97 | + .count(); |
| 98 | + |
| 99 | + // Calculate how many parent directories we need |
| 100 | + let ups = from_components.len() - common_len; |
| 101 | + |
| 102 | + // Build the relative path |
| 103 | + let mut result = vec!["..".to_string(); ups]; |
| 104 | + result.extend( |
| 105 | + to_components[common_len..] |
| 106 | + .iter() |
| 107 | + .map(|c| c.to_string_lossy().to_string()), |
| 108 | + ); |
| 109 | + |
| 110 | + if result.is_empty() { |
| 111 | + ".".to_string() |
| 112 | + } else { |
| 113 | + result.join("/") |
| 114 | + } |
| 115 | +} |
| 116 | + |
40 | 117 | #[cfg(test)] |
41 | 118 | mod test { |
42 | 119 | use super::*; |
@@ -70,4 +147,32 @@ mod test { |
70 | 147 | let child_files = get_child_files(&directory, files); |
71 | 148 | assert_eq!(child_files, ["foo/bar", "foo/baz", "foo/child/bar",]); |
72 | 149 | } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn should_get_relative_path_sibling() { |
| 153 | + assert_eq!(get_relative_path("a/b/c", "a/d/e"), "../../d/e"); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn should_get_relative_path_child() { |
| 158 | + assert_eq!(get_relative_path("a/b", "a/b/c"), "c"); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn should_get_relative_path_parent() { |
| 163 | + assert_eq!(get_relative_path("a/b/c/d", "a/b"), "../.."); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn should_get_relative_path_same() { |
| 168 | + assert_eq!(get_relative_path("a/b/c", "a/b/c"), "."); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn should_get_relative_path_deeply_nested() { |
| 173 | + assert_eq!( |
| 174 | + get_relative_path("a/b/c/d/e", "a/x/y/z"), |
| 175 | + "../../../../x/y/z" |
| 176 | + ); |
| 177 | + } |
73 | 178 | } |
0 commit comments