Skip to main content

relative_to

Function relative_to 

Source
pub fn relative_to(root: &Path, path: &str) -> Option<String>
Expand description

path relative to root, when it is inside it: absolute or relative to root, with . and .. resolved lexically, so a file that does not exist yet — the commonest thing a tool writes — is placed as surely as one that does. None outside the checkout.

use std::path::Path;
use majordomus_cli::guard::relative_to;

let root = Path::new("/work/repo");
assert_eq!(relative_to(root, "/work/repo/lib/a.sh").as_deref(), Some("lib/a.sh"));
assert_eq!(relative_to(root, "lib/new.sh").as_deref(), Some("lib/new.sh"));
assert_eq!(relative_to(root, "/work/repo/docs/../lib/a.sh").as_deref(), Some("lib/a.sh"));
assert_eq!(relative_to(root, "/work/other/a.sh"), None);
assert_eq!(relative_to(root, "/work/repo/../repo-wt/a.sh"), None);