2017-06-05 16:25:13 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-10-07 09:40:44 +02:00
|
|
|
use std::io;
|
2017-06-05 16:25:13 +02:00
|
|
|
|
|
|
|
/// Get a relative path with respect to a certain base path.
|
|
|
|
/// See: https://stackoverflow.com/a/39343127/704831
|
|
|
|
pub fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
|
|
|
|
use std::path::Component;
|
|
|
|
|
|
|
|
if path.is_absolute() != base.is_absolute() {
|
|
|
|
if path.is_absolute() {
|
|
|
|
Some(PathBuf::from(path))
|
|
|
|
} else {
|
2017-10-14 19:57:15 +02:00
|
|
|
Some(PathBuf::from(base.join(path)))
|
2017-06-05 16:25:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut ita = path.components();
|
|
|
|
let mut itb = base.components();
|
|
|
|
let mut comps: Vec<Component> = 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(b)) if b == Component::CurDir => comps.push(a),
|
|
|
|
(Some(_), Some(b)) if b == 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())
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
|
|
|
|
let path_buf = path.canonicalize()?;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2017-10-12 08:01:51 +02:00
|
|
|
let path_buf = Path::new(path_buf.as_path().to_string_lossy().trim_left_matches(
|
|
|
|
r"\\?\",
|
|
|
|
)).to_path_buf();
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
Ok(path_buf)
|
|
|
|
}
|