| 1 | //! A enable expansion of tildes in paths |
| 2 | //! |
| 3 | //! built for unix, windows patch welcome |
| 4 | //! |
| 5 | //! - ~ expands using the HOME environmental variable. |
| 6 | //! - if HOME does not exist, lookup current user in the user database |
| 7 | //! |
| 8 | //! - ~`user` will expand to the user's home directory from the user database |
| 9 | //! |
| 10 | |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#, Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 13 | |
| 14 | use nixrust-analyzer cargo nix 0.31.3 crate/::unistdrust-analyzer cargo nix 0.31.3 unistd/::{Uidrust-analyzer cargo nix 0.31.3 unistd/Uid#, Userrust-analyzer cargo nix 0.31.3 unistd/User#}; |
| 15 | |
| 16 | #[cfg(test)] |
| 17 | mod testsrust-analyzer cargo home-dir 0.1.0 tests/; |
| 18 | |
| 19 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, thiserrorrust-analyzer cargo thiserror 1.0.69 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.69 Error:)] |
| 20 | #[non_exhaustive] |
| 21 | /// Error while expanding path |
| 22 | pub enum Errorrust-analyzer cargo home-dir 0.1.0 Error# { |
| 23 | /// The user being looked up is not in the user database |
| 24 | #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("the user does not have entry")] |
| 25 | MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#, |
| 26 | } |
| 27 | |
| 28 | /// The expansion trait extension |
| 29 | pub trait HomeDirExtrust-analyzer cargo home-dir 0.1.0 HomeDirExt# { |
| 30 | /// Expands a users home directory signified by a tilde. |
| 31 | /// |
| 32 | /// ``` |
| 33 | /// # use home_dir::HomeDirExt; |
| 34 | /// # use std::env::var; |
| 35 | /// # use std::path::PathBuf; |
| 36 | /// let mut path = PathBuf::from(var("HOME").unwrap()); |
| 37 | /// path.push(".vimrc"); |
| 38 | /// |
| 39 | /// assert_eq!("~/.vimrc".expand_home().unwrap(), path, "current user path expansion"); |
| 40 | /// |
| 41 | /// # #[cfg(target_os = "macos")] |
| 42 | /// # const ROOT_VIMRC: &'static str = "/var/root/.vimrc"; |
| 43 | /// # #[cfg(target_os = "linux")] |
| 44 | /// # const ROOT_VIMRC: &'static str = "/root/.vimrc"; |
| 45 | /// assert_eq!("~root/.vimrc".expand_home().unwrap(), PathBuf::from(ROOT_VIMRC)); |
| 46 | /// ``` |
| 47 | fn expand_homerust-analyzer cargo home-dir 0.1.0 HomeDirExt#expand_home().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Errorrust-analyzer cargo home-dir 0.1.0 Error#>; |
| 48 | } |
| 49 | |
| 50 | impl HomeDirExtrust-analyzer cargo home-dir 0.1.0 HomeDirExt# for Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path# { |
| 51 | fn expand_homerust-analyzer cargo home-dir 0.1.0 impl#[Path][HomeDirExt]expand_home().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Errorrust-analyzer cargo home-dir 0.1.0 Error#> { |
| 52 | let mut path = PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]new().(); |
| 53 | let mut comps = self.componentsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]components().(); |
| 54 | |
| 55 | match comps.nextrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[`Components<'a>`][Iterator]next().() { |
| 56 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::Normalrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#Normal#(os)) => { |
| 57 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = os.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().() { |
| 58 | match s { |
| 59 | "~" => { |
| 60 | let p = getenvrust-analyzer cargo home-dir 0.1.0 getenv().() |
| 61 | .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo home-dir 0.1.0 Error#::MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#) |
| 62 | .or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or_else().(|_| getent_currentrust-analyzer cargo home-dir 0.1.0 getent_current().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 63 | path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(p); |
| 64 | } |
| 65 | s if s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('~') => { |
| 66 | path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(getentrust-analyzer cargo home-dir 0.1.0 getent().(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 67 | } |
| 68 | s => path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(s), |
| 69 | } |
| 70 | } else { |
| 71 | path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(os) |
| 72 | } |
| 73 | } |
| 74 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comp) => path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(comp), |
| 75 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path), |
| 76 | }; |
| 77 | |
| 78 | for comp in comps { |
| 79 | path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(comp); |
| 80 | } |
| 81 | |
| 82 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl<T> HomeDirExtrust-analyzer cargo home-dir 0.1.0 HomeDirExt# for T |
| 87 | where |
| 88 | T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 89 | { |
| 90 | fn expand_homerust-analyzer cargo home-dir 0.1.0 impl#[T][HomeDirExt]expand_home().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Errorrust-analyzer cargo home-dir 0.1.0 Error#> { |
| 91 | self.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().expand_homerust-analyzer cargo home-dir 0.1.0 impl#[Path][HomeDirExt]expand_home().() |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub(craterust-analyzer cargo home-dir 0.1.0 crate/) fn getentrust-analyzer cargo home-dir 0.1.0 getent().(name: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Errorrust-analyzer cargo home-dir 0.1.0 Error#> { |
| 96 | let usr = Userrust-analyzer cargo nix 0.31.3 unistd/User#::from_namerust-analyzer cargo nix 0.31.3 unistd/impl#[User]from_name().(name).orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo home-dir 0.1.0 Error#::MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 97 | let usr = usr.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo home-dir 0.1.0 Error#::MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 98 | |
| 99 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(usr.dirrust-analyzer cargo nix 0.31.3 unistd/User#dir.) |
| 100 | } |
| 101 | |
| 102 | pub(craterust-analyzer cargo home-dir 0.1.0 crate/) fn getenvrust-analyzer cargo home-dir 0.1.0 getenv().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 103 | envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("HOME").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().) |
| 104 | } |
| 105 | |
| 106 | pub(craterust-analyzer cargo home-dir 0.1.0 crate/) fn getent_currentrust-analyzer cargo home-dir 0.1.0 getent_current().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Errorrust-analyzer cargo home-dir 0.1.0 Error#> { |
| 107 | let usr = Userrust-analyzer cargo nix 0.31.3 unistd/User#::from_uidrust-analyzer cargo nix 0.31.3 unistd/impl#[User]from_uid().(Uidrust-analyzer cargo nix 0.31.3 unistd/Uid#::currentrust-analyzer cargo nix 0.31.3 unistd/impl#[Uid]current().()).orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo home-dir 0.1.0 Error#::MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 108 | let usr = usr.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo home-dir 0.1.0 Error#::MissingEntryrust-analyzer cargo home-dir 0.1.0 Error#MissingEntry#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 109 | |
| 110 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(usr.dirrust-analyzer cargo nix 0.31.3 unistd/User#dir.) |
| 111 | } |