1use std::{fmt, io::ErrorKind};
16
17use nix::errno::Errno;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum SyslogErrCode
22{
23 OsError(ErrorKind),
25
26 OsErrorErrno(Errno),
28
29 InternalError,
31
32 SyslogThreadNotAvailable,
35
36 UnboundedChannelError,
39
40 MutexPoisoned,
42
43 Unusable,
45
46 SendError,
48
49 NotAvail,
51
52 CowTransactionFailed,
53
54 NoCowLockFailed,
55}
56
57impl fmt::Display for SyslogErrCode
58{
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
60 {
61 match self
62 {
63 Self::OsError(error_kind) =>
64 write!(f, "[ErrorKind: {}]", error_kind),
65 Self::InternalError =>
66 write!(f, "[InternalError]"),
67 Self::SyslogThreadNotAvailable =>
68 write!(f, "[SyslogThreadNotAvailable]"),
69 Self::UnboundedChannelError =>
70 write!(f, "[UnboundedChannelError]"),
71 Self::MutexPoisoned =>
72 write!(f, "[MutexPoisoned]"),
73 Self::OsErrorErrno(errn) =>
74 write!(f, "[Errno: {}]", errn),
75 Self::Unusable =>
76 write!(f, "[Unusable]"),
77 Self::SendError =>
78 write!(f, "[SendError]"),
79 Self::NotAvail =>
80 write!(f, "[NotAvail]"),
81 Self::CowTransactionFailed =>
82 write!(f, "[CowTransactionFailed]"),
83 Self::NoCowLockFailed =>
84 write!(f, "[NoCowLockFailed]"),
85 }
86 }
87}
88
89impl SyslogErrCode
90{
91 pub
92 fn get_os_err_code(&self) -> Option<ErrorKind>
93 {
94 match *self
95 {
96 Self::OsError(errn) =>
97 return Some(errn),
98 _ =>
99 return None,
100 }
101 }
102}
103
104#[derive(Debug)]
106pub struct SyslogError
107{
108 errcode: SyslogErrCode,
109 message: String,
110}
111
112impl SyslogError
113{
114 pub
116 fn new(errcode: SyslogErrCode, msg: String) -> Self
117 {
118 return SyslogError{errcode: errcode, message: msg};
119 }
120
121 pub
123 fn new_io(err: &std::io::Error, msg: String) -> Self
124 {
125 return SyslogError{errcode: SyslogErrCode::OsError(err.kind()), message: msg};
126 }
127
128 pub
129 fn new_errno(err: Errno, msg: String) -> Self
130 {
131 return SyslogError{errcode: SyslogErrCode::OsErrorErrno(err), message: msg};
132 }
133
134 pub
136 fn get_errcode(&self) -> SyslogErrCode
137 {
138 return self.errcode;
139 }
140
141 pub
143 fn into_inner(self) -> String
144 {
145 return self.message;
146 }
147}
148
149impl fmt::Display for SyslogError
150{
151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
152 {
153 write!(f, "{} {}", self.errcode, self.message)
154 }
155}
156
157pub type SyRes<T> = Result<T, SyslogError>;
158
159#[macro_export]
160macro_rules! throw_error
161{
162 ($($arg:tt)*) => (
163 return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*)))
164 )
165}
166
167#[macro_export]
168macro_rules! throw_error_os
169{
170 ($err:expr, $($arg:tt)*) => (
171 return std::result::Result::Err($crate::error::SyslogError::new_io(&$err, format!($($arg)*)))
172 )
173}
174
175#[macro_export]
176macro_rules! throw_error_errno
177{
178 ($errno:expr, $($arg:tt)*) => (
179 return std::result::Result::Err($crate::error::SyslogError::new_errno($errno, format!($($arg)*)))
180 )
181}
182
183#[macro_export]
184macro_rules! map_error
185{
186 ($($arg:tt)*) => (
187 $crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*))
188 )
189}
190
191#[macro_export]
192macro_rules! map_error_os
193{
194 ($err:expr, $($arg:tt)*) => (
195 $crate::error::SyslogError::new_io(&$err, format!($($arg)*))
196 )
197}
198
199#[macro_export]
200macro_rules! throw_error_code
201{
202 ($code:tt, $($arg:tt)*) => (
203 return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*)))
204 )
205}
206
207#[macro_export]
208macro_rules! map_error_code
209{
210 ($code:tt, $($arg:tt)*) => (
211 $crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*))
212 )
213}