Switch from failure to anyhow (#1851)

This commit switches all of `wasm-bindgen` from the `failure` crate to
`anyhow`. The `anyhow` crate should serve all the purposes that we
previously used `failure` for but has a few advantages:

* It's based on the standard `Error` trait rather than a custom `Fail`
  trait, improving ecosystem compatibility.
* We don't need a `#[derive(Fail)]`, which means that's less code to
  compile for `wasm-bindgen`. This notably helps the compile time of
  `web-sys` itself.
* Using `Result<()>` in `fn main` with `anyhow::Error` produces
  human-readable output, so we can use that natively.
This commit is contained in:
Alex Crichton
2019-11-04 11:35:28 -06:00
committed by GitHub
parent 913fdbc3da
commit 935f71afec
37 changed files with 131 additions and 239 deletions

View File

@@ -21,7 +21,7 @@ test = false
[build-dependencies]
env_logger = { version = "0.7.0", optional = true }
failure = "0.1.2"
anyhow = "1.0"
wasm-bindgen-webidl = { path = "../webidl", version = "=0.2.53" }
sourcefile = "0.1"

View File

@@ -1,26 +1,15 @@
use failure::{Fail, ResultExt};
use anyhow::{Context, Result};
use sourcefile::SourceFile;
use std::collections::HashSet;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{self, PathBuf};
use std::process::{self, Command};
use std::process::Command;
fn main() {
fn main() -> Result<()> {
#[cfg(feature = "env_logger")]
env_logger::init();
if let Err(e) = try_main() {
eprintln!("Error: {}", e);
for c in e.iter_causes() {
eprintln!(" caused by {}", c);
}
process::exit(1);
}
}
fn try_main() -> Result<(), failure::Error> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=webidls/enabled");
@@ -35,7 +24,7 @@ fn try_main() -> Result<(), failure::Error> {
println!("cargo:rerun-if-changed={}", path.display());
source = source
.add_file(&path)
.with_context(|_| format!("reading contents of file \"{}\"", path.display()))?;
.with_context(|| format!("reading contents of file \"{}\"", path.display()))?;
}
// Read our manifest, learn all `[feature]` directives with "toml parsing".
@@ -76,9 +65,9 @@ fn try_main() -> Result<(), failure::Error> {
let bindings = match wasm_bindgen_webidl::compile(&source.contents, allowed) {
Ok(bindings) => bindings,
Err(e) => match e.kind() {
wasm_bindgen_webidl::ErrorKind::ParsingWebIDLSourcePos(pos) => {
if let Some(pos) = source.resolve_offset(pos) {
Err(e) => {
if let Some(err) = e.downcast_ref::<wasm_bindgen_webidl::WebIDLParseError>() {
if let Some(pos) = source.resolve_offset(err.0) {
let ctx = format!(
"compiling WebIDL into wasm-bindgen bindings in file \
\"{}\", line {} column {}",
@@ -86,19 +75,13 @@ fn try_main() -> Result<(), failure::Error> {
pos.line + 1,
pos.col + 1
);
return Err(e.context(ctx).into());
return Err(e.context(ctx));
} else {
return Err(e
.context("compiling WebIDL into wasm-bindgen bindings")
.into());
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
}
_ => {
return Err(e
.context("compiling WebIDL into wasm-bindgen bindings")
.into());
}
},
return Err(e.context("compiling WebIDL into wasm-bindgen bindings"));
}
};
let out_dir = env::var("OUT_DIR").context("reading OUT_DIR environment variable")?;