Skip to content
Myr

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

5 files+43 added−64 removed

modified:src/error.rs+18 added-61 removed
1use std::error::Error;2use std::fmt;1use std::num;2use std::num::ParseIntError;34use rust_decimal::Error as DecimalError;5use thiserror::Error;68#[derive(Debug)]7#[derive(Debug, Error)]8pub 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}2425impl From<num::ParseIntError> for ParameterScanError {
7 unmodified lines
33 ParameterScanError::ParseDecimalError(e)34 }35}3631impl 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}5556impl Error for ParameterScanError {}5758#[derive(Debug, PartialEq)]37#[derive(Debug, Error)]38pub 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}6768impl<'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}9596impl<'a> Error for OptionsError<'a> {}
modified:src/main.rs+1 added-1 removed
259 unmodified lines
260 Ok(export_manager)261}262263/// Build the commands to benchmark264fn build_commands(matches: &ArgMatches) -> Vec<Command> {264fn 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();267268 if let Some(args) = matches.values_of("parameter-scan") {
186 unmodified lines
modified:src/options.rs+2 added-2 removed
194 unmodified lines
195 Shell::parse("shell 'foo").unwrap_err(),196 OptionsError::ShellParseError(_)197 ));198199 assert_eq!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell);199 assert!(matches!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell));200201 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 lines
262 "serde_json",263 "shell-words",264 "statistical",265 "tempfile",266 "thiserror",267 "winapi",268]269270[[package]]
549 unmodified lines
820 "terminal_size",821]822823[[package]]824name = "thiserror"825version = "1.0.30"826source = "registry+https://github.com/rust-lang/crates.io-index"827checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"828dependencies = [829 "thiserror-impl",830]831832[[package]]833name = "thiserror-impl"834version = "1.0.30"835source = "registry+https://github.com/rust-lang/crates.io-index"836checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"837dependencies = [838 "proc-macro2",839 "quote",840 "syn",841]842843[[package]]844name = "unicode-xid"845version = "0.2.2"846source = "registry+https://github.com/rust-lang/crates.io-index"847checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
46 unmodified lines
modified:Cargo.toml+1 added