From 72c767cfabe270128d25f65479bbd210c293655b Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sat, 10 Feb 2018 10:59:22 -0700 Subject: [PATCH] add PathDir::current_dir --- Cargo.toml | 2 +- src/dir.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 61a8679..f4406f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT OR Apache-2.0" name = "path_abs" readme = "README.md" repository = "https://github.com/vitiral/path_abs" -version = "0.3.15" +version = "0.3.16" [dependencies] std_prelude = "0.2.12" diff --git a/src/dir.rs b/src/dir.rs index 8531293..a964463 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -37,6 +37,25 @@ impl PathDir { PathDir::from_abs(abs) } + /// Returns the current working directory from the `env` as a `PathDir`. + /// + /// # Examples + /// ```rust + /// # extern crate path_abs; + /// use path_abs::PathDir; + /// # fn try_main() -> ::std::io::Result<()> { + /// let cwd = PathDir::current_dir()?; + /// # let env_cwd = ::std::fs::canonicalize(::std::env::current_dir()?)?; + /// # let cwd_ref: &::std::path::PathBuf = cwd.as_ref(); + /// # assert_eq!(cwd_ref, &env_cwd); + /// # Ok(()) } fn main() { try_main().unwrap() } + /// ``` + pub fn current_dir() -> Result { + let dir = ::std::env::current_dir() + .map_err(|err| Error::new(err, "getting current_dir", PathArc::new("$CWD")))?; + PathDir::new(dir) + } + /// Consume the `PathAbs` validating that the path is a directory and returning `PathDir`. The /// directory must exist or `io::Error` will be returned. /// @@ -69,6 +88,7 @@ impl PathDir { } } + #[inline(always)] /// Do the conversion _without checking_. ///