libmedium 0.13.4

Library to interface with lm_sensors
Documentation
use crate::sensors::Error as SensorError;

use std::{
    error::Error as StdError,
    fmt::{Display, Formatter},
    io::Error as IoError,
    num::ParseIntError,
    path::PathBuf,
};

pub(crate) type Result<T> = std::result::Result<T, Error>;

#[allow(missing_docs)]
#[derive(Debug)]
pub enum Error {
    /// Error listing hwmons
    Hwmons { source: IoError, path: PathBuf },

    /// Error reading hwmon name file
    HwmonName { source: IoError, path: PathBuf },

    /// Error reading hwmon label file
    HwmonLabel { source: IoError, path: PathBuf },

    /// Error listing the contents of the hwmon directory
    HwmonDir { source: IoError, path: PathBuf },

    /// Error parsing the hwmon's index
    HwmonIndex {
        source: ParseIntError,
        path: PathBuf,
    },

    /// Error parsing sensor
    Sensor { source: SensorError },
}

impl Error {
    pub(crate) fn hwmons(source: IoError, path: impl Into<PathBuf>) -> Self {
        let path = path.into();

        Error::Hwmons { source, path }
    }

    pub(crate) fn hwmon_name(source: IoError, path: impl Into<PathBuf>) -> Self {
        let path = path.into();

        Error::HwmonName { source, path }
    }

    pub(crate) fn hwmon_label(source: IoError, path: impl Into<PathBuf>) -> Self {
        let path = path.into();

        Error::HwmonLabel { source, path }
    }

    pub(crate) fn hwmon_dir(source: IoError, path: impl Into<PathBuf>) -> Self {
        let path = path.into();

        Error::HwmonDir { source, path }
    }

    pub(crate) fn hwmon_index(source: ParseIntError, path: impl Into<PathBuf>) -> Self {
        let path = path.into();

        Error::HwmonIndex { source, path }
    }

    pub(crate) fn sensor(source: SensorError) -> Self {
        Error::Sensor { source }
    }
}

impl StdError for Error {
    fn cause(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::Hwmons { source, .. } => Some(source),
            Error::HwmonName { source, .. } => Some(source),
            Error::HwmonLabel { source, .. } => Some(source),
            Error::HwmonDir { source, .. } => Some(source),
            Error::HwmonIndex { source, .. } => Some(source),
            Error::Sensor { source } => Some(source),
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Hwmons { source, path } => {
                write!(f, "Error listing hwmons at {}: {}", path.display(), source)
            }
            Error::HwmonName { source, path } => write!(
                f,
                "Error reading hwmon name file at {}: {}",
                path.display(),
                source
            ),
            Error::HwmonLabel { source, path } => write!(
                f,
                "Error reading hwmon label file at {}: {}",
                path.display(),
                source
            ),
            Error::HwmonDir { source, path } => write!(
                f,
                "Error listing contents of hwmon directory at {}: {}",
                path.display(),
                source
            ),
            Error::HwmonIndex { source, path } => write!(
                f,
                "Error parsing index of hwmon at {}: {}",
                path.display(),
                source
            ),
            Error::Sensor { source } => {
                write!(f, "Error parsing sensor: {}", source)
            }
        }
    }
}

impl From<SensorError> for Error {
    fn from(value: SensorError) -> Self {
        Error::sensor(value)
    }
}