udevrs/
error.rs

1use std::fmt;
2
3/// Convenience alias for the `udev` library `Result` type.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error types for the `udev` library.
7#[repr(C)]
8#[derive(Clone, Debug, PartialEq)]
9pub enum Error {
10    InvalidLen(usize),
11    Udev(String),
12    UdevDevice(String),
13    UdevHwdb(String),
14    UdevMonitor(String),
15    UdevEnumerate(String),
16    UdevQueue(String),
17    UdevUtil(String),
18    Io {
19        kind: std::io::ErrorKind,
20        err: String,
21    },
22}
23
24impl Error {
25    /// Convenience function to create a I/O error.
26    pub fn io<S: Into<String>>(kind: std::io::ErrorKind, err: S) -> Self {
27        Self::Io {
28            kind,
29            err: err.into(),
30        }
31    }
32
33    /// Convenience function to create a I/O error.
34    pub fn io_other<S: Into<String>>(err: S) -> Self {
35        Self::Io {
36            kind: std::io::ErrorKind::Other,
37            err: err.into(),
38        }
39    }
40
41    /// Gets the [ErrorKind](std::io::ErrorKind).
42    pub fn kind(&self) -> std::io::ErrorKind {
43        match self {
44            Self::Io { kind, .. } => *kind,
45            _ => std::io::ErrorKind::Other,
46        }
47    }
48}
49
50impl From<std::io::Error> for Error {
51    fn from(err: std::io::Error) -> Self {
52        Self::io(err.kind(), format!("{err}"))
53    }
54}
55
56impl From<Error> for std::io::Error {
57    fn from(err: Error) -> Self {
58        match err {
59            Error::Io { kind, err } => Self::new(kind, err),
60            err => Self::new(err.kind(), format!("{err}")),
61        }
62    }
63}
64
65impl From<Error> for std::io::ErrorKind {
66    fn from(err: Error) -> Self {
67        err.kind()
68    }
69}
70
71impl From<std::array::TryFromSliceError> for Error {
72    fn from(err: std::array::TryFromSliceError) -> Self {
73        Self::io_other(format!("{err}"))
74    }
75}
76
77impl From<glob::PatternError> for Error {
78    fn from(err: glob::PatternError) -> Self {
79        Self::io_other(format!("invalid glob pattern: {err}"))
80    }
81}
82
83impl From<std::ffi::NulError> for Error {
84    fn from(err: std::ffi::NulError) -> Self {
85        Self::io_other(format!("invalid FFI C-String: {err}"))
86    }
87}
88
89impl fmt::Display for Error {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            Self::InvalidLen(err) => write!(f, "udev invalid length: {err}"),
93            Self::Udev(err) => write!(f, "udev: {err}"),
94            Self::UdevDevice(err) => write!(f, "udev device: {err}"),
95            Self::UdevHwdb(err) => write!(f, "udev hwdb: {err}"),
96            Self::UdevMonitor(err) => write!(f, "udev monitor: {err}"),
97            Self::UdevEnumerate(err) => write!(f, "udev enumerate: {err}"),
98            Self::UdevQueue(err) => write!(f, "udev queue: {err}"),
99            Self::UdevUtil(err) => write!(f, "udev util: {err}"),
100            Self::Io { kind, err } => write!(f, "I/O: kind: {kind}, error: {err}"),
101        }
102    }
103}
104
105impl std::error::Error for Error {}