home-dir-0.1.0/.cargo_vcs_info.json0000644000000001121373746640300126430ustar00{ "git": { "sha1": "e2ce32dee3a7036e16f583f1ce44e5c7ad436b31" } } home-dir-0.1.0/.gitignore010064400007650000024000000000231373746240400134300ustar0000000000000000/target Cargo.lock home-dir-0.1.0/Cargo.toml0000644000000014751373746640300106560ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "home-dir" version = "0.1.0" authors = ["eulegang "] description = "expands home directories in a path" license = "MIT" repository = "https://github.com/eulegang/home-dir" [dependencies.nix] version = "0.18" [dependencies.thiserror] version = "1.0" home-dir-0.1.0/Cargo.toml.orig010064400007650000024000000005641373746621000143400ustar0000000000000000[package] name = "home-dir" version = "0.1.0" authors = ["eulegang "] edition = "2018" description = "expands home directories in a path" license = "MIT" repository = "https://github.com/eulegang/home-dir" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] thiserror = "1.0" nix = "0.18" home-dir-0.1.0/LICENSE010064400007650000024000000020511373746240400124500ustar0000000000000000MIT License Copyright (c) 2020 eulegang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. home-dir-0.1.0/README.md010064400007650000024000000000571373746240400127260ustar0000000000000000# home-dir expands tildes to home directories home-dir-0.1.0/src/lib.rs010064400007650000024000000061161373746240400133540ustar0000000000000000//! A enable expansion of tildes in paths //! //! built for unix, windows patch welcome //! //! - ~ expands using the HOME environmental variable. //! - if HOME does not exist, lookup current user in the user database //! //! - ~`user` will expand to the user's home directory from the user database //! use std::env; use std::path::{Component, Path, PathBuf}; use nix::unistd::{Uid, User}; #[cfg(test)] mod tests; #[derive(Debug, thiserror::Error)] #[non_exhaustive] /// Error while expanding path pub enum Error { /// The user being looked up is not in the user database #[error("the user does not have entry")] MissingEntry, } /// The expansion trait extension pub trait HomeDirExt { /// Expands a users home directory signified by a tilde. /// /// ``` /// # use home_dir::HomeDirExt; /// # use std::env::var; /// # use std::path::PathBuf; /// let mut path = PathBuf::from(var("HOME").unwrap()); /// path.push(".vimrc"); /// /// assert_eq!("~/.vimrc".expand_home().unwrap(), path, "current user path expansion"); /// /// # #[cfg(target_os = "macos")] /// # const ROOT_VIMRC: &'static str = "/var/root/.vimrc"; /// # #[cfg(target_os = "linux")] /// # const ROOT_VIMRC: &'static str = "/root/.vimrc"; /// assert_eq!("~root/.vimrc".expand_home().unwrap(), PathBuf::from(ROOT_VIMRC)); /// ``` fn expand_home(&self) -> Result; } impl HomeDirExt for Path { fn expand_home(&self) -> Result { let mut path = PathBuf::new(); let mut comps = self.components(); match comps.next() { Some(Component::Normal(os)) => { if let Some(s) = os.to_str() { match s { "~" => { let p = getenv() .ok_or(Error::MissingEntry) .or_else(|_| getent_current())?; path.push(p); } s if s.starts_with('~') => { path.push(getent(&s[1..])?); } s => path.push(s), } } else { path.push(os) } } Some(comp) => path.push(comp), None => return Ok(path), }; for comp in comps { path.push(comp); } Ok(path) } } impl HomeDirExt for T where T: AsRef, { fn expand_home(&self) -> Result { self.as_ref().expand_home() } } pub(crate) fn getent(name: &str) -> Result { let usr = User::from_name(name).or(Err(Error::MissingEntry))?; let usr = usr.ok_or_else(|| Error::MissingEntry)?; Ok(usr.dir) } pub(crate) fn getenv() -> Option { env::var("HOME").ok().map(Into::into) } pub(crate) fn getent_current() -> Result { let usr = User::from_uid(Uid::current()).or(Err(Error::MissingEntry))?; let usr = usr.ok_or_else(|| Error::MissingEntry)?; Ok(usr.dir) } home-dir-0.1.0/src/tests.rs010064400007650000024000000016311373746240400137450ustar0000000000000000use super::{getent, Error}; use crate::HomeDirExt; use std::path::PathBuf; #[test] fn test_root() { #[cfg(target_os = "macos")] const ROOT_DIR: &'static str = "/var/root"; #[cfg(target_os = "linux")] const ROOT_DIR: &'static str = "/root"; assert_eq!(getent("root").unwrap(), PathBuf::from(ROOT_DIR)); } #[test] fn test_expand_root() { #[cfg(target_os = "macos")] const ROOT_DIR: &'static str = "/var/root/foobar"; #[cfg(target_os = "linux")] const ROOT_DIR: &'static str = "/root/foobar"; assert_eq!( "~root/foobar".expand_home().unwrap(), PathBuf::from(ROOT_DIR) ); } #[test] fn test_expand_nonexpansion() { assert_eq!( "/etc/some.conf".expand_home().unwrap(), PathBuf::from("/etc/some.conf") ); } #[test] fn test_missing() { assert!(matches!( getent("_foobar_").unwrap_err(), Error::MissingEntry )); }