feat untildify
This commit is contained in:
parent
60a64b7bf4
commit
65fc61f7e4
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/target
|
||||||
|
Cargo.lock
|
||||||
|
.DS_Store
|
||||||
18
Cargo.toml
Normal file
18
Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "untildify"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
license = "MIT"
|
||||||
|
readme = "README.md"
|
||||||
|
authors = ["sathishsoundharajan <sathishwelcomez@ymail.com>"]
|
||||||
|
categories = ["development-tools"]
|
||||||
|
homepage = "https://github.com/sathishsoundharajan/untildify"
|
||||||
|
repository = "https://github.com/sathishsoundharajan/untildify"
|
||||||
|
keywords = ["tilde", "untildify", "~", "utility"]
|
||||||
|
documentation = "https://docs.rs/untildify"
|
||||||
|
|
||||||
|
[badges.maintenance]
|
||||||
|
status = "actively-developed"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
regex = "1.5.4"
|
||||||
60
src/lib.rs
Normal file
60
src/lib.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use regex::Regex;
|
||||||
|
use std::env;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn untildify(input_path: &str) -> String {
|
||||||
|
if input_path.is_empty() {
|
||||||
|
return String::from(input_path);
|
||||||
|
}
|
||||||
|
return match get_host_dir() {
|
||||||
|
Some(path) => {
|
||||||
|
let host_dir = path.to_str().unwrap();
|
||||||
|
let re = Regex::new(r"^~([/\w]+)").unwrap();
|
||||||
|
match re.captures(input_path) {
|
||||||
|
Some(captures) => {
|
||||||
|
return format!("{}{}", host_dir, &captures[1]);
|
||||||
|
}
|
||||||
|
None => String::from(input_path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => String::from(input_path),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(unix, target_os = "redox"))]
|
||||||
|
fn get_host_dir() -> Option<PathBuf> {
|
||||||
|
#[allow(deprecated)]
|
||||||
|
env::home_dir()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::untildify;
|
||||||
|
use std::env;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_returns_untildfyed_string() {
|
||||||
|
env::remove_var("HOME");
|
||||||
|
|
||||||
|
let home = Path::new("/User/Untildify");
|
||||||
|
env::set_var("HOME", home.as_os_str());
|
||||||
|
|
||||||
|
assert_eq!(untildify("~/Desktop"), "/User/Untildify/Desktop");
|
||||||
|
assert_eq!(untildify("~/a/b/c/d/e"), "/User/Untildify/a/b/c/d/e");
|
||||||
|
assert_eq!(untildify("~/"), "/User/Untildify/");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_returns_empty_string() {
|
||||||
|
env::remove_var("HOME");
|
||||||
|
|
||||||
|
let home = Path::new("/User/Untildify");
|
||||||
|
env::set_var("HOME", home.as_os_str());
|
||||||
|
|
||||||
|
assert_eq!(untildify("Desktop"), "Desktop");
|
||||||
|
assert_eq!(untildify(""), "");
|
||||||
|
assert_eq!(untildify("/"), "/");
|
||||||
|
assert_eq!(untildify("~/Desktop/~/Code"), "/User/Untildify/Desktop/");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user