rusty_keys/
error.rs

1use std::fmt;
2use std::error;
3use std::ffi;
4use nix;
5
6#[cfg(feature = "udev")]
7use udev;
8
9/// UInput error.
10#[derive(Debug)]
11pub enum Error {
12	/// System errors.
13	Nix(nix::Error),
14
15	/// Errors with internal nulls in names.
16	Nul(ffi::NulError),
17
18	#[cfg(feature = "udev")]
19	/// Errors coming from udev.
20	Udev(udev::Error),
21
22	/// The uinput file could not be found.
23	NotFound,
24}
25
26impl From<ffi::NulError> for Error {
27	fn from(value: ffi::NulError) -> Self {
28		Error::Nul(value)
29	}
30}
31
32impl From<nix::Error> for Error {
33	fn from(value: nix::Error) -> Self {
34		Error::Nix(value)
35	}
36}
37
38#[cfg(feature = "udev")]
39impl From<udev::Error> for Error {
40	fn from(value: udev::Error) -> Self {
41		Error::Udev(value)
42	}
43}
44
45impl fmt::Display for Error {
46	fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
47		f.write_str(error::Error::description(self))
48	}
49}
50
51impl error::Error for Error {
52	fn description(&self) -> &str {
53		match self {
54			&Error::Nix(ref err) =>
55				err.description(),
56
57			&Error::Nul(ref err) =>
58				err.description(),
59
60			#[cfg(feature = "udev")]
61			&Error::Udev(ref err) =>
62				err.description(),
63
64			&Error::NotFound =>
65				"Device not found.",
66		}
67	}
68}