use std::path::{Component, Path, PathBuf};
pub(crate) fn clean(path: &Path) -> PathBuf {
let mut out = Vec::new();
for comp in path.components() {
match comp {
Component::CurDir => (),
Component::ParentDir => match out.last() {
Some(Component::RootDir) => (),
Some(Component::Normal(_)) => {
out.pop();
}
None
| Some(Component::CurDir)
| Some(Component::ParentDir)
| Some(Component::Prefix(_)) => out.push(comp),
},
comp => out.push(comp),
}
}
if !out.is_empty() {
out.iter().collect()
} else {
PathBuf::from(".")
}
}
pub(crate) fn diff_paths(
path: &Path,
base: &Path,
manifest_dir: Option<String>,
) -> Option<PathBuf> {
let path_is_absolute = path.is_absolute();
let base_is_absolute = base.is_absolute();
if path_is_absolute != base_is_absolute {
if path_is_absolute {
Some(PathBuf::from(path))
} else {
None
}
} else {
if base_is_absolute &&
let Some(manifest_dir) = manifest_dir &&
!base.starts_with(manifest_dir)
{
return Some(PathBuf::from(path));
}
let mut ita = path.components();
let mut itb = base.components();
let mut comps = vec![];
loop {
match (ita.next(), itb.next()) {
(None, None) => break,
(Some(a), None) => {
comps.push(a);
comps.extend(ita.by_ref());
break;
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(Component::CurDir)) => comps.push(a),
(Some(_), Some(Component::ParentDir)) => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
}
comps.push(a);
comps.extend(ita.by_ref());
break;
}
}
}
Some(comps.iter().map(|c| c.as_os_str()).collect())
}
}
#[test]
fn test_diff_paths() {
fn t(a: &str, b: &str) -> Option<PathBuf> {
diff_paths(Path::new(a), Path::new(b), None)
}
assert_eq!(t("/foo/bar", "/foo/bar/baz"), Some("../".into()));
assert_eq!(t("/foo/bar/baz", "/foo/bar"), Some("baz".into()));
assert_eq!(t("/foo/bar/quux", "/foo/bar/baz"), Some("../quux".into()));
assert_eq!(t("/foo/bar/baz", "/foo/bar/quux"), Some("../baz".into()));
assert_eq!(t("/foo/bar", "/foo/bar/quux"), Some("../".into()));
assert_eq!(t("/foo/bar", "baz"), Some("/foo/bar".into()));
assert_eq!(t("/foo/bar", "/baz"), Some("../foo/bar".into()));
assert_eq!(t("foo", "bar"), Some("../foo".into()));
if !cfg!(windows) {
assert_eq!(
diff_paths(
Path::new("/askama-bugs/b/templates/empty.txt"),
Path::new("/askama-bugs/a"),
Some("/askama-bugs/b".into()),
),
Some("/askama-bugs/b/templates/empty.txt".into()),
);
assert_eq!(
diff_paths(
Path::new("/askama-bugs/b/templates/empty.txt"),
Path::new("/askama-bugs/b"),
Some("/askama-bugs/b".into()),
),
Some("templates/empty.txt".into()),
);
} else {
assert_eq!(
diff_paths(
Path::new("C:/askama-bugs/b/templates/empty.txt"),
Path::new("C:/askama-bugs/a"),
Some("C:/askama-bugs/b".into()),
),
Some("C:/askama-bugs/b/templates/empty.txt".into()),
);
assert_eq!(
diff_paths(
Path::new("C:/askama-bugs/b/templates/empty.txt"),
Path::new("C:/askama-bugs/b"),
Some("C:/askama-bugs/b".into()),
),
Some("templates/empty.txt".into()),
);
}
}