Use thiserror for to derive Error

This commit is contained in:
David Peter authored and David Peter committed 2022-02-06 20:19:50 +01:00
1 parent e6dad51f34
commit ce7087b107
5 files changed
+43 -64

No files matched your search

Generated
+21
View File
@@ -263,6 +263,7 @@ dependencies = [
"shell-words",
"statistical",
"tempfile",
"thiserror",
"winapi",
]
@@ -819,6 +820,26 @@ dependencies = [
"terminal_size",
]
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
+1
View File
@@ -22,6 +22,7 @@ serde_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"
+18 -61
View File
@@ -1,18 +1,24 @@
use std::error::Error;
use std::fmt;
use std::num;
use std::num::ParseIntError;
use rust_decimal::Error as DecimalError;
use thiserror::Error;
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ParameterScanError {
#[error("Error while parsing parameter scan arguments ({0})")]
ParseIntError(num::ParseIntError),
#[error("Error while parsing parameter scan arguments ({0})")]
ParseDecimalError(DecimalError),
#[error("Empty parameter range")]
EmptyRange,
#[error("Parameter range is too large")]
TooLarge,
#[error("Zero is not a valid parameter step")]
ZeroStep,
#[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")]
StepRequired,
#[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]
UnexpectedCommandNameCount(usize, usize),
}
@@ -28,69 +34,20 @@ impl From<DecimalError> for ParameterScanError {
}
}
impl fmt::Display for ParameterScanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ParameterScanError::ParseIntError(ref e) => write!(f, "{}", e),
ParameterScanError::ParseDecimalError(ref e) => write!(f, "{}", e),
ParameterScanError::EmptyRange => write!(f, "Empty parameter range"),
ParameterScanError::TooLarge => write!(f, "Parameter range is too large"),
ParameterScanError::ZeroStep => write!(f, "Zero is not a valid parameter step"),
ParameterScanError::StepRequired => write!(
f,
"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"
),
ParameterScanError::UnexpectedCommandNameCount(real, expected) => {
write!(
f,
"'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",
real, expected
)
}
}
}
}
impl Error for ParameterScanError {}
#[derive(Debug, PartialEq)]
#[derive(Debug, Error)]
pub enum OptionsError<'a> {
#[error(
"Conflicting requirements for the number of runs (empty range, min is larger than max)"
)]
EmptyRunsRange,
#[error("Too many --command-name options: Expected {0} at most")]
TooManyCommandNames(usize),
#[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]
UnexpectedCommandNameCount(usize, usize),
#[error("Could not read numeric argument to '--{0}': {1}")]
NumericParsingError(&'a str, ParseIntError),
#[error("An empty command has been specified for the '--shell <command>' option")]
EmptyShell,
#[error("Failed to parse '--shell <command>' expression as command line: {0}")]
ShellParseError(shell_words::ParseError),
}
impl<'a> fmt::Display for OptionsError<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
OptionsError::EmptyRunsRange => write!(f, "Empty runs range"),
OptionsError::TooManyCommandNames(n) => {
write!(f, "Too many --command-name options: expected {} at most", n)
}
OptionsError::UnexpectedCommandNameCount(real, expected) => {
write!(
f,
"'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",
real, expected
)
}
OptionsError::NumericParsingError(cmd, ref err) => write!(
f,
"Could not read numeric argument to '--{cmd}': {err}",
cmd = cmd,
err = err
),
OptionsError::EmptyShell => write!(f, "Empty command at --shell option"),
OptionsError::ShellParseError(ref err) => {
write!(f, "Could not parse --shell value as command line: {}", err)
}
}
}
}
impl<'a> Error for OptionsError<'a> {}
+1 -1
View File
@@ -261,7 +261,7 @@ fn build_export_manager(matches: &ArgMatches) -> io::Result<ExportManager> {
}
/// Build the commands to benchmark
fn build_commands(matches: &ArgMatches) -> Vec<Command> {
fn build_commands(matches: &ArgMatches) -> Result<Vec<Command>> {
let command_names = matches.values_of("command-name");
let command_strings = matches.values_of("command").unwrap();
+2 -2
View File
@@ -196,7 +196,7 @@ fn test_shell_parse_command() {
OptionsError::ShellParseError(_)
));
assert_eq!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell);
assert!(matches!(Shell::parse("").unwrap_err(), OptionsError::EmptyShell));
assert_eq!(Shell::parse("''").unwrap_err(), OptionsError::EmptyShell);
assert!(matches!(Shell::parse("''").unwrap_err(), OptionsError::EmptyShell));
}