1 //! This module is deprecated and types are exported from the top-level of the crate
2 //!
3 //! In futures versions of the crate, this module will no longer be included in the crate.
4
5 use crate::IoctlKind;
6 use std::error::Error as StdError;
7 use std::fmt;
8 use std::io::Error as IOError;
9
10 pub(crate) type Result<T> = std::result::Result<T, Error>;
11
12 #[derive(Debug)]
13 pub struct Error {
14 kind: ErrorKind,
15 }
16
17 #[derive(Debug)]
18 pub enum ErrorKind {
19 Event(nix::Error),
20 Io(IOError),
21 Ioctl { kind: IoctlKind, cause: nix::Error },
22 InvalidRequest(usize, usize),
23 Offset(u32),
24 }
25
ioctl_err(kind: IoctlKind, cause: nix::Error) -> Error26 pub(crate) fn ioctl_err(kind: IoctlKind, cause: nix::Error) -> Error {
27 Error {
28 kind: ErrorKind::Ioctl { kind, cause },
29 }
30 }
31
invalid_err(n_lines: usize, n_values: usize) -> Error32 pub(crate) fn invalid_err(n_lines: usize, n_values: usize) -> Error {
33 Error {
34 kind: ErrorKind::InvalidRequest(n_lines, n_values),
35 }
36 }
37
offset_err(offset: u32) -> Error38 pub(crate) fn offset_err(offset: u32) -> Error {
39 Error {
40 kind: ErrorKind::Offset(offset),
41 }
42 }
43
event_err(err: nix::Error) -> Error44 pub(crate) fn event_err(err: nix::Error) -> Error {
45 Error {
46 kind: ErrorKind::Event(err),
47 }
48 }
49
50 impl fmt::Display for IoctlKind {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 match *self {
53 IoctlKind::ChipInfo => write!(f, "get chip info"),
54 IoctlKind::LineInfo => write!(f, "get line info"),
55 IoctlKind::LineHandle => write!(f, "get line handle"),
56 IoctlKind::LineEvent => write!(f, "get line event "),
57 IoctlKind::GetLine => write!(f, "get line value"),
58 IoctlKind::SetLine => write!(f, "set line value"),
59 }
60 }
61 }
62
63 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 match &self.kind {
66 ErrorKind::Event(err) => write!(f, "Failed to read event: {}", err),
67 ErrorKind::Io(err) => err.fmt(f),
68 ErrorKind::Ioctl { cause, kind } => write!(f, "Ioctl to {} failed: {}", kind, cause),
69 ErrorKind::InvalidRequest(n_lines, n_values) => write!(
70 f,
71 "Invalid request: {} values requested to be set but only {} lines are open",
72 n_values, n_lines
73 ),
74 ErrorKind::Offset(offset) => write!(f, "Offset {} is out of range", offset),
75 }
76 }
77 }
78
79 impl StdError for Error {
source(&self) -> Option<&(dyn StdError + 'static)>80 fn source(&self) -> Option<&(dyn StdError + 'static)> {
81 match &self.kind {
82 ErrorKind::Event(err) => Some(err),
83 ErrorKind::Io(err) => Some(err),
84 ErrorKind::Ioctl { kind: _, cause } => Some(cause),
85 _ => None,
86 }
87 }
88 }
89
90 impl From<IOError> for Error {
from(err: IOError) -> Self91 fn from(err: IOError) -> Self {
92 Self {
93 kind: ErrorKind::Io(err),
94 }
95 }
96 }
97