Skip to content

Commit 87a0f3e

Browse files
Merge #73
73: Move from failure to anyhow r=therealprof a=ZeroErrors ## Changes - Move from `failure` to `anyhow` for error handling ## Description As of `failure 0.1.8` it has been deprecated ([rust-lang-deprecated/failure#347](rust-lang-deprecated/failure#347)) in favor of using standard rust errors and it is recommended to do that via libraries such as [`anyhow`](https://github.com/dtolnay/anyhow) or [`thiserror`](https://github.com/dtolnay/thiserror). I chose to go with `anyhow` as its the simplest to convert over to and also since `cargo-binutils` isn't expected to be used as a library I see no need for more detailed error types (See: [dtolnay/anyhow#comparison-to-thiserror](https://github.com/dtolnay/anyhow#comparison-to-thiserror)). --- **Note:** `failure` isn't fully removed yet as the `rustc-cfg` dependency uses `failure`. I have opened a PR on that repo to hopefully get that changed (japaric/rustc-cfg#12) r? @therealprof Co-authored-by: Zero <nick@zero.dev>
2 parents 7db038e + a417523 commit 87a0f3e

5 files changed

Lines changed: 18 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
- Changed help output to more closely reflect the help command of `cargo` subcommands
2828
- Removed `walkdir` dependency by using expected path to tool executable
2929
- Updated `cargo_metadata 0.9 -> 0.10`
30+
- Replaced `failure` dependency with [`anyhow`](https://github.com/dtolnay/anyhow)
3031

3132
## [v0.2.0] - 2020-04-11
3233

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ version = "0.2.0"
1414
[dependencies]
1515
cargo_metadata = "0.10"
1616
clap = "2.33"
17-
failure = "0.1"
1817
regex = "1.3"
1918
rustc-cfg = "0.4"
2019
rustc-demangle = "0.1"
2120
rustc_version = "0.2"
2221
serde = "1.0"
2322
toml = "0.5"
23+
anyhow = "1.0"

src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use std::io::{self, BufReader, Write};
44
use std::path::{Component, Path};
55
use std::process::{Command, Stdio};
6-
use std::{env, str};
6+
use std::str;
77

8+
use anyhow::{bail, Result};
89
use cargo_metadata::{Artifact, CargoOpt, Message, Metadata, MetadataCommand};
910
use clap::{App, AppSettings, Arg};
10-
use failure::bail;
1111
use rustc_cfg::Cfg;
1212

1313
pub use tool::Tool;
@@ -36,7 +36,7 @@ fn search<'p>(path: &'p Path, file: &str) -> Option<&'p Path> {
3636
path.ancestors().find(|dir| dir.join(file).exists())
3737
}
3838

39-
fn parse<T>(path: &Path) -> Result<T, failure::Error>
39+
fn parse<T>(path: &Path) -> Result<T>
4040
where
4141
T: for<'de> serde::Deserialize<'de>,
4242
{
@@ -52,7 +52,7 @@ where
5252
impl Context {
5353
/* Constructors */
5454
/// Get a context structure from a built artifact.
55-
fn from_artifact(metadata: Metadata, artifact: &Artifact) -> Result<Self, failure::Error> {
55+
fn from_artifact(metadata: Metadata, artifact: &Artifact) -> Result<Self> {
5656
// Currently there is no clean way to get the target triple from cargo so we can only make
5757
// an approximation, we do this by extracting the target triple from the artifacts path.
5858
// For more info on the path structure see: https://doc.rust-lang.org/cargo/guide/build-cache.html
@@ -81,7 +81,7 @@ impl Context {
8181

8282
/// Get a context structure from a provided target flag, used when cargo
8383
/// was not used to build the binary.
84-
fn from_flag(metadata: Metadata, target_flag: Option<&str>) -> Result<Self, failure::Error> {
84+
fn from_flag(metadata: Metadata, target_flag: Option<&str>) -> Result<Self> {
8585
let meta = rustc_version::version_meta()?;
8686
let host = meta.host;
8787
let host_target_name = host;
@@ -106,8 +106,8 @@ impl Context {
106106
Self::from_target_name(target_name)
107107
}
108108

109-
fn from_target_name(target_name: &str) -> Result<Self, failure::Error> {
110-
let cfg = Cfg::of(target_name)?;
109+
fn from_target_name(target_name: &str) -> Result<Self> {
110+
let cfg = Cfg::of(target_name).map_err(|e| e.compat())?;
111111

112112
Ok(Context {
113113
cfg,
@@ -167,9 +167,7 @@ impl<'a> BuildType<'a> {
167167
}
168168
}
169169

170-
fn determine_artifact(
171-
matches: &clap::ArgMatches,
172-
) -> Result<(Metadata, Option<Artifact>), failure::Error> {
170+
fn determine_artifact(matches: &clap::ArgMatches) -> Result<(Metadata, Option<Artifact>)> {
173171
let verbose = matches.is_present("verbose");
174172
let target_flag = matches.value_of("target");
175173

@@ -281,7 +279,7 @@ fn determine_artifact(
281279
Ok((metadata, wanted_artifact))
282280
}
283281

284-
pub fn run(tool: Tool, examples: Option<&str>) -> Result<i32, failure::Error> {
282+
pub fn run(tool: Tool, examples: Option<&str>) -> Result<i32> {
285283
let name = tool.name();
286284
let about = format!(
287285
"Proxy for the `llvm-{}` tool shipped with the Rust toolchain.",

src/rustc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
use std::env;
12
use std::path::PathBuf;
23
use std::process::Command;
34

4-
use failure::Error;
5-
use std::env;
5+
use anyhow::Result;
66

7-
pub fn sysroot() -> Result<String, Error> {
7+
pub fn sysroot() -> Result<String> {
88
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
99
let output = Command::new(rustc).arg("--print").arg("sysroot").output()?;
1010
// Note: We must trim() to remove the `\n` from the end of stdout
1111
Ok(String::from_utf8(output.stdout)?.trim().to_owned())
1212
}
1313

1414
// See: https://github.com/rust-lang/rust/blob/564758c4c329e89722454dd2fbb35f1ac0b8b47c/src/bootstrap/dist.rs#L2334-L2341
15-
pub fn rustlib() -> Result<PathBuf, Error> {
15+
pub fn rustlib() -> Result<PathBuf> {
1616
let sysroot = sysroot()?;
1717
let mut pathbuf = PathBuf::from(sysroot);
1818
pathbuf.push("lib");

src/tool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33
use std::process::Command;
44
use std::{env, process};
55

6-
use failure::Error;
6+
use anyhow::Result;
77

88
use crate::rustc::rustlib;
99

@@ -38,11 +38,11 @@ impl Tool {
3838
pub fn exe(self) -> String {
3939
match self {
4040
Tool::Lld => format!("rust-lld{}", EXE_SUFFIX),
41-
_ => format!("llvm-{}{}", self.name(), EXE_SUFFIX)
41+
_ => format!("llvm-{}{}", self.name(), EXE_SUFFIX),
4242
}
4343
}
4444

45-
pub fn path(self) -> Result<PathBuf, Error> {
45+
pub fn path(self) -> Result<PathBuf> {
4646
let mut path = rustlib()?;
4747
path.push(self.exe());
4848
Ok(path)

0 commit comments

Comments
 (0)