diff options
author | Haibo Huang <hhb@google.com> | 2020-07-15 19:00:35 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-07-15 19:00:35 +0000 |
commit | 6abc81d54074f9dac08db0a0bec8181f3301f5ee (patch) | |
tree | 6a70b20b99ff51d93896380d942ea1628c1ad441 | |
parent | 53ec0621d03b1564a7a06f3baa955e9e0175f3e9 (diff) | |
parent | 74fd0b41ebeeb69c066cab7ea5e2837b5d8754fd (diff) | |
download | which-6abc81d54074f9dac08db0a0bec8181f3301f5ee.tar.gz |
Upgrade rust/crates/which to 4.0.1 am: 8b6eb89f36 am: ed9bfb672b am: e71d767a0f am: 74fd0b41eb
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/which/+/1360861
Change-Id: I26e2d3aff2f3172dd4aa85f22ae988fcc033cd78
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 7 | ||||
-rw-r--r-- | Cargo.toml | 14 | ||||
-rw-r--r-- | Cargo.toml.orig | 12 | ||||
-rw-r--r-- | METADATA | 6 | ||||
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/checker.rs | 8 | ||||
-rw-r--r-- | src/error.rs | 93 | ||||
-rw-r--r-- | src/finder.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 20 | ||||
-rw-r--r-- | tests/basic.rs | 4 |
11 files changed, 41 insertions, 136 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 98aa420..eb684ab 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "1ffb2479652982ab8aaa1706bba6de5281a7ba22" + "sha1": "e5039c15d1202a59b9e35d628263134cf92514fe" } } @@ -8,8 +8,15 @@ rust_library { edition: "2015", rustlibs: [ "liblibc", + "libthiserror", ], } // dependent_library ["feature_list"] // libc-0.2.72 "default,std" +// proc-macro2-1.0.18 "default,proc-macro" +// quote-1.0.7 "default,proc-macro" +// syn-1.0.34 "clone-impls,default,derive,parsing,printing,proc-macro,quote" +// thiserror-1.0.20 +// thiserror-impl-1.0.20 +// unicode-xid-0.2.1 "default" @@ -12,7 +12,7 @@ [package] name = "which" -version = "3.1.1" +version = "4.0.1" authors = ["Harry Fei <tiziyuanfang@gmail.com>"] description = "A Rust equivalent of Unix command \"which\". Locate installed executable in cross platforms." documentation = "https://docs.rs/which/" @@ -21,16 +21,10 @@ keywords = ["which", "which-rs", "unix", "command"] categories = ["os", "filesystem"] license = "MIT" repository = "https://github.com/harryfei/which-rs.git" -[dependencies.failure] -version = "0.1.7" -features = ["std"] -optional = true -default-features = false - [dependencies.libc] version = "0.2.65" + +[dependencies.thiserror] +version = "1.0" [dev-dependencies.tempdir] version = "0.3.7" - -[features] -default = ["failure"] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 2c353c8..34399d6 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "which" -version = "3.1.1" +version = "4.0.1" authors = ["Harry Fei <tiziyuanfang@gmail.com>"] repository = "https://github.com/harryfei/which-rs.git" documentation = "https://docs.rs/which/" @@ -12,15 +12,7 @@ keywords = ["which", "which-rs", "unix", "command"] [dependencies] libc = "0.2.65" - -[dependencies.failure] -version = "0.1.7" -default-features = false -features = ["std"] -optional = true +thiserror = "1.0" [dev-dependencies] tempdir = "0.3.7" - -[features] -default = ["failure"] @@ -9,11 +9,11 @@ third_party { type: GIT value: "https://github.com/harryfei/which-rs.git" } - version: "3.1.1" + version: "4.0.1" license_type: NOTICE last_upgrade_date { year: 2020 - month: 3 - day: 31 + month: 7 + day: 10 } } @@ -22,13 +22,6 @@ let result = which::which("rustc").unwrap(); assert_eq!(result, PathBuf::from("/usr/bin/rustc")); ``` -## Errors - -By default this crate exposes a [`failure`] based error. This is optional, disable the default -features to get an error type implementing the standard library `Error` trait. - -[`failure`]: https://crates.io/crates/failure - ## Documentation The documentation is [available online](https://docs.rs/which/). diff --git a/src/checker.rs b/src/checker.rs index 6021711..817ae2f 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -39,6 +39,14 @@ impl ExistedChecker { } impl Checker for ExistedChecker { + #[cfg(target_os="windows")] + fn is_valid(&self, path: &Path) -> bool { + fs::symlink_metadata(path) + .map(|metadata| metadata.is_file()) + .unwrap_or(false) + } + + #[cfg(not(target_os="windows"))] fn is_valid(&self, path: &Path) -> bool { fs::metadata(path) .map(|metadata| metadata.is_file()) diff --git a/src/error.rs b/src/error.rs index c75fe4c..708c884 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,92 +1,17 @@ -#[cfg(feature = "failure")] -use failure::{Backtrace, Context, Fail}; -use std; -use std::fmt::{self, Display}; +use thiserror; -#[derive(Debug)] -pub struct Error { - #[cfg(feature = "failure")] - inner: Context<ErrorKind>, - #[cfg(not(feature = "failure"))] - inner: ErrorKind, -} +pub type Result<T> = std::result::Result<T, Error>; -// To suppress false positives from cargo-clippy -#[cfg_attr(feature = "cargo-clippy", allow(empty_line_after_outer_attr))] -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum ErrorKind { +#[derive(thiserror::Error, Copy, Clone, Eq, PartialEq, Debug)] +pub enum Error { + #[error("bad absolute path")] BadAbsolutePath, + #[error("bad relative path")] BadRelativePath, + #[error("cannot find binary path")] CannotFindBinaryPath, + #[error("cannot get current directory")] CannotGetCurrentDir, + #[error("cannot canonicalize path")] CannotCanonicalize, } - -#[cfg(feature = "failure")] -impl Fail for ErrorKind {} - -impl Display for ErrorKind { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let display = match *self { - ErrorKind::BadAbsolutePath => "Bad absolute path", - ErrorKind::BadRelativePath => "Bad relative path", - ErrorKind::CannotFindBinaryPath => "Cannot find binary path", - ErrorKind::CannotGetCurrentDir => "Cannot get current directory", - ErrorKind::CannotCanonicalize => "Cannot canonicalize path", - }; - f.write_str(display) - } -} - -#[cfg(feature = "failure")] -impl Fail for Error { - fn cause(&self) -> Option<&dyn Fail> { - self.inner.cause() - } - - fn backtrace(&self) -> Option<&Backtrace> { - self.inner.backtrace() - } -} - -#[cfg(not(feature = "failure"))] -impl std::error::Error for Error {} - -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - Display::fmt(&self.inner, f) - } -} - -impl Error { - pub fn kind(&self) -> ErrorKind { - #[cfg(feature = "failure")] - { - *self.inner.get_context() - } - #[cfg(not(feature = "failure"))] - { - self.inner - } - } -} - -impl From<ErrorKind> for Error { - fn from(kind: ErrorKind) -> Error { - Error { - #[cfg(feature = "failure")] - inner: Context::new(kind), - #[cfg(not(feature = "failure"))] - inner: kind, - } - } -} - -#[cfg(feature = "failure")] -impl From<Context<ErrorKind>> for Error { - fn from(inner: Context<ErrorKind>) -> Error { - Error { inner } - } -} - -pub type Result<T> = std::result::Result<T, Error>; diff --git a/src/finder.rs b/src/finder.rs index 2519aa8..d23cbaa 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -66,7 +66,7 @@ impl Finder { Box::new(candidates) } else { // Search binary in PATHs(defined in environment variable). - let p = paths.ok_or(ErrorKind::CannotFindBinaryPath)?; + let p = paths.ok_or(Error::CannotFindBinaryPath)?; let paths: Vec<_> = env::split_paths(&p).collect(); let candidates = Self::path_search_candidates(path, paths).into_iter(); @@ -82,7 +82,7 @@ impl Finder { } // can't find any binary - return Err(ErrorKind::CannotFindBinaryPath.into()); + Err(Error::CannotFindBinaryPath) } fn cwd_search_candidates<C>(binary_name: PathBuf, cwd: C) -> impl IntoIterator<Item = PathBuf> @@ -13,12 +13,9 @@ //! //! ``` -#[cfg(feature = "failure")] -extern crate failure; extern crate libc; +extern crate thiserror; -#[cfg(feature = "failure")] -use failure::ResultExt; mod checker; mod error; mod finder; @@ -59,10 +56,7 @@ use finder::Finder; /// /// ``` pub fn which<T: AsRef<OsStr>>(binary_name: T) -> Result<path::PathBuf> { - #[cfg(feature = "failure")] - let cwd = env::current_dir().context(ErrorKind::CannotGetCurrentDir)?; - #[cfg(not(feature = "failure"))] - let cwd = env::current_dir().map_err(|_| ErrorKind::CannotGetCurrentDir)?; + let cwd = env::current_dir().map_err(|_| Error::CannotGetCurrentDir)?; which_in(binary_name, env::var_os("PATH"), &cwd) } @@ -194,10 +188,7 @@ impl CanonicalPath { /// This calls `which` and `Path::canonicalize` and maps the result into a `CanonicalPath`. pub fn new<T: AsRef<OsStr>>(binary_name: T) -> Result<CanonicalPath> { which(binary_name) - .and_then(|p| { - p.canonicalize() - .map_err(|_| ErrorKind::CannotCanonicalize.into()) - }) + .and_then(|p| p.canonicalize().map_err(|_| Error::CannotCanonicalize)) .map(|inner| CanonicalPath { inner }) } @@ -212,10 +203,7 @@ impl CanonicalPath { V: AsRef<path::Path>, { which_in(binary_name, paths, cwd) - .and_then(|p| { - p.canonicalize() - .map_err(|_| ErrorKind::CannotCanonicalize.into()) - }) + .and_then(|p| p.canonicalize().map_err(|_| Error::CannotCanonicalize)) .map(|inner| CanonicalPath { inner }) } diff --git a/tests/basic.rs b/tests/basic.rs index b6ba30d..24a700d 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -293,12 +293,10 @@ fn test_which_relative_non_executable() { } #[test] -#[cfg(feature = "failure")] fn test_failure() { let f = TestFixture::new(); - let run = || -> std::result::Result<PathBuf, failure::Error> { - // Test the conversion to failure + let run = || -> which::Result<PathBuf> { let p = _which(&f, "./b/bin")?; Ok(p.into_path_buf()) }; |