1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::{
error::Error as StdError,
fmt::{self, Debug},
io,
};
use netlink_packet_core::NetlinkMessage;
#[derive(Debug)]
pub struct Error<T>
where
T: Debug + Eq + PartialEq + Clone,
{
kind: ErrorKind<T>,
}
impl<T> Error<T>
where
T: Debug + Eq + PartialEq + Clone,
{
pub fn kind(&self) -> &ErrorKind<T> {
&self.kind
}
pub fn into_inner(self) -> ErrorKind<T> {
self.kind
}
}
#[derive(Debug)]
pub enum ErrorKind<T>
where
T: Debug + Eq + PartialEq + Clone,
{
ConnectionClosed,
NetlinkError(NetlinkMessage<T>),
SocketIo(io::Error),
}
impl<T> From<ErrorKind<T>> for Error<T>
where
T: Debug + Eq + PartialEq + Clone,
{
fn from(kind: ErrorKind<T>) -> Error<T> {
Error { kind }
}
}
impl<T> fmt::Display for Error<T>
where
T: Debug + Eq + PartialEq + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::ErrorKind::*;
match self.kind() {
SocketIo(ref e) => write!(f, "{}: {}", self, e),
ConnectionClosed => write!(f, "{}", self),
NetlinkError(ref message) => write!(f, "{}: {:?}", self, message),
}
}
}
impl<T> StdError for Error<T>
where
T: Debug + Eq + PartialEq + Clone,
{
fn description(&self) -> &str {
use crate::ErrorKind::*;
match self.kind() {
SocketIo(_) => "Error while reading from or writing to the netlink socket",
ConnectionClosed => "The netlink connection is closed",
NetlinkError(_) => "Received an error message as a response",
}
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
if let ErrorKind::SocketIo(ref e) = self.kind() {
Some(e)
} else {
None
}
}
}