Files
hyperfine/src/error.rs
T

91 lines
3.1 KiB
Rust
Raw Normal View History

2019-08-31 12:48:26 +05:30
use rust_decimal::Error as DecimalError;
use std::error::Error;
use std::fmt;
2018-05-22 22:57:51 +02:00
use std::num;
use std::num::ParseIntError;
#[derive(Debug)]
2018-03-24 20:17:17 +01:00
pub enum ParameterScanError {
ParseIntError(num::ParseIntError),
2019-08-31 12:48:26 +05:30
ParseDecimalError(DecimalError),
EmptyRange,
TooLarge,
ZeroStep,
2019-08-31 12:48:26 +05:30
StepRequired,
UnexpectedCommandNameCount(usize, usize),
}
2018-03-24 20:17:17 +01:00
impl From<num::ParseIntError> for ParameterScanError {
fn from(e: num::ParseIntError) -> ParameterScanError {
ParameterScanError::ParseIntError(e)
}
}
2019-08-31 12:48:26 +05:30
impl From<DecimalError> for ParameterScanError {
fn from(e: DecimalError) -> ParameterScanError {
ParameterScanError::ParseDecimalError(e)
}
}
2018-03-24 20:17:17 +01:00
impl fmt::Display for ParameterScanError {
2019-05-07 11:55:38 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
2019-12-15 23:47:53 +01:00
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,
2019-09-01 16:00:53 +05:30
"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"
2019-12-15 23:47:53 +01:00
),
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
)
}
}
}
}
2019-12-15 23:47:53 +01:00
impl Error for ParameterScanError {}
#[derive(Debug)]
pub enum OptionsError<'a> {
RunsBelowTwo,
EmptyRunsRange,
TooManyCommandNames(usize),
UnexpectedCommandNameCount(usize, usize),
NumericParsingError(&'a str, ParseIntError),
}
impl<'a> fmt::Display for OptionsError<'a> {
2019-05-07 11:55:38 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
2019-12-15 23:47:53 +01:00
OptionsError::EmptyRunsRange => write!(f, "Empty runs range"),
OptionsError::RunsBelowTwo => write!(f, "Number of runs below two"),
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
),
}
}
}
2019-12-15 23:47:53 +01:00
impl<'a> Error for OptionsError<'a> {}