Use thiserror for to derive Error
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent e6dad51 · Browse files · 5 files·+43 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+43 added−64 removed
- other:+43 added−64 removed
Provenance
- Commit
ce7087b- Tree
6b121bf- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e6dad51
5 files changed+43 added−64 removed
modified:src/error.rs+18 added-61 removed
1-use std::error::Error;2-use std::fmt;1 use std::num;2 use std::num::ParseIntError;3 4 use rust_decimal::Error as DecimalError;5+use thiserror::Error;6 8-#[derive(Debug)]7+#[derive(Debug, Error)]8 pub enum ParameterScanError {9+ #[error("Error while parsing parameter scan arguments ({0})")]10 ParseIntError(num::ParseIntError),11+ #[error("Error while parsing parameter scan arguments ({0})")]12 ParseDecimalError(DecimalError),13+ #[error("Empty parameter range")]14 EmptyRange,15+ #[error("Parameter range is too large")]16 TooLarge,17+ #[error("Zero is not a valid parameter step")]18 ZeroStep,19+ #[error("A step size is required when the range bounds are floating point numbers. The step size can be specified with the '--parameter-step-size' parameter")]20 StepRequired,21+ #[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]22 UnexpectedCommandNameCount(usize, usize),23 }24 25 impl From<num::ParseIntError> for ParameterScanError {7 unmodified lines33 ParameterScanError::ParseDecimalError(e)34 }35 }36 31-impl fmt::Display for ParameterScanError {32- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {33- match *self {34- ParameterScanError::ParseIntError(ref e) => write!(f, "{}", e),35- ParameterScanError::ParseDecimalError(ref e) => write!(f, "{}", e),36- ParameterScanError::EmptyRange => write!(f, "Empty parameter range"),37- ParameterScanError::TooLarge => write!(f, "Parameter range is too large"),38- ParameterScanError::ZeroStep => write!(f, "Zero is not a valid parameter step"),39- ParameterScanError::StepRequired => write!(40- f,41- "A step size is required when the range bounds are \42- floating point numbers. The step size can be specified \43- with the '--parameter-step-size' parameter"44- ),45- ParameterScanError::UnexpectedCommandNameCount(real, expected) => {46- write!(47- f,48- "'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",49- real, expected50- )51- }52- }53- }54-}55-56-impl Error for ParameterScanError {}57-58-#[derive(Debug, PartialEq)]37+#[derive(Debug, Error)]38 pub enum OptionsError<'a> {39+ #[error(40+ "Conflicting requirements for the number of runs (empty range, min is larger than max)"41+ )]42 EmptyRunsRange,43+ #[error("Too many --command-name options: Expected {0} at most")]44 TooManyCommandNames(usize),45+ #[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]46 UnexpectedCommandNameCount(usize, usize),47+ #[error("Could not read numeric argument to '--{0}': {1}")]48 NumericParsingError(&'a str, ParseIntError),49+ #[error("An empty command has been specified for the '--shell <command>' option")]50 EmptyShell,51+ #[error("Failed to parse '--shell <command>' expression as command line: {0}")]52 ShellParseError(shell_words::ParseError),53 }67-68-impl<'a> fmt::Display for OptionsError<'a> {69- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {70- match *self {71- OptionsError::EmptyRunsRange => write!(f, "Empty runs range"),72- OptionsError::TooManyCommandNames(n) => {73- write!(f, "Too many --command-name options: expected {} at most", n)74- }75- OptionsError::UnexpectedCommandNameCount(real, expected) => {76- write!(77- f,78- "'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",79- real, expected80- )81- }82- OptionsError::NumericParsingError(cmd, ref err) => write!(83- f,84- "Could not read numeric argument to '--{cmd}': {err}",85- cmd = cmd,86- err = err87- ),88- OptionsError::EmptyShell => write!(f, "Empty command at --shell option"),89- OptionsError::ShellParseError(ref err) => {90- write!(f, "Could not parse --shell value as command line: {}", err)91- }92- }93- }94-}95-96-impl<'a> Error for OptionsError<'a> {}modified:src/main.rs+1 added-1 removed
259 unmodified lines260 Ok(export_manager)261 }262 263 /// Build the commands to benchmark264-fn build_commands(matches: &ArgMatches) -> Vec<Command> {264+fn build_commands(matches: &ArgMatches) -> Result<Vec<Command>> {265 let command_names = matches.values_of("command-name");266 let command_strings = matches.values_of("command").unwrap();267 268 if let Some(args) = matches.values_of("parameter-scan") {186 unmodified linesmodified:src/options.rs+2 added-2 removed
194 unmodified lines195 Shell::parse("shell 'foo").unwrap_err(),196 OptionsError::ShellParseError(_)197 ));198 199- assert_eq!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell);199+ assert!(matches!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell));200 201- assert_eq!(Shell::parse("''").unwrap_err(), OptionsError::EmptyShell);201+ assert!(matches!(Shell::parse("''").unwrap_err(), OptionsError::EmptyShell));202 }modified:Cargo.lock+21 added
261 unmodified lines262 "serde_json",263 "shell-words",264 "statistical",265 "tempfile",266+ "thiserror",267 "winapi",268 ]269 270 [[package]]549 unmodified lines820 "terminal_size",821 ]822 823 [[package]]824+name = "thiserror"825+version = "1.0.30"826+source = "registry+https://github.com/rust-lang/crates.io-index"827+checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"828+dependencies = [829+ "thiserror-impl",830+]831+832+[[package]]833+name = "thiserror-impl"834+version = "1.0.30"835+source = "registry+https://github.com/rust-lang/crates.io-index"836+checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"837+dependencies = [838+ "proc-macro2",839+ "quote",840+ "syn",841+]842+843+[[package]]844 name = "unicode-xid"845 version = "0.2.2"846 source = "registry+https://github.com/rust-lang/crates.io-index"847 checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"46 unmodified linesmodified:Cargo.toml+1 added
20 unmodified lines21222324252627282921 unmodified lines20 unmodified linesserde_json = "1.0"rust_decimal = "1.19"rand = "0.8"shell-words = "1.0"thiserror = "1.0"
[target.'cfg(not(windows))'.dependencies]libc = "0.2"
21 unmodified lines