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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std;
use std::result;
#[allow(unused_imports)]
use log::{debug, info, warn, trace, error};
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
BadParameter { reason: String },
Unsupported,
OutOfResources,
NotEnabled,
ImmutablePolicy,
InconsistentPolicy { reason: String },
PreconditionNotMet { precondition: String },
IllegalOperation { reason: String },
LockPoisoned,
Internal { reason: String },
Io { inner: std::io::Error },
Serialization { reason: String },
Discovery {reason: String},
}
impl Error {
pub fn bad_parameter<T>(reason: &str) -> Result<T>
{
Err( Error::BadParameter{ reason: reason.to_string() })
}
pub fn precondition_not_met<T>(precondition: &str) -> Result<T>
{
Err( Error::PreconditionNotMet{ precondition: precondition.to_string() })
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_precondition_not_met {
($err_msg:literal) => (
{ error!($err_msg);
Error::precondition_not_met($err_msg)
}
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_internal {
($($arg:tt)*) => (
{ error!($($arg)*);
Err( Error::Internal{ reason: format!($($arg)*) } )
}
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_discovery {
($($arg:tt)*) => (
{ error!($($arg)*);
Error::Message(format!($($arg)*) )
}
)
}
impl From<std::io::Error> for Error {
fn from(e:std::io::Error) -> Error {
Error::Io { inner: e }
}
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(_e:std::sync::PoisonError<T>) -> Error {
Error::LockPoisoned
}
}