diff options
| author | Tibo <delor.thibault@gmail.com> | 2018-04-26 13:48:48 +1000 |
|---|---|---|
| committer | Tibo <delor.thibault@gmail.com> | 2018-04-26 13:48:48 +1000 |
| commit | b7df23c3d5a3950ea6432acd71d34cd847a3be89 (patch) | |
| tree | b529bd2a741dce5af1dd0e71997da5cbb25771a3 /src | |
| parent | 5581be26162fb34275538a7c4660b5f4e69a5d2a (diff) | |
Replace completely std::error with failure crate
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/main.rs | 11 | ||||
| -rw-r--r-- | src/config/config_type.rs | 8 | ||||
| -rw-r--r-- | src/config/mod.rs | 19 | ||||
| -rw-r--r-- | src/config/options.rs | 19 | ||||
| -rw-r--r-- | src/lib.rs | 7 |
5 files changed, 35 insertions, 29 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 453c7806d23..0ddf2f8c3df 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -11,6 +11,7 @@ #![cfg(not(test))] extern crate env_logger; +extern crate failure; extern crate getopts; extern crate rustfmt_nightly as rustfmt; @@ -19,6 +20,8 @@ use std::fs::File; use std::io::{self, stdout, Read, Write}; use std::path::PathBuf; +use failure::err_msg; + use getopts::{Matches, Options}; use rustfmt::{emit_post_matter, emit_pre_matter, load_config, CliOptions, Config, FmtResult, @@ -167,7 +170,7 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> { Ok((WriteMode::None, Summary::default())) } Operation::ConfigOutputDefault { path } => { - let toml = Config::default().all_options().to_toml()?; + let toml = Config::default().all_options().to_toml().map_err(err_msg)?; if let Some(path) = path { let mut file = File::create(path)?; file.write_all(toml.as_bytes())?; @@ -186,7 +189,9 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> { // parse file_lines if let Some(ref file_lines) = matches.opt_str("file-lines") { - config.set().file_lines(file_lines.parse()?); + config + .set() + .file_lines(file_lines.parse().map_err(err_msg)?); for f in config.file_lines().files() { match *f { FileName::Custom(ref f) if f == "stdin" => {} @@ -273,7 +278,7 @@ fn format( // that were used during formatting as TOML. if let Some(path) = minimal_config_path { let mut file = File::create(path)?; - let toml = config.used_options().to_toml()?; + let toml = config.used_options().to_toml().map_err(err_msg)?; file.write_all(toml.as_bytes())?; } diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 3b9ca90350c..828dfe8b024 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -305,12 +305,12 @@ macro_rules! create_config { /// /// Return a `Config` if the config could be read and parsed from /// the file, Error otherwise. - pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, Error> { + pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, ::failure::Error> { let mut file = File::open(&file_path)?; let mut toml = String::new(); file.read_to_string(&mut toml)?; Config::from_toml(&toml, file_path.parent().unwrap()) - .map_err(|err| Error::new(ErrorKind::InvalidData, err)) + .map_err(::failure::err_msg) } /// Resolve the config for input in `dir`. @@ -322,12 +322,12 @@ macro_rules! create_config { /// /// Returns the `Config` to use, and the path of the project file if there was /// one. - pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> { + pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), ::failure::Error> { /// Try to find a project file in the given directory and its parents. /// Returns the path of a the nearest project file if one exists, /// or `None` if no project file was found. - fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> { + fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, ::failure::Error> { let mut current = if dir.is_relative() { env::current_dir()?.join(dir) } else { diff --git a/src/config/mod.rs b/src/config/mod.rs index 8dde1e05c3d..8c90c556b4d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -12,16 +12,17 @@ use regex::Regex; use std::cell::Cell; use std::default::Default; use std::fs::File; -use std::io::{Error, ErrorKind, Read}; +use std::io::{ErrorKind, Read}; use std::path::{Path, PathBuf}; use std::{env, fs}; -use {FmtError, FmtResult}; +use FmtResult; use config::config_type::ConfigType; use config::file_lines::FileLines; pub use config::lists::*; pub use config::options::*; +use failure::Error; #[macro_use] pub mod config_type; @@ -161,11 +162,9 @@ pub fn load_config( }; let result = if let Some(over_ride) = over_ride { - Config::from_toml_path(over_ride.as_ref()) - .map(|p| (p, Some(over_ride.to_owned()))) - .map_err(FmtError::from) + Config::from_toml_path(over_ride.as_ref()).map(|p| (p, Some(over_ride.to_owned()))) } else if let Some(file_path) = file_path { - Config::from_resolved_toml_path(file_path).map_err(FmtError::from) + Config::from_resolved_toml_path(file_path) } else { Ok((Config::default(), None)) }; @@ -181,7 +180,7 @@ pub fn load_config( // Check for the presence of known config file names (`rustfmt.toml, `.rustfmt.toml`) in `dir` // // Return the path if a config file exists, empty if no file exists, and Error for IO errors -fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> { +fn get_toml_path(dir: &Path) -> FmtResult<Option<PathBuf>> { const CONFIG_FILE_NAMES: [&str; 2] = [".rustfmt.toml", "rustfmt.toml"]; for config_file_name in &CONFIG_FILE_NAMES { let config_file = dir.join(config_file_name); @@ -193,7 +192,7 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> { // find the project file yet, and continue searching. Err(e) => { if e.kind() != ErrorKind::NotFound { - return Err(e); + return Err(Error::from(e)); } } _ => {} @@ -204,10 +203,10 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> { fn config_path(options: &CliOptions) -> FmtResult<Option<PathBuf>> { let config_path_not_found = |path: &str| -> FmtResult<Option<PathBuf>> { - Err(FmtError::from(format!( + Err(format_err!( "Error: unable to find a config file for the given path: `{}`", path - ))) + )) }; // Read the config_path and convert to parent dir if a file is provided. diff --git a/src/config/options.rs b/src/config/options.rs index 73dbdb88663..9721815eeee 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -14,7 +14,9 @@ use config::config_type::ConfigType; use config::file_lines::FileLines; use config::lists::*; use config::Config; -use {FmtError, FmtResult, WRITE_MODE_LIST}; +use {FmtResult, WRITE_MODE_LIST}; + +use failure::err_msg; use getopts::Matches; use std::collections::HashSet; @@ -332,8 +334,8 @@ impl CliOptions { .map(|c| c == "nightly") .unwrap_or(false); if unstable_features && !rust_nightly { - return Err(FmtError::from( - "Unstable features are only available on Nightly channel", + return Err(format_err!( + "Unstable features are only available on Nightly channel" )); } else { options.unstable_features = unstable_features; @@ -345,22 +347,23 @@ impl CliOptions { if let Ok(write_mode) = WriteMode::from_str(write_mode) { options.write_mode = Some(write_mode); } else { - return Err(FmtError::from(format!( + return Err(format_err!( "Invalid write-mode: {}, expected one of {}", - write_mode, WRITE_MODE_LIST - ))); + write_mode, + WRITE_MODE_LIST + )); } } if let Some(ref color) = matches.opt_str("color") { match Color::from_str(color) { Ok(color) => options.color = Some(color), - _ => return Err(FmtError::from(format!("Invalid color: {}", color))), + _ => return Err(format_err!("Invalid color: {}", color)), } } if let Some(ref file_lines) = matches.opt_str("file-lines") { - options.file_lines = file_lines.parse()?; + options.file_lines = file_lines.parse().map_err(err_msg)?; } if matches.opt_present("skip-children") { diff --git a/src/lib.rs b/src/lib.rs index 9b1168f5d12..b272402b20f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ #[macro_use] extern crate derive_new; extern crate diff; +#[macro_use] extern crate failure; extern crate getopts; extern crate itertools; @@ -38,7 +39,6 @@ extern crate toml; extern crate unicode_segmentation; use std::collections::HashMap; -use std::error; use std::fmt; use std::io::{self, stdout, Write}; use std::panic::{catch_unwind, AssertUnwindSafe}; @@ -64,8 +64,7 @@ pub use config::options::CliOptions; pub use config::summary::Summary; pub use config::{file_lines, load_config, Config, WriteMode}; -pub type FmtError = Box<error::Error + Send + Sync>; -pub type FmtResult<T> = std::result::Result<T, FmtError>; +pub type FmtResult<T> = std::result::Result<T, failure::Error>; pub const WRITE_MODE_LIST: &str = "[replace|overwrite|display|plain|diff|coverage|checkstyle|check]"; @@ -896,7 +895,7 @@ pub enum Input { pub fn format_and_emit_report(input: Input, config: &Config) -> FmtResult<Summary> { if !config.version_meets_requirement() { - return Err(FmtError::from("Version mismatch")); + return Err(format_err!("Version mismatch")); } let out = &mut stdout(); match format_input(input, config, Some(out)) { |
