Skip to content

Commit d19fc45

Browse files
authored
Merge pull request #2650 from thibaultdelor/useFailureCrate
Use failure crate
2 parents 0f4ed08 + e06c9c8 commit d19fc45

File tree

7 files changed

+121
-71
lines changed

7 files changed

+121
-71
lines changed

Cargo.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ getopts = "0.2"
4747
derive-new = "0.5"
4848
cargo_metadata = "0.5.1"
4949
rustc-ap-syntax = "110.0.0"
50+
failure = "0.1.1"
5051

5152
[dev-dependencies]
5253
assert_cli = "0.5"

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/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use std::io::{Error, ErrorKind, Read};
1616
use std::path::{Path, PathBuf};
1717
use std::{env, fs};
1818

19-
use {FmtError, FmtResult};
20-
2119
use config::config_type::ConfigType;
2220
use config::file_lines::FileLines;
2321
pub use config::lists::*;
@@ -154,18 +152,16 @@ create_config! {
154152
pub fn load_config(
155153
file_path: Option<&Path>,
156154
options: Option<&CliOptions>,
157-
) -> FmtResult<(Config, Option<PathBuf>)> {
155+
) -> Result<(Config, Option<PathBuf>), Error> {
158156
let over_ride = match options {
159157
Some(opts) => config_path(opts)?,
160158
None => None,
161159
};
162160

163161
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)
162+
Config::from_toml_path(over_ride.as_ref()).map(|p| (p, Some(over_ride.to_owned())))
167163
} else if let Some(file_path) = file_path {
168-
Config::from_resolved_toml_path(file_path).map_err(FmtError::from)
164+
Config::from_resolved_toml_path(file_path)
169165
} else {
170166
Ok((Config::default(), None))
171167
};
@@ -202,12 +198,15 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
202198
Ok(None)
203199
}
204200

205-
fn config_path(options: &CliOptions) -> FmtResult<Option<PathBuf>> {
206-
let config_path_not_found = |path: &str| -> FmtResult<Option<PathBuf>> {
207-
Err(FmtError::from(format!(
208-
"Error: unable to find a config file for the given path: `{}`",
209-
path
210-
)))
201+
fn config_path(options: &CliOptions) -> Result<Option<PathBuf>, Error> {
202+
let config_path_not_found = |path: &str| -> Result<Option<PathBuf>, Error> {
203+
Err(Error::new(
204+
ErrorKind::NotFound,
205+
format!(
206+
"Error: unable to find a config file for the given path: `{}`",
207+
path
208+
),
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/format-diff/main.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#![deny(warnings)]
1616

1717
extern crate env_logger;
18+
#[macro_use]
19+
extern crate failure;
1820
extern crate getopts;
1921
#[macro_use]
2022
extern crate log;
@@ -24,9 +26,8 @@ extern crate serde_derive;
2426
extern crate serde_json as json;
2527

2628
use std::collections::HashSet;
27-
use std::error::Error;
2829
use std::io::{self, BufRead};
29-
use std::{env, fmt, process};
30+
use std::{env, process};
3031

3132
use regex::Regex;
3233

@@ -35,31 +36,14 @@ use regex::Regex;
3536
/// We only want to format rust files by default.
3637
const DEFAULT_PATTERN: &str = r".*\.rs";
3738

38-
#[derive(Debug)]
39+
#[derive(Fail, Debug)]
3940
enum FormatDiffError {
40-
IncorrectOptions(getopts::Fail),
41-
IncorrectFilter(regex::Error),
42-
IoError(io::Error),
43-
}
44-
45-
impl fmt::Display for FormatDiffError {
46-
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
47-
fmt::Display::fmt(self.cause().unwrap(), f)
48-
}
49-
}
50-
51-
impl Error for FormatDiffError {
52-
fn description(&self) -> &str {
53-
self.cause().unwrap().description()
54-
}
55-
56-
fn cause(&self) -> Option<&Error> {
57-
Some(match *self {
58-
FormatDiffError::IoError(ref e) => e,
59-
FormatDiffError::IncorrectFilter(ref e) => e,
60-
FormatDiffError::IncorrectOptions(ref e) => e,
61-
})
62-
}
41+
#[fail(display = "{}", _0)]
42+
IncorrectOptions(#[cause] getopts::Fail),
43+
#[fail(display = "{}", _0)]
44+
IncorrectFilter(#[cause] regex::Error),
45+
#[fail(display = "{}", _0)]
46+
IoError(#[cause] io::Error),
6347
}
6448

6549
impl From<getopts::Fail> for FormatDiffError {
@@ -99,7 +83,7 @@ fn main() {
9983
);
10084

10185
if let Err(e) = run(&opts) {
102-
println!("{}", opts.usage(e.description()));
86+
println!("{}", opts.usage(&format!("{}", e)));
10387
process::exit(1);
10488
}
10589
}

0 commit comments

Comments
 (0)