1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 23:40:05 +08:00

add resolve_file_path

This commit is contained in:
2020-05-02 12:48:31 +08:00
parent dd0a754631
commit 2f7d7d238e

View File

@@ -17,6 +17,17 @@ pub fn get_home_str() -> Option<String> {
iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
}
pub fn resolve_file_path(path: &str) -> String {
let home_path = match get_home_str() {
Some(p) => p, None => return path.to_owned(),
};
match path {
"~" => home_path,
p if p.starts_with("~/") => home_path + &path.chars().skip(1).collect::<String>(),
p => p.to_owned(),
}
}
pub fn get_home_path() -> Option<PathBuf> {
Some(PathBuf::from(get_home_str()?))
}