diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/libudev/src/error.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/libudev/src/error.rs')
-rw-r--r-- | third_party/rust/libudev/src/error.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/third_party/rust/libudev/src/error.rs b/third_party/rust/libudev/src/error.rs new file mode 100644 index 0000000000..2c503149c8 --- /dev/null +++ b/third_party/rust/libudev/src/error.rs @@ -0,0 +1,76 @@ +use std::ffi::CStr; +use std::fmt; +use std::io; +use std::str; + +use std::error::Error as StdError; +use std::result::Result as StdResult; + +use ::libc::c_int; + +/// A `Result` type for libudev operations. +pub type Result<T> = StdResult<T,Error>; + +/// Types of errors that occur in libudev. +#[derive(Debug,Clone,Copy,PartialEq,Eq)] +pub enum ErrorKind { + NoMem, + InvalidInput, + Io(io::ErrorKind) +} + +/// The error type for libudev operations. +#[derive(Debug)] +pub struct Error { + errno: c_int, +} + +impl Error { + fn strerror(&self) -> &str { + unsafe { + str::from_utf8_unchecked(CStr::from_ptr(::libc::strerror(self.errno)).to_bytes()) + } + } + + /// Returns the corresponding `ErrorKind` for this error. + pub fn kind(&self) -> ErrorKind { + match self.errno { + ::libc::ENOMEM => ErrorKind::NoMem, + ::libc::EINVAL => ErrorKind::InvalidInput, + errno => ErrorKind::Io(io::Error::from_raw_os_error(errno).kind()), + } + } + + /// Returns a description of the error. + pub fn description(&self) -> &str { + self.strerror() + } +} + +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter) -> StdResult<(),fmt::Error> { + fmt.write_str(self.strerror()) + } +} + +impl StdError for Error { + fn description(&self) -> &str { + self.strerror() + } +} + +impl From<Error> for io::Error { + fn from(error: Error) -> io::Error { + let io_error_kind = match error.kind() { + ErrorKind::Io(kind) => kind, + ErrorKind::InvalidInput => io::ErrorKind::InvalidInput, + ErrorKind::NoMem => io::ErrorKind::Other, + }; + + io::Error::new(io_error_kind, error.strerror()) + } +} + +pub fn from_errno(errno: c_int) -> Error { + Error { errno: -errno } +} |