Skip to content

Commit b7df23c

Browse files
committed
Replace completely std::error with failure crate
1 parent 5581be2 commit b7df23c

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

src/bin/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![cfg(not(test))]
1212

1313
extern crate env_logger;
14+
extern crate failure;
1415
extern crate getopts;
1516
extern crate rustfmt_nightly as rustfmt;
1617

@@ -19,6 +20,8 @@ use std::fs::File;
1920
use std::io::{self, stdout, Read, Write};
2021
use std::path::PathBuf;
2122

23+
use failure::err_msg;
24+
2225
use getopts::{Matches, Options};
2326

2427
use rustfmt::{emit_post_matter, emit_pre_matter, load_config, CliOptions, Config, FmtResult,
@@ -167,7 +170,7 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
167170
Ok((WriteMode::None, Summary::default()))
168171
}
169172
Operation::ConfigOutputDefault { path } => {
170-
let toml = Config::default().all_options().to_toml()?;
173+
let toml = Config::default().all_options().to_toml().map_err(err_msg)?;
171174
if let Some(path) = path {
172175
let mut file = File::create(path)?;
173176
file.write_all(toml.as_bytes())?;
@@ -186,7 +189,9 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
186189

187190
// parse file_lines
188191
if let Some(ref file_lines) = matches.opt_str("file-lines") {
189-
config.set().file_lines(file_lines.parse()?);
192+
config
193+
.set()
194+
.file_lines(file_lines.parse().map_err(err_msg)?);
190195
for f in config.file_lines().files() {
191196
match *f {
192197
FileName::Custom(ref f) if f == "stdin" => {}
@@ -273,7 +278,7 @@ fn format(
273278
// that were used during formatting as TOML.
274279
if let Some(path) = minimal_config_path {
275280
let mut file = File::create(path)?;
276-
let toml = config.used_options().to_toml()?;
281+
let toml = config.used_options().to_toml().map_err(err_msg)?;
277282
file.write_all(toml.as_bytes())?;
278283
}
279284

src/config/config_type.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ macro_rules! create_config {
305305
///
306306
/// Return a `Config` if the config could be read and parsed from
307307
/// the file, Error otherwise.
308-
pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, Error> {
308+
pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, ::failure::Error> {
309309
let mut file = File::open(&file_path)?;
310310
let mut toml = String::new();
311311
file.read_to_string(&mut toml)?;
312312
Config::from_toml(&toml, file_path.parent().unwrap())
313-
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
313+
.map_err(::failure::err_msg)
314314
}
315315

316316
/// Resolve the config for input in `dir`.
@@ -322,12 +322,12 @@ macro_rules! create_config {
322322
///
323323
/// Returns the `Config` to use, and the path of the project file if there was
324324
/// one.
325-
pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> {
325+
pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), ::failure::Error> {
326326

327327
/// Try to find a project file in the given directory and its parents.
328328
/// Returns the path of a the nearest project file if one exists,
329329
/// or `None` if no project file was found.
330-
fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> {
330+
fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, ::failure::Error> {
331331
let mut current = if dir.is_relative() {
332332
env::current_dir()?.join(dir)
333333
} else {

src/config/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ use regex::Regex;
1212
use std::cell::Cell;
1313
use std::default::Default;
1414
use std::fs::File;
15-
use std::io::{Error, ErrorKind, Read};
15+
use std::io::{ErrorKind, Read};
1616
use std::path::{Path, PathBuf};
1717
use std::{env, fs};
1818

19-
use {FmtError, FmtResult};
19+
use FmtResult;
2020

2121
use config::config_type::ConfigType;
2222
use config::file_lines::FileLines;
2323
pub use config::lists::*;
2424
pub use config::options::*;
25+
use failure::Error;
2526

2627
#[macro_use]
2728
pub mod config_type;
@@ -161,11 +162,9 @@ pub fn load_config(
161162
};
162163

163164
let result = if let Some(over_ride) = over_ride {
164-
Config::from_toml_path(over_ride.as_ref())
165-
.map(|p| (p, Some(over_ride.to_owned())))
166-
.map_err(FmtError::from)
165+
Config::from_toml_path(over_ride.as_ref()).map(|p| (p, Some(over_ride.to_owned())))
167166
} else if let Some(file_path) = file_path {
168-
Config::from_resolved_toml_path(file_path).map_err(FmtError::from)
167+
Config::from_resolved_toml_path(file_path)
169168
} else {
170169
Ok((Config::default(), None))
171170
};
@@ -181,7 +180,7 @@ pub fn load_config(
181180
// Check for the presence of known config file names (`rustfmt.toml, `.rustfmt.toml`) in `dir`
182181
//
183182
// Return the path if a config file exists, empty if no file exists, and Error for IO errors
184-
fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
183+
fn get_toml_path(dir: &Path) -> FmtResult<Option<PathBuf>> {
185184
const CONFIG_FILE_NAMES: [&str; 2] = [".rustfmt.toml", "rustfmt.toml"];
186185
for config_file_name in &CONFIG_FILE_NAMES {
187186
let config_file = dir.join(config_file_name);
@@ -193,7 +192,7 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
193192
// find the project file yet, and continue searching.
194193
Err(e) => {
195194
if e.kind() != ErrorKind::NotFound {
196-
return Err(e);
195+
return Err(Error::from(e));
197196
}
198197
}
199198
_ => {}
@@ -204,10 +203,10 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
204203

205204
fn config_path(options: &CliOptions) -> FmtResult<Option<PathBuf>> {
206205
let config_path_not_found = |path: &str| -> FmtResult<Option<PathBuf>> {
207-
Err(FmtError::from(format!(
206+
Err(format_err!(
208207
"Error: unable to find a config file for the given path: `{}`",
209208
path
210-
)))
209+
))
211210
};
212211

213212
// Read the config_path and convert to parent dir if a file is provided.

src/config/options.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use config::config_type::ConfigType;
1414
use config::file_lines::FileLines;
1515
use config::lists::*;
1616
use config::Config;
17-
use {FmtError, FmtResult, WRITE_MODE_LIST};
17+
use {FmtResult, WRITE_MODE_LIST};
18+
19+
use failure::err_msg;
1820

1921
use getopts::Matches;
2022
use std::collections::HashSet;
@@ -332,8 +334,8 @@ impl CliOptions {
332334
.map(|c| c == "nightly")
333335
.unwrap_or(false);
334336
if unstable_features && !rust_nightly {
335-
return Err(FmtError::from(
336-
"Unstable features are only available on Nightly channel",
337+
return Err(format_err!(
338+
"Unstable features are only available on Nightly channel"
337339
));
338340
} else {
339341
options.unstable_features = unstable_features;
@@ -345,22 +347,23 @@ impl CliOptions {
345347
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
346348
options.write_mode = Some(write_mode);
347349
} else {
348-
return Err(FmtError::from(format!(
350+
return Err(format_err!(
349351
"Invalid write-mode: {}, expected one of {}",
350-
write_mode, WRITE_MODE_LIST
351-
)));
352+
write_mode,
353+
WRITE_MODE_LIST
354+
));
352355
}
353356
}
354357

355358
if let Some(ref color) = matches.opt_str("color") {
356359
match Color::from_str(color) {
357360
Ok(color) => options.color = Some(color),
358-
_ => return Err(FmtError::from(format!("Invalid color: {}", color))),
361+
_ => return Err(format_err!("Invalid color: {}", color)),
359362
}
360363
}
361364

362365
if let Some(ref file_lines) = matches.opt_str("file-lines") {
363-
options.file_lines = file_lines.parse()?;
366+
options.file_lines = file_lines.parse().map_err(err_msg)?;
364367
}
365368

366369
if matches.opt_present("skip-children") {

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#[macro_use]
2020
extern crate derive_new;
2121
extern crate diff;
22+
#[macro_use]
2223
extern crate failure;
2324
extern crate getopts;
2425
extern crate itertools;
@@ -38,7 +39,6 @@ extern crate toml;
3839
extern crate unicode_segmentation;
3940

4041
use std::collections::HashMap;
41-
use std::error;
4242
use std::fmt;
4343
use std::io::{self, stdout, Write};
4444
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -64,8 +64,7 @@ pub use config::options::CliOptions;
6464
pub use config::summary::Summary;
6565
pub use config::{file_lines, load_config, Config, WriteMode};
6666

67-
pub type FmtError = Box<error::Error + Send + Sync>;
68-
pub type FmtResult<T> = std::result::Result<T, FmtError>;
67+
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
6968

7069
pub const WRITE_MODE_LIST: &str =
7170
"[replace|overwrite|display|plain|diff|coverage|checkstyle|check]";
@@ -896,7 +895,7 @@ pub enum Input {
896895

897896
pub fn format_and_emit_report(input: Input, config: &Config) -> FmtResult<Summary> {
898897
if !config.version_meets_requirement() {
899-
return Err(FmtError::from("Version mismatch"));
898+
return Err(format_err!("Version mismatch"));
900899
}
901900
let out = &mut stdout();
902901
match format_input(input, config, Some(out)) {

0 commit comments

Comments
 (0)