From 80bcbb07c92ebe737376ecfb867917c8324d77d8 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 15 Feb 2022 19:16:27 +0000 Subject: improved link command if the destination path ends with a slash then it is considered a directory and the links will be made under that directory. otherwise all the links will have the same destination. --- src/utils.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index a7e945a..556bad3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -125,6 +125,13 @@ pub fn current_working_directory() -> PathBuf { std::env::current_dir().expect("Failed to obtain current working directory") } +pub fn path_ends_with_slash(path: impl AsRef) -> bool { + path.as_ref() + .to_str() + .map(|s| s.ends_with('/')) + .unwrap_or_default() +} + #[cfg(test)] mod tests { use super::*; @@ -153,4 +160,19 @@ mod tests { weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd) ); } + + #[test] + fn test_path_ends_with_slash() { + assert!(!path_ends_with_slash("")); + assert!(!path_ends_with_slash("/f1")); + assert!(!path_ends_with_slash("/f1/f2")); + assert!(!path_ends_with_slash("./f1/f2")); + assert!(!path_ends_with_slash("./f1/f2/../f3")); + + assert!(path_ends_with_slash("/")); + assert!(path_ends_with_slash("/f1/")); + assert!(path_ends_with_slash("f1/")); + assert!(path_ends_with_slash("f1/f2/")); + assert!(path_ends_with_slash("f1/f2/../f3/")); + } } -- cgit