pub use std::{
error::Error as StdError,
fmt::{Display, Formatter},
io::Error as IoError,
path::PathBuf,
};
use crate::sensors::SensorSubFunctionType;
use crate::units::Error as UnitError;
pub(super) type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error
where
Self: Send, {
Read {
source: IoError,
path: PathBuf,
},
Write {
source: IoError,
path: PathBuf,
},
UnitError {
source: UnitError,
},
InsufficientRights {
path: PathBuf,
},
SubtypeNotSupported {
sub_type: SensorSubFunctionType,
},
FaultySensor,
DisabledSensor,
#[cfg(feature = "virtual_sensors")]
CustomVirtual {
source: Box<dyn StdError + Send>,
},
}
impl Error {
pub(crate) fn read(source: IoError, path: impl Into<PathBuf>) -> Self {
Self::Read {
source,
path: path.into(),
}
}
#[cfg(feature = "writeable")]
pub(crate) fn write(source: IoError, path: impl Into<PathBuf>) -> Self {
Self::Write {
source,
path: path.into(),
}
}
pub(crate) fn insufficient_rights(path: impl Into<PathBuf>) -> Self {
Self::InsufficientRights { path: path.into() }
}
pub(crate) fn subtype_not_supported(sub_type: SensorSubFunctionType) -> Self {
Self::SubtypeNotSupported { sub_type }
}
#[cfg(feature = "virtual_sensors")]
pub(crate) fn custom_virtual(source: impl StdError + Send + 'static) -> Self {
Self::CustomVirtual {
source: Box::new(source),
}
}
}
impl StdError for Error {
fn cause(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::Read { source, .. } => Some(source),
Error::Write { source, .. } => Some(source),
Error::UnitError { source } => Some(source),
Error::InsufficientRights { .. } => None,
Error::SubtypeNotSupported { .. } => None,
Error::FaultySensor => None,
Error::DisabledSensor => None,
#[cfg(feature = "virtual_sensors")]
Error::CustomVirtual { source } => Some(source.as_ref()),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Read { path, source } => write!(
f,
"Reading from sensor at {} failed: {}",
path.display(),
source
),
Error::Write { path, source } => write!(
f,
"Writing to sensor at {} failed: {}",
path.display(),
source
),
Error::UnitError { source } => write!(f, "Raw sensor error: {}", source),
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"),
#[cfg(feature = "virtual_sensors")]
Error::CustomVirtual { source } => write!(f, "Custom virtual sensor error: {}", source),
}
}
}
impl From<UnitError> for Error {
fn from(raw_error: UnitError) -> Error {
Error::UnitError { source: raw_error }
}
}