pub use std::{
error::Error as StdError,
fmt::{Display, Formatter},
path::PathBuf,
};
use crate::units::Error as RawError;
use super::SensorSubFunctionType;
pub(super) type Result<T> = std::result::Result<T, Error>;
#[allow(missing_docs)]
#[derive(Debug)]
pub enum Error {
Read {
source: std::io::Error,
path: PathBuf,
},
Write {
source: std::io::Error,
path: PathBuf,
},
RawError { source: RawError },
InsufficientRights { path: PathBuf },
SubtypeNotSupported { sub_type: SensorSubFunctionType },
FaultySensor,
DisabledSensor,
}
impl StdError for Error {
fn cause(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::Read { source, .. } => Some(source),
Error::Write { source, .. } => Some(source),
Error::RawError { source } => Some(source),
Error::InsufficientRights { .. } => None,
Error::SubtypeNotSupported { .. } => None,
Error::FaultySensor => None,
Error::DisabledSensor => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Read { path, .. } => {
write!(f, "Reading from sensor at {} failed", path.display())
}
Error::Write { path, .. } => {
write!(f, "Writing to sensor at {} failed", path.display())
}
Error::RawError { .. } => write!(f, "Raw sensor error"),
Error::InsufficientRights { path } => write!(
f,
"You have insufficient rights to read/write {}",
path.display()
),
Error::SubtypeNotSupported { sub_type } => {
write!(f, "Sensor does not support the subtype {}", sub_type)
}
Error::FaultySensor => write!(f, "The sensor is faulty"),
Error::DisabledSensor => write!(f, "The sensor is disabled"),
}
}
}
impl From<RawError> for Error {
fn from(raw_error: RawError) -> Error {
Error::RawError { source: raw_error }
}
}