refactor: Return failure::Error instead of wasm_pack::error::Error

master
Jesper Håkansson 7 years ago
parent 899f5e0ffc
commit 0be4905e31
  1. 6
      src/build.rs
  2. 4
      src/command/utils.rs
  3. 15
      src/lockfile.rs
  4. 4
      src/logger.rs
  5. 15
      src/manifest/mod.rs
  6. 4
      src/readme.rs

@ -12,7 +12,7 @@ use std::str;
use PBAR; use PBAR;
/// Ensure that `rustc` is present and that it is >= 1.30.0 /// Ensure that `rustc` is present and that it is >= 1.30.0
pub fn check_rustc_version(step: &Step) -> Result<String, Error> { pub fn check_rustc_version(step: &Step) -> Result<String, failure::Error> {
let msg = format!("{}Checking `rustc` version...", emoji::CRAB); let msg = format!("{}Checking `rustc` version...", emoji::CRAB);
PBAR.step(step, &msg); PBAR.step(step, &msg);
let local_minor_version = rustc_minor_version(); let local_minor_version = rustc_minor_version();
@ -25,14 +25,14 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
mv.to_string() mv.to_string()
), ),
local_minor_version: mv.to_string(), local_minor_version: mv.to_string(),
}) }.into())
} else { } else {
Ok(mv.to_string()) Ok(mv.to_string())
} }
}, },
None => Err(Error::RustcMissing { 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(), 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()),
} }
} }

@ -1,7 +1,7 @@
//! Utility functions for commands. //! Utility functions for commands.
use emoji; use emoji;
use error::Error; use failure;
use progressbar::Step; use progressbar::Step;
use std::fs; use std::fs;
use std::io; use std::io;
@ -20,7 +20,7 @@ pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
} }
/// Construct our `pkg` directory in the crate. /// 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); let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg); PBAR.step(step, &msg);
fs::create_dir_all(&out_dir)?; fs::create_dir_all(&out_dir)?;

@ -23,10 +23,10 @@ struct Package {
impl Lockfile { impl Lockfile {
/// Read the `Cargo.lock` file for the crate at the given path. /// Read the `Cargo.lock` file for the crate at the given path.
pub fn new(crate_path: &Path) -> Result<Lockfile, Error> { pub fn new(crate_path: &Path) -> Result<Lockfile, failure::Error> {
let lock_path = get_lockfile_path(crate_path)?; let lock_path = get_lockfile_path(crate_path)?;
let lockfile = fs::read_to_string(lock_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`. /// 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 /// Like `wasm_bindgen_version`, except it returns an error instead of
/// `None`. /// `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(|| { self.wasm_bindgen_version().ok_or_else(|| {
let message = format!( let message = format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
@ -44,7 +44,7 @@ impl Lockfile {
wasm-bindgen = \"0.2\"", wasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim(), 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` /// 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. /// containing the location of the lock file, by finding the workspace root.
fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, Error> { fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, failure::Error> {
// Identify the crate's root directory, or return an error. // Identify the crate's root directory, or return an error.
let manifest = crate_path.join("Cargo.toml"); let manifest = crate_path.join("Cargo.toml");
let crate_root = cargo_metadata::metadata(Some(&manifest)) let crate_root = cargo_metadata::metadata(Some(&manifest))
.map_err(|_| Error::CrateConfig { .map_err(|_| Error::CrateConfig {
message: String::from("Error while processing crate metadata"), 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 // Check that a lock file can be found in the directory. Return an error
// if it cannot, otherwise return the path buffer. // if it cannot, otherwise return the path buffer.
let lockfile_path = Path::new(&crate_root).join("Cargo.lock"); let lockfile_path = Path::new(&crate_root).join("Cargo.lock");
if !lockfile_path.is_file() { if !lockfile_path.is_file() {
Err(Error::CrateConfig { Err(Error::CrateConfig {
message: format!("Could not find lockfile at {:?}", lockfile_path), message: format!("Could not find lockfile at {:?}", lockfile_path),
}) }.into())
} else { } else {
Ok(lockfile_path) Ok(lockfile_path)
} }

@ -1,7 +1,7 @@
//! Logging facilities for `wasm-pack`. //! Logging facilities for `wasm-pack`.
use command::Command; use command::Command;
use error::Error; use failure;
use slog::{Drain, Level, Logger}; use slog::{Drain, Level, Logger};
use slog_async::Async; use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator}; use slog_term::{FullFormat, PlainDecorator};
@ -9,7 +9,7 @@ use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
/// Create the logger for wasm-pack that will output any info warning or errors we encounter /// Create the logger for wasm-pack that will output any info warning or errors we encounter
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> { pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, failure::Error> {
let log_path = log_file_path(&cmd); let log_path = log_file_path(&cmd);
let file = OpenOptions::new() let file = OpenOptions::new()
.create(true) .create(true)

@ -11,6 +11,7 @@ use self::npm::{
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage, repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
}; };
use emoji; use emoji;
use failure;
use error::Error; use error::Error;
use progressbar::Step; use progressbar::Step;
use serde_json; use serde_json;
@ -75,13 +76,13 @@ struct CargoLib {
crate_type: Option<Vec<String>>, crate_type: Option<Vec<String>>,
} }
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> { fn read_cargo_toml(path: &Path) -> Result<CargoManifest, failure::Error> {
let manifest_path = path.join("Cargo.toml"); let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() { if !manifest_path.is_file() {
return Err(Error::crate_config(&format!( return Err(Error::crate_config(&format!(
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
path.display() path.display()
))); )).into());
} }
let mut cargo_file = File::open(manifest_path)?; let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new(); let mut cargo_contents = String::new();
@ -213,7 +214,7 @@ pub fn write_package_json(
disable_dts: bool, disable_dts: bool,
target: &str, target: &str,
step: &Step, step: &Step,
) -> Result<(), Error> { ) -> Result<(), failure::Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO); let msg = format!("{}Writing a package.json...", emoji::MEMO);
PBAR.step(step, &msg); PBAR.step(step, &msg);
@ -234,19 +235,19 @@ pub fn write_package_json(
} }
/// Get the crate name for the crate at the given path. /// Get the crate name for the crate at the given path.
pub fn get_crate_name(path: &Path) -> Result<String, Error> { pub fn get_crate_name(path: &Path) -> Result<String, failure::Error> {
Ok(read_cargo_toml(path)?.package.name) Ok(read_cargo_toml(path)?.package.name)
} }
/// Check that the crate the given path is properly configured. /// 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); let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg); PBAR.step(&step, &msg);
check_crate_type(path)?; check_crate_type(path)?;
Ok(()) 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| { if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib")) .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\ Cargo.toml file:\n\n\
[lib]\n\ [lib]\n\
crate-type = [\"cdylib\", \"rlib\"]" crate-type = [\"cdylib\", \"rlib\"]"
)) ).into())
} }

@ -1,7 +1,7 @@
//! Generating `README` files for the packaged wasm. //! Generating `README` files for the packaged wasm.
use error::Error;
use std::fs; use std::fs;
use failure;
use std::path::Path; use std::path::Path;
use emoji; use emoji;
@ -9,7 +9,7 @@ use progressbar::Step;
use PBAR; use PBAR;
/// Copy the crate's README into the `pkg` directory. /// 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!( assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()), fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist" "crate directory should exist"

Loading…
Cancel
Save