aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-03-19 11:43:19 +0000
committerAkshay <[email protected]>2020-03-19 11:43:19 +0000
commitca0dc4be5174481b0fe3ce0483df00fe44b55b51 (patch)
treedbf88c0519277beeead11dd7dabf59cfcbf3eb4c
parent186611630fd258b6114cad41085be9a078fce4ba (diff)
add expand_tilde util fn
-rw-r--r--src/lib.rs18
1 files changed, 17 insertions, 1 deletions
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 @@
1pub use directories::ProjectDirs; 1pub use directories::{ProjectDirs, UserDirs};
2pub use serde::{de::DeserializeOwned, Serialize}; 2pub use serde::{de::DeserializeOwned, Serialize};
3pub use serde_json; 3pub use serde_json;
4pub use serde_yaml; 4pub use serde_yaml;
5pub use toml; 5pub use toml;
6 6
7use std::path::{Path, PathBuf};
8
7#[derive(Debug)] 9#[derive(Debug)]
8pub enum FondantError { 10pub enum FondantError {
9 InvalidHomeDir, 11 InvalidHomeDir,
@@ -15,6 +17,20 @@ pub enum FondantError {
15 FileOpenError, 17 FileOpenError,
16} 18}
17 19
20pub fn expand_tilde<P: AsRef<Path>>(path: P) -> PathBuf {
21 let p = path.as_ref();
22 if p.starts_with("~") {
23 if p == Path::new("~") {
24 return UserDirs::new().unwrap().home_dir().to_path_buf();
25 } else {
26 let mut h = UserDirs::new().unwrap().home_dir().to_path_buf();
27 h.push(p.strip_prefix("~/").unwrap());
28 return h;
29 }
30 }
31 return p.to_path_buf();
32}
33
18pub trait Configure: Serialize + DeserializeOwned + Default { 34pub trait Configure: Serialize + DeserializeOwned + Default {
19 fn load() -> Result<Self, FondantError>; 35 fn load() -> Result<Self, FondantError>;
20 fn store(&self) -> Result<(), FondantError>; 36 fn store(&self) -> Result<(), FondantError>;