use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum HaProxErrType
{
ArgumentEinval,
IoError,
MalformedData,
ProtocolNotSuported,
IncorrectBanner,
ProtocolMsgIncomplete,
ProtocolUnknownData
}
impl fmt::Display for HaProxErrType
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self
{
HaProxErrType::ArgumentEinval =>
write!(f, "EINVAL"),
HaProxErrType::IoError =>
write!(f, "IOERROR"),
HaProxErrType::MalformedData =>
write!(f, "MALFORMED_DATA"),
HaProxErrType::ProtocolNotSuported =>
write!(f, "PROTO_NOT_SUPPORTED"),
HaProxErrType::IncorrectBanner =>
write!(f, "INCORRECT_BANNER"),
HaProxErrType::ProtocolMsgIncomplete =>
write!(f, "MESSAGE_INCOMPLETE"),
HaProxErrType::ProtocolUnknownData =>
write!(f, "UNKNOWN_DATA"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HaProxErr
{
err_type: HaProxErrType,
msg: String,
}
impl fmt::Display for HaProxErr
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "[{}]: {}", self.err_type, self.msg)
}
}
impl HaProxErr
{
pub
fn new(err_type: HaProxErrType, msg: String) -> Self
{
return Self{ err_type: err_type, msg: msg };
}
pub
fn get_err_type(&self) -> HaProxErrType
{
return self.err_type;
}
pub
fn get_msg(&self) -> &str
{
return &self.msg;
}
}
pub type HaProxRes<R> = Result<R, HaProxErr>;
#[macro_export]
macro_rules! return_error
{
($err_type:tt, $($arg:tt)*) => (
return std::result::Result::Err($crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! map_error
{
($err_type:tt, $($arg:tt)*) => (
$crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*))
)
}