mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
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:
@ -1,6 +1,6 @@
|
||||
use crate::webidl::{NonstandardIncoming, NonstandardOutgoing};
|
||||
use crate::webidl::{NonstandardWebidlSection, WasmBindgenAux};
|
||||
use failure::Error;
|
||||
use anyhow::Error;
|
||||
use std::collections::HashSet;
|
||||
use walrus::Module;
|
||||
use wasm_bindgen_anyref_xform::Context;
|
||||
|
@ -11,7 +11,7 @@
|
||||
//! functions.
|
||||
|
||||
use crate::descriptor::{Closure, Descriptor};
|
||||
use failure::Error;
|
||||
use anyhow::Error;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use walrus::ImportId;
|
||||
|
@ -8,7 +8,7 @@ use crate::js::incoming;
|
||||
use crate::js::outgoing;
|
||||
use crate::js::Context;
|
||||
use crate::webidl::Binding;
|
||||
use failure::{bail, Error};
|
||||
use anyhow::{bail, Error};
|
||||
use std::collections::HashSet;
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
|
@ -8,7 +8,7 @@ use crate::descriptor::VectorKind;
|
||||
use crate::js::binding::JsBuilder;
|
||||
use crate::js::Context;
|
||||
use crate::webidl::NonstandardIncoming;
|
||||
use failure::{bail, Error};
|
||||
use anyhow::{bail, Error};
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
pub struct Incoming<'a, 'b> {
|
||||
|
@ -5,7 +5,7 @@ use crate::webidl::{AuxEnum, AuxExport, AuxExportKind, AuxImport, AuxStruct};
|
||||
use crate::webidl::{AuxValue, Binding};
|
||||
use crate::webidl::{JsImport, JsImportName, NonstandardWebidlSection, WasmBindgenAux};
|
||||
use crate::{Bindgen, EncodeInto, OutputMode};
|
||||
use failure::{bail, Error, ResultExt};
|
||||
use anyhow::{bail, Context as _, Error};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
@ -1888,7 +1888,7 @@ impl<'a> Context<'a> {
|
||||
check_duplicated_getter_and_setter_names(&pairs)?;
|
||||
for (id, export) in pairs {
|
||||
self.generate_export(*id, export, bindings)
|
||||
.with_context(|_| {
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"failed to generate bindings for Rust export `{}`",
|
||||
export.debug_name,
|
||||
@ -1901,7 +1901,7 @@ impl<'a> Context<'a> {
|
||||
let catch = aux.imports_with_catch.contains(&id);
|
||||
let assert_no_shim = aux.imports_with_assert_no_shim.contains(&id);
|
||||
self.generate_import(*id, import, bindings, variadic, catch, assert_no_shim)
|
||||
.with_context(|_| {
|
||||
.with_context(|| {
|
||||
format!("failed to generate bindings for import `{:?}`", import,)
|
||||
})?;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use crate::descriptor::VectorKind;
|
||||
use crate::js::binding::JsBuilder;
|
||||
use crate::js::Context;
|
||||
use crate::webidl::NonstandardOutgoing;
|
||||
use failure::{bail, Error};
|
||||
use anyhow::{bail, Error};
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
pub struct Outgoing<'a, 'b> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]
|
||||
|
||||
use failure::{bail, Error, ResultExt};
|
||||
use anyhow::{bail, Context, Error};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
@ -256,7 +256,7 @@ impl Bindgen {
|
||||
}
|
||||
Input::Path(ref path) => {
|
||||
let contents = fs::read(&path)
|
||||
.with_context(|_| format!("failed to read `{}`", path.display()))?;
|
||||
.with_context(|| format!("failed to read `{}`", path.display()))?;
|
||||
let module = walrus::ModuleConfig::new()
|
||||
// Skip validation of the module as LLVM's output is
|
||||
// generally already well-formed and so we won't gain much
|
||||
@ -307,7 +307,7 @@ impl Bindgen {
|
||||
|
||||
self.threads
|
||||
.run(&mut module)
|
||||
.with_context(|_| "failed to prepare module for threading")?;
|
||||
.with_context(|| "failed to prepare module for threading")?;
|
||||
|
||||
// If requested, turn all mangled symbols into prettier unmangled
|
||||
// symbols with the help of `rustc-demangle`.
|
||||
@ -373,10 +373,10 @@ impl Bindgen {
|
||||
.context("failed to transform return pointers into multi-value Wasm")?;
|
||||
}
|
||||
webidl::standard::add_section(&mut module, &aux, &bindings)
|
||||
.with_context(|_| "failed to generate a standard wasm bindings custom section")?;
|
||||
.with_context(|| "failed to generate a standard wasm bindings custom section")?;
|
||||
} else {
|
||||
if self.multi_value {
|
||||
failure::bail!(
|
||||
anyhow::bail!(
|
||||
"Wasm multi-value is currently only available when \
|
||||
Wasm interface types is also enabled"
|
||||
);
|
||||
@ -565,11 +565,11 @@ impl Output {
|
||||
&self.module
|
||||
}
|
||||
|
||||
pub fn emit(&self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
|
||||
pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
|
||||
self._emit(out_dir.as_ref())
|
||||
}
|
||||
|
||||
fn _emit(&self, out_dir: &Path) -> Result<(), Error> {
|
||||
fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
|
||||
let wasm_name = if self.wasm_interface_types {
|
||||
self.stem.clone()
|
||||
} else {
|
||||
@ -579,7 +579,7 @@ impl Output {
|
||||
fs::create_dir_all(out_dir)?;
|
||||
let wasm_bytes = self.module.emit_wasm();
|
||||
fs::write(&wasm_path, wasm_bytes)
|
||||
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
|
||||
|
||||
if self.wasm_interface_types {
|
||||
return Ok(());
|
||||
@ -593,7 +593,7 @@ impl Output {
|
||||
let path = out_dir.join("snippets").join(identifier).join(name);
|
||||
fs::create_dir_all(path.parent().unwrap())?;
|
||||
fs::write(&path, js)
|
||||
.with_context(|_| format!("failed to write `{}`", path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", path.display()))?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -601,7 +601,7 @@ impl Output {
|
||||
let path = out_dir.join("snippets").join(path);
|
||||
fs::create_dir_all(path.parent().unwrap())?;
|
||||
fs::write(&path, contents)
|
||||
.with_context(|_| format!("failed to write `{}`", path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", path.display()))?;
|
||||
}
|
||||
|
||||
if self.npm_dependencies.len() > 0 {
|
||||
@ -623,26 +623,26 @@ impl Output {
|
||||
};
|
||||
let js_path = out_dir.join(&self.stem).with_extension(extension);
|
||||
fs::write(&js_path, reset_indentation(&self.js))
|
||||
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", js_path.display()))?;
|
||||
|
||||
if self.typescript {
|
||||
let ts_path = js_path.with_extension("d.ts");
|
||||
fs::write(&ts_path, &self.ts)
|
||||
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", ts_path.display()))?;
|
||||
}
|
||||
|
||||
if self.mode.nodejs() {
|
||||
let js_path = wasm_path.with_extension(extension);
|
||||
let shim = self.generate_node_wasm_import(&self.module, &wasm_path);
|
||||
fs::write(&js_path, shim)
|
||||
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", js_path.display()))?;
|
||||
}
|
||||
|
||||
if self.typescript {
|
||||
let ts_path = wasm_path.with_extension("d.ts");
|
||||
let ts = wasm2es6js::typescript(&self.module)?;
|
||||
fs::write(&ts_path, ts)
|
||||
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
|
||||
.with_context(|| format!("failed to write `{}`", ts_path.display()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -1,4 +1,4 @@
|
||||
use failure::{bail, Error};
|
||||
use anyhow::{bail, Error};
|
||||
use std::collections::HashSet;
|
||||
use walrus::Module;
|
||||
|
||||
|
@ -14,7 +14,7 @@ use crate::descriptor::Function;
|
||||
use crate::webidl::incoming::IncomingBuilder;
|
||||
use crate::webidl::outgoing::OutgoingBuilder;
|
||||
use crate::webidl::{Binding, NonstandardWebidlSection};
|
||||
use failure::{format_err, Error};
|
||||
use anyhow::{format_err, Error};
|
||||
use walrus::{FunctionId, Module, ValType};
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
//! the `outgoing.rs` module.
|
||||
|
||||
use crate::descriptor::{Descriptor, VectorKind};
|
||||
use failure::{bail, format_err, Error};
|
||||
use anyhow::{bail, format_err, Error};
|
||||
use walrus::ValType;
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
|
@ -27,7 +27,7 @@ use crate::decode;
|
||||
use crate::descriptor::{Descriptor, Function};
|
||||
use crate::descriptors::WasmBindgenDescriptorsSection;
|
||||
use crate::intrinsic::Intrinsic;
|
||||
use failure::{bail, format_err, Error};
|
||||
use anyhow::{anyhow, bail, Error};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
@ -1340,7 +1340,7 @@ impl<'a> Context<'a> {
|
||||
walrus::ExportItem::Function(f) => f == bind.func,
|
||||
_ => false,
|
||||
})
|
||||
.ok_or_else(|| format_err!("missing export function for webidl binding"))?;
|
||||
.ok_or_else(|| anyhow!("missing export function for webidl binding"))?;
|
||||
let id = export.id();
|
||||
self.standard_export(binding, id)?;
|
||||
}
|
||||
@ -1363,7 +1363,7 @@ impl<'a> Context<'a> {
|
||||
let binding: &ast::FunctionBinding = std
|
||||
.bindings
|
||||
.get(bind.binding)
|
||||
.ok_or_else(|| format_err!("bad binding id"))?;
|
||||
.ok_or_else(|| anyhow!("bad binding id"))?;
|
||||
let (wasm_ty, webidl_ty, incoming, outgoing) = match binding {
|
||||
ast::FunctionBinding::Export(e) => (
|
||||
e.wasm_ty,
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
use crate::descriptor::{Descriptor, VectorKind};
|
||||
use crate::webidl::NonstandardWebidlSection;
|
||||
use failure::{bail, format_err, Error};
|
||||
use anyhow::{bail, format_err, Error};
|
||||
use walrus::{Module, ValType};
|
||||
use wasm_webidl_bindings::ast;
|
||||
|
||||
|
@ -20,7 +20,7 @@ use crate::descriptor::VectorKind;
|
||||
use crate::webidl::{AuxExportKind, AuxImport, AuxValue, JsImport, JsImportName};
|
||||
use crate::webidl::{NonstandardIncoming, NonstandardOutgoing};
|
||||
use crate::webidl::{NonstandardWebidlSection, WasmBindgenAux};
|
||||
use failure::{bail, Error, ResultExt};
|
||||
use anyhow::{bail, Context, Error};
|
||||
use walrus::Module;
|
||||
use wasm_bindgen_multi_value_xform as multi_value_xform;
|
||||
use wasm_bindgen_wasm_conventions as wasm_conventions;
|
||||
@ -210,14 +210,14 @@ pub fn add_section(
|
||||
}
|
||||
|
||||
let name = &module.exports.get(*export).name;
|
||||
let params = extract_incoming(&binding.incoming).with_context(|_| {
|
||||
let params = extract_incoming(&binding.incoming).with_context(|| {
|
||||
format!(
|
||||
"failed to map arguments for export `{}` to standard \
|
||||
binding expressions",
|
||||
name
|
||||
)
|
||||
})?;
|
||||
let result = extract_outgoing(&binding.outgoing).with_context(|_| {
|
||||
let result = extract_outgoing(&binding.outgoing).with_context(|| {
|
||||
format!(
|
||||
"failed to map return value for export `{}` to standard \
|
||||
binding expressions",
|
||||
@ -252,14 +252,14 @@ pub fn add_section(
|
||||
let import = module.imports.get(*import);
|
||||
(&import.module, &import.name)
|
||||
};
|
||||
let params = extract_outgoing(&binding.outgoing).with_context(|_| {
|
||||
let params = extract_outgoing(&binding.outgoing).with_context(|| {
|
||||
format!(
|
||||
"failed to map arguments of import `{}::{}` to standard \
|
||||
binding expressions",
|
||||
module_name, name,
|
||||
)
|
||||
})?;
|
||||
let result = extract_incoming(&binding.incoming).with_context(|_| {
|
||||
let result = extract_incoming(&binding.incoming).with_context(|| {
|
||||
format!(
|
||||
"failed to map return value of import `{}::{}` to standard \
|
||||
binding expressions",
|
||||
|
Reference in New Issue
Block a user