libmedium/hwmon/
error.rs

1use std::{
2    error::Error as StdError,
3    fmt::{Display, Formatter},
4    io::Error as IoError,
5    path::PathBuf,
6};
7
8use crate::units::Error as UnitError;
9
10pub(crate) type Result<T> = std::result::Result<T, Error>;
11
12/// Error returned from interacting with an hwmon.
13#[derive(Debug)]
14pub enum Error {
15    /// The hwmon does not expose an update interval.
16    UpdateIntervalNotAvailable,
17
18    /// The hwmon does not expose the beep_enable functionality.
19    BeepEnable,
20
21    /// Error reading or writing to sysfs.
22    Io {
23        /// The source of the error.
24        source: IoError,
25        /// The path where the error occurred.
26        path: PathBuf,
27    },
28
29    /// Unit conversion error.
30    Unit {
31        /// The source of the error.
32        source: UnitError,
33        /// The path where the error occurred.
34        path: PathBuf,
35    },
36
37    /// You have insufficient rights.
38    InsufficientRights {
39        /// The path where the error occurred.
40        path: PathBuf,
41    },
42}
43
44impl Error {
45    pub(crate) fn update_interval_not_available() -> Self {
46        Self::UpdateIntervalNotAvailable
47    }
48
49    pub(crate) fn beep_enable() -> Self {
50        Self::BeepEnable
51    }
52
53    pub(crate) fn io(source: IoError, path: impl Into<PathBuf>) -> Self {
54        let path = path.into();
55
56        Error::Io { source, path }
57    }
58
59    pub(crate) fn unit(source: UnitError, path: impl Into<PathBuf>) -> Self {
60        let path = path.into();
61
62        Error::Unit { source, path }
63    }
64
65    #[cfg(feature = "writeable")]
66    pub(crate) fn insufficient_rights(path: impl Into<PathBuf>) -> Self {
67        Self::InsufficientRights { path: path.into() }
68    }
69}
70
71impl StdError for Error {
72    fn cause(&self) -> Option<&(dyn StdError + 'static)> {
73        match self {
74            Error::UpdateIntervalNotAvailable => None,
75            Error::BeepEnable => None,
76            Error::Io { source, .. } => Some(source),
77            Error::Unit { source, .. } => Some(source),
78            Error::InsufficientRights { .. } => None,
79        }
80    }
81}
82
83impl Display for Error {
84    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
85        match self {
86            Error::UpdateIntervalNotAvailable => {
87                write!(f, "Hwmon does not expose an update interval")
88            }
89            Error::BeepEnable => {
90                write!(f, "Hwmon does not expose the beep_enable functionality")
91            }
92            Error::Unit { source, path } => {
93                write!(f, "Unit conversion error at {}: {}", path.display(), source)
94            }
95            Error::Io { source, path } => {
96                write!(f, "Io error at {}: {}", path.display(), source)
97            }
98            Error::InsufficientRights { path } => {
99                write!(
100                    f,
101                    "You have insufficient rights to write to {}",
102                    path.display()
103                )
104            }
105        }
106    }
107}