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