From ca0dc4be5174481b0fe3ce0483df00fe44b55b51 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 19 Mar 2020 17:13:19 +0530 Subject: add expand_tilde util fn --- src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6c03510..5da5bdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,11 @@ -pub use directories::ProjectDirs; +pub use directories::{ProjectDirs, UserDirs}; pub use serde::{de::DeserializeOwned, Serialize}; pub use serde_json; pub use serde_yaml; pub use toml; +use std::path::{Path, PathBuf}; + #[derive(Debug)] pub enum FondantError { InvalidHomeDir, @@ -15,6 +17,20 @@ pub enum FondantError { FileOpenError, } +pub fn expand_tilde>(path: P) -> PathBuf { + let p = path.as_ref(); + if p.starts_with("~") { + if p == Path::new("~") { + return UserDirs::new().unwrap().home_dir().to_path_buf(); + } else { + let mut h = UserDirs::new().unwrap().home_dir().to_path_buf(); + h.push(p.strip_prefix("~/").unwrap()); + return h; + } + } + return p.to_path_buf(); +} + pub trait Configure: Serialize + DeserializeOwned + Default { fn load() -> Result; fn store(&self) -> Result<(), FondantError>; -- cgit v1.2.3