diff options
| author | hozan23 <hozan23@proton.me> | 2023-11-28 22:41:33 +0300 | 
|---|---|---|
| committer | hozan23 <hozan23@proton.me> | 2023-11-28 22:41:33 +0300 | 
| commit | 98a1de91a2dae06323558422c239e5a45fc86e7b (patch) | |
| tree | 38c640248824fcb3b4ca5ba12df47c13ef26ccda /core/src/util/path.rs | |
| parent | ca2a5f8bbb6983d9555abd10eaaf86950b794957 (diff) | |
implement TLS for inbound and outbound connections
Diffstat (limited to 'core/src/util/path.rs')
| -rw-r--r-- | core/src/util/path.rs | 39 | 
1 files changed, 39 insertions, 0 deletions
| diff --git a/core/src/util/path.rs b/core/src/util/path.rs new file mode 100644 index 0000000..2cd900a --- /dev/null +++ b/core/src/util/path.rs @@ -0,0 +1,39 @@ +use std::path::PathBuf; + +use crate::{error::Error, Result}; + +/// Returns the user's home directory as a `PathBuf`. +#[allow(dead_code)] +pub fn home_dir() -> Result<PathBuf> { +    dirs::home_dir().ok_or(Error::PathNotFound("Home dir not found")) +} + +/// Expands a tilde (~) in a path and returns the expanded `PathBuf`. +#[allow(dead_code)] +pub fn tilde_expand(path: &str) -> Result<PathBuf> { +    match path { +        "~" => home_dir(), +        p if p.starts_with("~/") => Ok(home_dir()?.join(&path[2..])), +        _ => Ok(PathBuf::from(path)), +    } +} + +#[cfg(test)] +mod tests { +    use super::*; + +    #[test] +    fn test_tilde_expand() { +        let path = "~/src"; +        let expanded_path = dirs::home_dir().unwrap().join("src"); +        assert_eq!(tilde_expand(path).unwrap(), expanded_path); + +        let path = "~"; +        let expanded_path = dirs::home_dir().unwrap(); +        assert_eq!(tilde_expand(path).unwrap(), expanded_path); + +        let path = ""; +        let expanded_path = PathBuf::from(""); +        assert_eq!(tilde_expand(path).unwrap(), expanded_path); +    } +} | 
