netlink_rust/
errors.rs

1use std::error;
2use std::fmt;
3use std::io;
4use std::result;
5use std::str;
6use std::string;
7
8#[derive(Debug)]
9pub enum NetlinkErrorKind {
10    NotEnoughData,
11    NotFound,
12    InvalidValue,
13    InvalidLength,
14}
15
16#[derive(Debug)]
17pub struct NetlinkError {
18    pub kind: NetlinkErrorKind,
19}
20
21impl NetlinkError {
22    pub fn new(kind: NetlinkErrorKind) -> NetlinkError {
23        NetlinkError { kind: kind }
24    }
25}
26
27impl fmt::Display for NetlinkError {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        write!(f, "NetlinkError {:?}", self.kind)
30    }
31}
32
33impl error::Error for NetlinkError {
34    fn description(&self) -> &str {
35        "NetlinkError"
36    }
37}
38
39/// Errors signaling issues with the Netlink communication
40#[derive(Debug)]
41pub enum Error {
42    /// An std::io error has occured
43    Io(io::Error),
44    /// A str UTF-8 error has occured
45    Utf8(str::Utf8Error),
46    /// An UTF-8 string conversion error has occured
47    FromUtf8(string::FromUtf8Error),
48    /// A Netlink transport error has occured
49    Netlink(NetlinkError),
50}
51
52impl fmt::Display for Error {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        match *self {
55            Error::Io(ref err) => write!(f, "IO error: {}", err),
56            Error::Utf8(ref err) => write!(f, "UTF8 error: {}", err),
57            Error::FromUtf8(ref err) => write!(f, "From UTF8 error: {}", err),
58            Error::Netlink(ref err) => write!(f, "Pack error: {}", err),
59        }
60    }
61}
62
63impl error::Error for Error {
64    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
65        match *self {
66            Error::Io(ref err) => Some(err),
67            Error::Utf8(ref err) => Some(err),
68            Error::FromUtf8(ref err) => Some(err),
69            Error::Netlink(ref err) => Some(err),
70        }
71    }
72}
73
74impl From<io::Error> for Error {
75    fn from(err: io::Error) -> Error {
76        Error::Io(err)
77    }
78}
79
80impl From<NetlinkError> for Error {
81    fn from(err: NetlinkError) -> Error {
82        Error::Netlink(err)
83    }
84}
85
86impl From<str::Utf8Error> for Error {
87    fn from(err: str::Utf8Error) -> Error {
88        Error::Utf8(err)
89    }
90}
91
92impl From<string::FromUtf8Error> for Error {
93    fn from(err: string::FromUtf8Error) -> Error {
94        Error::FromUtf8(err)
95    }
96}
97
98/// Result alias for crate errors
99pub type Result<T> = result::Result<T, Error>;