1use std::fmt;
14
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub enum HaProxErrType
17{
18 ArgumentEinval,
20
21 IoError,
23
24 MalformedData,
27
28 ProtocolNotSuported,
30
31 IncorrectBanner,
33
34 ProtocolMsgIncomplete,
36
37 ProtocolUnknownData
39}
40
41impl fmt::Display for HaProxErrType
42{
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
44 {
45 match self
46 {
47 HaProxErrType::ArgumentEinval =>
48 write!(f, "EINVAL"),
49 HaProxErrType::IoError =>
50 write!(f, "IOERROR"),
51 HaProxErrType::MalformedData =>
52 write!(f, "MALFORMED_DATA"),
53 HaProxErrType::ProtocolNotSuported =>
54 write!(f, "PROTO_NOT_SUPPORTED"),
55 HaProxErrType::IncorrectBanner =>
56 write!(f, "INCORRECT_BANNER"),
57 HaProxErrType::ProtocolMsgIncomplete =>
58 write!(f, "MESSAGE_INCOMPLETE"),
59 HaProxErrType::ProtocolUnknownData =>
60 write!(f, "UNKNOWN_DATA"),
61 }
62 }
63}
64
65#[derive(Clone, Debug, PartialEq, Eq)]
67pub struct HaProxErr
68{
69 err_type: HaProxErrType,
71
72 msg: String,
74}
75
76impl fmt::Display for HaProxErr
77{
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
79 {
80 write!(f, "[{}]: {}", self.err_type, self.msg)
81 }
82}
83
84impl HaProxErr
85{
86 pub
88 fn new(err_type: HaProxErrType, msg: String) -> Self
89 {
90 return Self{ err_type: err_type, msg: msg };
91 }
92
93 pub
95 fn get_err_type(&self) -> HaProxErrType
96 {
97 return self.err_type;
98 }
99
100 pub
102 fn get_msg(&self) -> &str
103 {
104 return &self.msg;
105 }
106}
107
108pub type HaProxRes<R> = Result<R, HaProxErr>;
109
110
111#[macro_export]
112macro_rules! return_error
113{
114 ($err_type:tt, $($arg:tt)*) => (
115 return std::result::Result::Err($crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*)))
116 )
117}
118
119#[macro_export]
120macro_rules! map_error
121{
122 ($err_type:tt, $($arg:tt)*) => (
123 $crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*))
124 )
125}