pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf>
Expand description
A helper function to determine the relative path to path
from base
.
Returns None
if there is no relative path from base
to path
, that is,
base
and path
do not share a common ancestor. path
and base
must be
either both absolute or both relative; returns None
if one is relative and
the other absolute.
use std::path::Path;
use figment::util::diff_paths;
// Paths must be both relative or both absolute.
assert_eq!(diff_paths("/a/b/c", "b/c"), None);
assert_eq!(diff_paths("a/b/c", "/b/c"), None);
// The root/relative root is always a common ancestor.
assert_eq!(diff_paths("/a/b/c", "/b/c"), Some("../../a/b/c".into()));
assert_eq!(diff_paths("c/a", "b/c/a"), Some("../../../c/a".into()));
let bar = "/foo/bar";
let baz = "/foo/bar/baz";
let quux = "/foo/bar/quux";
assert_eq!(diff_paths(bar, baz), Some("../".into()));
assert_eq!(diff_paths(baz, bar), Some("baz".into()));
assert_eq!(diff_paths(quux, baz), Some("../quux".into()));
assert_eq!(diff_paths(baz, quux), Some("../baz".into()));
assert_eq!(diff_paths(bar, quux), Some("../".into()));
assert_eq!(diff_paths(baz, bar), Some("baz".into()));