Skip to main content

teaql_provider_linux/
error.rs

1use std::fmt;
2
3/// Errors produced by the Linux provider.
4#[derive(Debug)]
5pub enum LinuxProviderError {
6    /// The requested entity name is not supported by this provider.
7    UnknownEntity(String),
8    /// An error originating from the procfs library.
9    ProcFs(String),
10    /// A standard I/O error.
11    Io(std::io::Error),
12}
13
14impl fmt::Display for LinuxProviderError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            Self::UnknownEntity(name) => write!(f, "unknown entity: {name}"),
18            Self::ProcFs(msg) => write!(f, "procfs error: {msg}"),
19            Self::Io(err) => write!(f, "I/O error: {err}"),
20        }
21    }
22}
23
24impl std::error::Error for LinuxProviderError {
25    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
26        match self {
27            Self::Io(err) => Some(err),
28            _ => None,
29        }
30    }
31}
32
33impl From<std::io::Error> for LinuxProviderError {
34    fn from(err: std::io::Error) -> Self {
35        Self::Io(err)
36    }
37}
38
39impl From<procfs::ProcError> for LinuxProviderError {
40    fn from(err: procfs::ProcError) -> Self {
41        Self::ProcFs(err.to_string())
42    }
43}