Skip to content

Commit

Permalink
Merge #73
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and ZeroErrors committed May 20, 2020
2 parents 7db038e + a417523 commit 87a0f3e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Changed help output to more closely reflect the help command of `cargo` subcommands
- Removed `walkdir` dependency by using expected path to tool executable
- Updated `cargo_metadata 0.9 -> 0.10`
- Replaced `failure` dependency with [`anyhow`](https://github.com/dtolnay/anyhow)

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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ version = "0.2.0"
[dependencies]
cargo_metadata = "0.10"
clap = "2.33"
failure = "0.1"
regex = "1.3"
rustc-cfg = "0.4"
rustc-demangle = "0.1"
rustc_version = "0.2"
serde = "1.0"
toml = "0.5"
anyhow = "1.0"
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use std::io::{self, BufReader, Write};
use std::path::{Component, Path};
use std::process::{Command, Stdio};
use std::{env, str};
use std::str;

use anyhow::{bail, Result};
use cargo_metadata::{Artifact, CargoOpt, Message, Metadata, MetadataCommand};
use clap::{App, AppSettings, Arg};
use failure::bail;
use rustc_cfg::Cfg;

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

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

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

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

Ok(Context {
cfg,
Expand Down Expand Up @@ -167,9 +167,7 @@ impl<'a> BuildType<'a> {
}
}

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

Expand Down Expand Up @@ -281,7 +279,7 @@ fn determine_artifact(
Ok((metadata, wanted_artifact))
}

pub fn run(tool: Tool, examples: Option<&str>) -> Result<i32, failure::Error> {
pub fn run(tool: Tool, examples: Option<&str>) -> Result<i32> {
let name = tool.name();
let about = format!(
"Proxy for the `llvm-{}` tool shipped with the Rust toolchain.",
Expand Down
8 changes: 4 additions & 4 deletions src/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

use failure::Error;
use std::env;
use anyhow::Result;

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

// See: https://github.com/rust-lang/rust/blob/564758c4c329e89722454dd2fbb35f1ac0b8b47c/src/bootstrap/dist.rs#L2334-L2341
pub fn rustlib() -> Result<PathBuf, Error> {
pub fn rustlib() -> Result<PathBuf> {
let sysroot = sysroot()?;
let mut pathbuf = PathBuf::from(sysroot);
pathbuf.push("lib");
Expand Down
6 changes: 3 additions & 3 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use std::process::Command;
use std::{env, process};

use failure::Error;
use anyhow::Result;

use crate::rustc::rustlib;

Expand Down Expand Up @@ -38,11 +38,11 @@ impl Tool {
pub fn exe(self) -> String {
match self {
Tool::Lld => format!("rust-lld{}", EXE_SUFFIX),
_ => format!("llvm-{}{}", self.name(), EXE_SUFFIX)
_ => format!("llvm-{}{}", self.name(), EXE_SUFFIX),
}
}

pub fn path(self) -> Result<PathBuf, Error> {
pub fn path(self) -> Result<PathBuf> {
let mut path = rustlib()?;
path.push(self.exe());
Ok(path)
Expand Down

0 comments on commit 87a0f3e

Please sign in to comment.