rusty-keys/src/error.rs

94 lines
1.6 KiB
Rust
Raw Normal View History

2016-04-30 16:54:13 -04:00
use std::fmt;
use std::error;
2016-04-24 22:32:24 -04:00
use std::ffi;
use std::io;
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
use std::sync::mpsc;
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
2016-04-24 22:32:24 -04:00
use nix;
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
use libc;
2016-04-25 11:57:45 -04:00
/// UInput error.
2016-04-24 22:32:24 -04:00
#[derive(Debug)]
pub enum Error {
2016-04-25 11:57:45 -04:00
/// System errors.
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
2016-04-24 22:32:24 -04:00
Nix(nix::Error),
2016-04-25 11:57:45 -04:00
/// Errors with internal nulls in names.
2016-04-24 22:32:24 -04:00
Nul(ffi::NulError),
Io(io::Error),
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
Send(mpsc::SendError<libc::input_event>),
2016-04-25 11:57:45 -04:00
/// The uinput file could not be found.
2016-04-24 22:32:24 -04:00
NotFound,
/// error reading input_event
ShortRead,
2016-04-24 22:32:24 -04:00
}
impl From<ffi::NulError> for Error {
fn from(value: ffi::NulError) -> Self {
Error::Nul(value)
}
}
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
2016-04-24 22:32:24 -04:00
impl From<nix::Error> for Error {
fn from(value: nix::Error) -> Self {
Error::Nix(value)
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Error::Io(value)
}
}
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
impl From<mpsc::SendError<libc::input_event>> for Error {
fn from(value: mpsc::SendError<libc::input_event>) -> Self {
Error::Send(value)
}
}
2016-04-30 16:54:13 -04:00
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(error::Error::description(self))
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match self {
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
2016-04-30 16:54:13 -04:00
&Error::Nix(ref err) =>
err.description(),
&Error::Nul(ref err) =>
err.description(),
&Error::Io(ref err) =>
err.description(),
2019-10-07 01:24:29 -04:00
#[cfg(target_os = "linux")]
&Error::Send(ref err) =>
err.description(),
2016-04-30 16:54:13 -04:00
&Error::NotFound =>
"Device not found.",
&Error::ShortRead =>
"Error while reading from device file.",
2016-04-30 16:54:13 -04:00
}
}
}