From e9e0fb3cbae270f555a9b92ac04d71114720e829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sun, 7 Oct 2018 15:45:04 +0200 Subject: [PATCH 1/5] refactor: Return failure::Error instead of wasm_pack::error::Error --- src/binaries.rs | 8 ++++---- src/bindgen.rs | 4 ++-- src/test/webdriver.rs | 13 +++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/binaries.rs b/src/binaries.rs index 82eacb70..e1601868 100644 --- a/src/binaries.rs +++ b/src/binaries.rs @@ -138,7 +138,7 @@ pub fn install_binaries_from_targz_at_url<'a, I>( crate_path: &Path, url: &str, binaries: I, -) -> Result<(), Error> +) -> Result<(), failure::Error> where I: IntoIterator, { @@ -175,7 +175,7 @@ where .map(|s| s.to_string_lossy()) .collect::>() .join(", "), - ))) + )).into()) } } @@ -186,7 +186,7 @@ pub fn install_binaries_from_zip_at_url<'a, I>( crate_path: &Path, url: &str, binaries: I, -) -> Result<(), Error> +) -> Result<(), failure::Error> where I: IntoIterator, { @@ -226,7 +226,7 @@ where .map(|s| s.to_string_lossy()) .collect::>() .join(", "), - ))) + )).into()) } } diff --git a/src/bindgen.rs b/src/bindgen.rs index 091d4a94..e6e9f545 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -55,7 +55,7 @@ pub fn install_wasm_bindgen( } /// Download a tarball containing a pre-built `wasm-bindgen` binary. -pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), Error> { +pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), failure::Error> { let target = if target::LINUX && target::x86_64 { "x86_64-unknown-linux-musl" } else if target::MACOS && target::x86_64 { @@ -65,7 +65,7 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result } else { return Err(Error::unsupported( "there are no pre-built `wasm-bindgen` binaries for this target", - )); + ).into()); }; let url = format!( diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index c3ddf6a2..2ea67420 100644 --- a/src/test/webdriver.rs +++ b/src/test/webdriver.rs @@ -5,6 +5,7 @@ use binaries::{ }; use command::build::BuildMode; use error::Error; +use failure; use slog::Logger; use std::path::{Path, PathBuf}; use target; @@ -15,7 +16,7 @@ pub fn get_or_install_chromedriver( log: &Logger, crate_path: &Path, mode: BuildMode, -) -> Result { +) -> Result { match (mode, bin_path(log, crate_path, "chromedriver")) { (_, Some(path)) => Ok(path), (BuildMode::Normal, None) => install_chromedriver(crate_path), @@ -24,7 +25,7 @@ pub fn get_or_install_chromedriver( "No crate-local `chromedriver` binary found, and could not find a global \ `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ mode.", - )), + ).into()), } } @@ -52,7 +53,7 @@ fn get_chromedriver_url() -> Result { } /// Download and install a pre-built `chromedriver` binary. -pub fn install_chromedriver(crate_path: &Path) -> Result { +pub fn install_chromedriver(crate_path: &Path) -> Result { let url = get_chromedriver_url()?; install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?; let chromedriver = get_local_chromedriver_path(crate_path); @@ -66,7 +67,7 @@ pub fn get_or_install_geckodriver( log: &Logger, crate_path: &Path, mode: BuildMode, -) -> Result { +) -> Result { match (mode, bin_path(log, crate_path, "geckodriver")) { (_, Some(path)) => Ok(path), (BuildMode::Normal, None) => install_geckodriver(crate_path), @@ -74,7 +75,7 @@ pub fn get_or_install_geckodriver( (BuildMode::Noinstall, None) => Err(Error::crate_config( "No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \ on the `$PATH`. Not installing `geckodriver` because of noinstall mode.", - )), + ).into()), } } @@ -107,7 +108,7 @@ fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf { } /// Download and install a pre-built `geckodriver` binary. -pub fn install_geckodriver(crate_path: &Path) -> Result { +pub fn install_geckodriver(crate_path: &Path) -> Result { let url = get_geckodriver_url()?; if url.ends_with("tar.gz") { From 899f5e0ffcf37593ae039c0bec162b75bc5f7349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sun, 7 Oct 2018 16:00:11 +0200 Subject: [PATCH 2/5] refactor: Import self and use full module path for failure Use full module path for failure to be consistent since it's used like that in other modules. --- src/installer.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/installer.rs b/src/installer.rs index 8d1f779a..b2e42ad2 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -23,7 +23,7 @@ use std::path::Path; use std::process; use atty; -use failure::{Error, ResultExt}; +use failure::{self, ResultExt}; use which; pub fn install() -> ! { @@ -47,7 +47,7 @@ pub fn install() -> ! { process::exit(0); } -fn do_install() -> Result<(), Error> { +fn do_install() -> Result<(), failure::Error> { // Find `rustup.exe` in PATH, we'll be using its installation directory as // our installation directory. let rustup = match which::which("rustup") { @@ -85,7 +85,7 @@ fn do_install() -> Result<(), Error> { Ok(()) } -fn confirm_can_overwrite(dst: &Path) -> Result<(), Error> { +fn confirm_can_overwrite(dst: &Path) -> Result<(), failure::Error> { // If the `-f` argument was passed, we can always overwrite everything. if env::args().any(|arg| arg == "-f") { return Ok(()); From 0be4905e315764faa46c2dba33066462132bb8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sun, 7 Oct 2018 16:29:11 +0200 Subject: [PATCH 3/5] refactor: Return failure::Error instead of wasm_pack::error::Error --- src/build.rs | 6 +++--- src/command/utils.rs | 4 ++-- src/lockfile.rs | 15 +++++++-------- src/logger.rs | 4 ++-- src/manifest/mod.rs | 15 ++++++++------- src/readme.rs | 4 ++-- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/build.rs b/src/build.rs index 8b6e7c7f..f539744c 100644 --- a/src/build.rs +++ b/src/build.rs @@ -12,7 +12,7 @@ use std::str; use PBAR; /// Ensure that `rustc` is present and that it is >= 1.30.0 -pub fn check_rustc_version(step: &Step) -> Result { +pub fn check_rustc_version(step: &Step) -> Result { let msg = format!("{}Checking `rustc` version...", emoji::CRAB); PBAR.step(step, &msg); let local_minor_version = rustc_minor_version(); @@ -25,14 +25,14 @@ pub fn check_rustc_version(step: &Step) -> Result { mv.to_string() ), local_minor_version: mv.to_string(), - }) + }.into()) } else { Ok(mv.to_string()) } }, None => Err(Error::RustcMissing { message: "We can't figure out what your Rust version is- which means you might not have Rust installed. Please install Rust version 1.30.0 or higher.".to_string(), - }), + }.into()), } } diff --git a/src/command/utils.rs b/src/command/utils.rs index 07db00cd..1e49d853 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -1,7 +1,7 @@ //! Utility functions for commands. use emoji; -use error::Error; +use failure; use progressbar::Step; use std::fs; use std::io; @@ -20,7 +20,7 @@ pub fn set_crate_path(path: Option) -> io::Result { } /// Construct our `pkg` directory in the crate. -pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), Error> { +pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), failure::Error> { let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); PBAR.step(step, &msg); fs::create_dir_all(&out_dir)?; diff --git a/src/lockfile.rs b/src/lockfile.rs index e016802f..ec628bff 100644 --- a/src/lockfile.rs +++ b/src/lockfile.rs @@ -23,10 +23,10 @@ struct Package { impl Lockfile { /// Read the `Cargo.lock` file for the crate at the given path. - pub fn new(crate_path: &Path) -> Result { + pub fn new(crate_path: &Path) -> Result { let lock_path = get_lockfile_path(crate_path)?; let lockfile = fs::read_to_string(lock_path)?; - toml::from_str(&lockfile).map_err(Error::from) + toml::from_str(&lockfile).map_err(|err| Error::from(err).into()) } /// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`. @@ -36,7 +36,7 @@ impl Lockfile { /// Like `wasm_bindgen_version`, except it returns an error instead of /// `None`. - pub fn require_wasm_bindgen(&self) -> Result<&str, Error> { + pub fn require_wasm_bindgen(&self) -> Result<&str, failure::Error> { self.wasm_bindgen_version().ok_or_else(|| { let message = format!( "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ @@ -44,7 +44,7 @@ impl Lockfile { wasm-bindgen = \"0.2\"", style("wasm-bindgen").bold().dim(), ); - Error::CrateConfig { message } + Error::CrateConfig { message }.into() }) } @@ -63,21 +63,20 @@ impl Lockfile { /// Given the path to the crate that we are buliding, return a `PathBuf` /// containing the location of the lock file, by finding the workspace root. -fn get_lockfile_path(crate_path: &Path) -> Result { +fn get_lockfile_path(crate_path: &Path) -> Result { // Identify the crate's root directory, or return an error. let manifest = crate_path.join("Cargo.toml"); let crate_root = cargo_metadata::metadata(Some(&manifest)) .map_err(|_| Error::CrateConfig { message: String::from("Error while processing crate metadata"), - })? - .workspace_root; + })?.workspace_root; // Check that a lock file can be found in the directory. Return an error // if it cannot, otherwise return the path buffer. let lockfile_path = Path::new(&crate_root).join("Cargo.lock"); if !lockfile_path.is_file() { Err(Error::CrateConfig { message: format!("Could not find lockfile at {:?}", lockfile_path), - }) + }.into()) } else { Ok(lockfile_path) } diff --git a/src/logger.rs b/src/logger.rs index 543e6038..f8f6cc4c 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,7 +1,7 @@ //! Logging facilities for `wasm-pack`. use command::Command; -use error::Error; +use failure; use slog::{Drain, Level, Logger}; use slog_async::Async; use slog_term::{FullFormat, PlainDecorator}; @@ -9,7 +9,7 @@ use std::fs::OpenOptions; use std::path::PathBuf; /// Create the logger for wasm-pack that will output any info warning or errors we encounter -pub fn new(cmd: &Command, verbosity: u8) -> Result { +pub fn new(cmd: &Command, verbosity: u8) -> Result { let log_path = log_file_path(&cmd); let file = OpenOptions::new() .create(true) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 1b7a2bf4..f67ec7ca 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -11,6 +11,7 @@ use self::npm::{ repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage, }; use emoji; +use failure; use error::Error; use progressbar::Step; use serde_json; @@ -75,13 +76,13 @@ struct CargoLib { crate_type: Option>, } -fn read_cargo_toml(path: &Path) -> Result { +fn read_cargo_toml(path: &Path) -> Result { let manifest_path = path.join("Cargo.toml"); if !manifest_path.is_file() { return Err(Error::crate_config(&format!( "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", path.display() - ))); + )).into()); } let mut cargo_file = File::open(manifest_path)?; let mut cargo_contents = String::new(); @@ -213,7 +214,7 @@ pub fn write_package_json( disable_dts: bool, target: &str, step: &Step, -) -> Result<(), Error> { +) -> Result<(), failure::Error> { let msg = format!("{}Writing a package.json...", emoji::MEMO); PBAR.step(step, &msg); @@ -234,19 +235,19 @@ pub fn write_package_json( } /// Get the crate name for the crate at the given path. -pub fn get_crate_name(path: &Path) -> Result { +pub fn get_crate_name(path: &Path) -> Result { Ok(read_cargo_toml(path)?.package.name) } /// Check that the crate the given path is properly configured. -pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> { +pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), failure::Error> { let msg = format!("{}Checking crate configuration...", emoji::WRENCH); PBAR.step(&step, &msg); check_crate_type(path)?; Ok(()) } -fn check_crate_type(path: &Path) -> Result<(), Error> { +fn check_crate_type(path: &Path) -> Result<(), failure::Error> { if read_cargo_toml(path)?.lib.map_or(false, |lib| { lib.crate_type .map_or(false, |types| types.iter().any(|s| s == "cdylib")) @@ -258,5 +259,5 @@ fn check_crate_type(path: &Path) -> Result<(), Error> { Cargo.toml file:\n\n\ [lib]\n\ crate-type = [\"cdylib\", \"rlib\"]" - )) + ).into()) } diff --git a/src/readme.rs b/src/readme.rs index 2c34a2e6..bd897997 100644 --- a/src/readme.rs +++ b/src/readme.rs @@ -1,7 +1,7 @@ //! Generating `README` files for the packaged wasm. -use error::Error; use std::fs; +use failure; use std::path::Path; use emoji; @@ -9,7 +9,7 @@ use progressbar::Step; use PBAR; /// Copy the crate's README into the `pkg` directory. -pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), Error> { +pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), failure::Error> { assert!( fs::metadata(path).ok().map_or(false, |m| m.is_dir()), "crate directory should exist" From b7a84200a68aac8674ac76f9ba6ee7943ad67c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sun, 7 Oct 2018 17:28:23 +0200 Subject: [PATCH 4/5] chore: Run rustfmt --- src/binaries.rs | 6 ++++-- src/bindgen.rs | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/binaries.rs b/src/binaries.rs index e1601868..ba7a1ec2 100644 --- a/src/binaries.rs +++ b/src/binaries.rs @@ -175,7 +175,8 @@ where .map(|s| s.to_string_lossy()) .collect::>() .join(", "), - )).into()) + )) + .into()) } } @@ -226,7 +227,8 @@ where .map(|s| s.to_string_lossy()) .collect::>() .join(", "), - )).into()) + )) + .into()) } } diff --git a/src/bindgen.rs b/src/bindgen.rs index e6e9f545..68e9b606 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -55,7 +55,10 @@ pub fn install_wasm_bindgen( } /// Download a tarball containing a pre-built `wasm-bindgen` binary. -pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), failure::Error> { +pub fn download_prebuilt_wasm_bindgen( + root_path: &Path, + version: &str, +) -> Result<(), failure::Error> { let target = if target::LINUX && target::x86_64 { "x86_64-unknown-linux-musl" } else if target::MACOS && target::x86_64 { @@ -65,7 +68,8 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result } else { return Err(Error::unsupported( "there are no pre-built `wasm-bindgen` binaries for this target", - ).into()); + ) + .into()); }; let url = format!( From 9404c1496d0bac938784c024f10528824614e7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sun, 7 Oct 2018 17:46:21 +0200 Subject: [PATCH 5/5] chore: Run rustfmt --- src/lockfile.rs | 6 ++++-- src/manifest/mod.rs | 5 +++-- src/readme.rs | 2 +- src/test/webdriver.rs | 6 ++++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lockfile.rs b/src/lockfile.rs index ec628bff..1b2cb81a 100644 --- a/src/lockfile.rs +++ b/src/lockfile.rs @@ -69,14 +69,16 @@ fn get_lockfile_path(crate_path: &Path) -> Result { let crate_root = cargo_metadata::metadata(Some(&manifest)) .map_err(|_| Error::CrateConfig { message: String::from("Error while processing crate metadata"), - })?.workspace_root; + })? + .workspace_root; // Check that a lock file can be found in the directory. Return an error // if it cannot, otherwise return the path buffer. let lockfile_path = Path::new(&crate_root).join("Cargo.lock"); if !lockfile_path.is_file() { Err(Error::CrateConfig { message: format!("Could not find lockfile at {:?}", lockfile_path), - }.into()) + } + .into()) } else { Ok(lockfile_path) } diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index f67ec7ca..4d39f41d 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -11,8 +11,8 @@ use self::npm::{ repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage, }; use emoji; -use failure; use error::Error; +use failure; use progressbar::Step; use serde_json; use toml; @@ -82,7 +82,8 @@ fn read_cargo_toml(path: &Path) -> Result { return Err(Error::crate_config(&format!( "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", path.display() - )).into()); + )) + .into()); } let mut cargo_file = File::open(manifest_path)?; let mut cargo_contents = String::new(); diff --git a/src/readme.rs b/src/readme.rs index bd897997..83a4b10c 100644 --- a/src/readme.rs +++ b/src/readme.rs @@ -1,7 +1,7 @@ //! Generating `README` files for the packaged wasm. -use std::fs; use failure; +use std::fs; use std::path::Path; use emoji; diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index 2ea67420..cac6d394 100644 --- a/src/test/webdriver.rs +++ b/src/test/webdriver.rs @@ -25,7 +25,8 @@ pub fn get_or_install_chromedriver( "No crate-local `chromedriver` binary found, and could not find a global \ `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ mode.", - ).into()), + ) + .into()), } } @@ -75,7 +76,8 @@ pub fn get_or_install_geckodriver( (BuildMode::Noinstall, None) => Err(Error::crate_config( "No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \ on the `$PATH`. Not installing `geckodriver` because of noinstall mode.", - ).into()), + ) + .into()), } }