pathdiff

Function diff_utf8_paths

Source
pub fn diff_utf8_paths<P, B>(path: P, base: B) -> Option<Utf8PathBuf>
where P: AsRef<Utf8Path>, B: AsRef<Utf8Path>,
Expand description

Construct a relative UTF-8 path from a provided base directory path to the provided path.

Requires the camino feature.

use camino::*;
use pathdiff::diff_utf8_paths;

assert_eq!(diff_utf8_paths("/foo/bar",      "/foo/bar/baz"),  Some("../".into()));
assert_eq!(diff_utf8_paths("/foo/bar/baz",  "/foo/bar"),      Some("baz".into()));
assert_eq!(diff_utf8_paths("/foo/bar/quux", "/foo/bar/baz"),  Some("../quux".into()));
assert_eq!(diff_utf8_paths("/foo/bar/baz",  "/foo/bar/quux"), Some("../baz".into()));
assert_eq!(diff_utf8_paths("/foo/bar",      "/foo/bar/quux"), Some("../".into()));

assert_eq!(diff_utf8_paths("/foo/bar",      "baz"),           Some("/foo/bar".into()));
assert_eq!(diff_utf8_paths("/foo/bar",      "/baz"),          Some("../foo/bar".into()));
assert_eq!(diff_utf8_paths("foo",           "bar"),           Some("../foo".into()));

assert_eq!(
    diff_utf8_paths(&"/foo/bar/baz", "/foo/bar".to_string()),
    Some("baz".into())
);
assert_eq!(
    diff_utf8_paths(Utf8Path::new("/foo/bar/baz"), Utf8Path::new("/foo/bar").to_path_buf()),
    Some("baz".into())
);