Skip to main content

sysinfo/
errors.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{error, fmt, io};
4
5/// Error type covering the main errors which can happen when querying system information.
6#[derive(Debug)]
7pub enum Error {
8    /// This feature is not supported on this platform.
9    Unsupported,
10    /// An IO error happened when querying the information.
11    Io(io::Error),
12    /// Any non-IO error on supported platforms.
13    Other(Box<dyn error::Error + Send + Sync + 'static>),
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        match self {
19            Error::Unsupported => f.write_str("unsupported"),
20            Error::Io(e) => fmt::Display::fmt(e, f),
21            Error::Other(e) => fmt::Display::fmt(e, f),
22        }
23    }
24}
25
26impl error::Error for Error {
27    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
28        match self {
29            Error::Io(e) => Some(e),
30            Error::Other(e) => Some(&**e),
31            _ => None,
32        }
33    }
34}
35
36impl From<io::Error> for Error {
37    fn from(e: io::Error) -> Self {
38        Error::Io(e)
39    }
40}
41
42impl From<&'static str> for Error {
43    fn from(e: &'static str) -> Self {
44        Error::Other(e.into())
45    }
46}