use std::{fmt, io::ErrorKind};
use nix::errno::Errno;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SyslogErrCode
{
OsError(ErrorKind),
OsErrorErrno(Errno),
InternalError,
SyslogThreadNotAvailable,
UnboundedChannelError,
MutexPoisoned,
Unusable,
SendError,
NotAvail,
}
impl fmt::Display for SyslogErrCode
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self
{
Self::OsError(error_kind) =>
write!(f, "[ErrorKind: {}]", error_kind),
Self::InternalError =>
write!(f, "[InternalError]"),
Self::SyslogThreadNotAvailable =>
write!(f, "[SyslogThreadNotAvailable]"),
Self::UnboundedChannelError =>
write!(f, "[UnboundedChannelError]"),
Self::MutexPoisoned =>
write!(f, "[MutexPoisoned]"),
Self::OsErrorErrno(errn) =>
write!(f, "[Errno: {}]", errn),
Self::Unusable =>
write!(f, "[Unusable]"),
Self::SendError =>
write!(f, "[SendError]"),
Self::NotAvail =>
write!(f, "[NotAvail]"),
}
}
}
impl SyslogErrCode
{
pub
fn get_os_err_code(&self) -> Option<ErrorKind>
{
match *self
{
Self::OsError(errn) =>
return Some(errn),
_ =>
return None,
}
}
}
#[derive(Debug)]
pub struct SyslogError
{
errcode: SyslogErrCode,
message: String,
}
impl SyslogError
{
pub(crate)
fn new(errcode: SyslogErrCode, msg: String) -> Self
{
return SyslogError{errcode: errcode, message: msg};
}
pub(crate)
fn new_io(err: &std::io::Error, msg: String) -> Self
{
return SyslogError{errcode: SyslogErrCode::OsError(err.kind()), message: msg};
}
pub(crate)
fn new_errno(err: Errno, msg: String) -> Self
{
return SyslogError{errcode: SyslogErrCode::OsErrorErrno(err), message: msg};
}
pub
fn get_errcode(&self) -> SyslogErrCode
{
return self.errcode;
}
pub
fn into_inner(self) -> String
{
return self.message;
}
}
impl fmt::Display for SyslogError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "{} {}", self.errcode, self.message)
}
}
pub type SyRes<T> = Result<T, SyslogError>;
#[macro_export]
macro_rules! throw_error
{
($($arg:tt)*) => (
return std::result::Result::Err(crate::error::SyslogError::new(crate::error::SyslogErrCode::InternalError, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! throw_error_os
{
($err:expr, $($arg:tt)*) => (
return std::result::Result::Err(crate::error::SyslogError::new_io(&$err, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! throw_error_errno
{
($errno:expr, $($arg:tt)*) => (
return std::result::Result::Err(crate::error::SyslogError::new_errno($errno, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! map_error
{
($($arg:tt)*) => (
crate::error::SyslogError::new(crate::error::SyslogErrCode::InternalError, format!($($arg)*))
)
}
#[macro_export]
macro_rules! map_error_os
{
($err:expr, $($arg:tt)*) => (
crate::error::SyslogError::new_io(&$err, format!($($arg)*))
)
}
#[macro_export]
macro_rules! throw_error_code
{
($code:tt, $($arg:tt)*) => (
return std::result::Result::Err(crate::error::SyslogError::new(crate::error::SyslogErrCode::$code, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! map_error_code
{
($code:tt, $($arg:tt)*) => (
crate::error::SyslogError::new(crate::error::SyslogErrCode::$code, format!($($arg)*))
)
}