use std::{
fmt,
num::{ParseFloatError, ParseIntError},
path::PathBuf,
};
pub type CollectResult<T> = std::result::Result<T, MetricError>;
#[derive(Debug)]
pub enum MetricError {
DmiSupportError,
IOError(PathBuf, std::io::Error),
SerdeJsonError(serde_json::Error),
ParseIntError(String, ParseIntError),
ParseFloatError(String, ParseFloatError),
ByteConvertError(String),
InvalidFieldNumberError(String, usize, String),
ProcessNotFound(usize),
PathNotFound(PathBuf),
RegexError(regex::Error),
ParseError(String),
}
impl fmt::Display for MetricError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MetricError::DmiSupportError => write!(
f,
"platform does not support Desktop Management Interface (DMI) information",
),
MetricError::IOError(ref p, ref e) => {
write!(f, "cannot read sysfs {:?}: {}", p, e)
}
MetricError::SerdeJsonError(ref e) => write!(f, "json pretty error: {}", e),
MetricError::ParseIntError(ref item, ref e) => {
write!(f, "{} parse {} int error", item, e)
}
MetricError::ParseFloatError(ref item, ref e) => {
write!(f, "{} parse {} float error", item, e)
}
MetricError::ProcessNotFound(ref pid) => write!(f, "process (pid={}) not found", pid),
MetricError::PathNotFound(ref p) => write!(f, "path ({:?}) not found", p),
MetricError::ByteConvertError(ref unit) => write!(f, "invalid unit: {}", unit),
MetricError::ParseError(ref msg) => write!(f, "parse error: {}", msg),
MetricError::RegexError(ref e) => write!(f, "regex error: {}", e),
MetricError::InvalidFieldNumberError(ref title, ref num, ref fields) => {
write!(f, "invalid {} fields number {}: {:?}", title, num, fields)
}
}
}
}