use core::{fmt, marker::PhantomData};
pub mod v5424;
type Priority = u8;
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub enum Facility {
Kern = 0 << 3,
User = 1 << 3,
Mail = 2 << 3,
Daemon = 3 << 3,
Auth = 4 << 3,
Syslog = 5 << 3,
Lpr = 6 << 3,
News = 7 << 3,
Uucp = 8 << 3,
Cron = 9 << 3,
Authpriv = 10 << 3,
Ftp = 11 << 3,
Local0 = 16 << 3,
Local1 = 17 << 3,
Local2 = 18 << 3,
Local3 = 19 << 3,
Local4 = 20 << 3,
Local5 = 21 << 3,
Local6 = 22 << 3,
Local7 = 23 << 3,
}
impl Default for Facility {
fn default() -> Self {
Self::Local0
}
}
impl fmt::Display for Facility {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Facility::Kern => "Kern",
Facility::User => "User",
Facility::Mail => "Mail",
Facility::Daemon => "Daemon",
Facility::Auth => "Auth",
Facility::Syslog => "Syslog",
Facility::Lpr => "Lpr",
Facility::News => "News",
Facility::Uucp => "Uucp",
Facility::Cron => "Cron",
Facility::Authpriv => "Authpriv",
Facility::Ftp => "Ftp",
Facility::Local0 => "Local0",
Facility::Local1 => "Local1",
Facility::Local2 => "Local2",
Facility::Local3 => "Local3",
Facility::Local4 => "Local4",
Facility::Local5 => "Local5",
Facility::Local6 => "Local6",
Facility::Local7 => "Local7",
};
f.write_str(s)
}
}
impl<T> fmt::Display for IntToEnumError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let enum_name: &'static str = std::any::type_name::<T>();
write!(f, "Failed to convert {} to {}", self.value, enum_name)
}
}
impl TryFrom<u8> for Facility {
type Error = IntToEnumError<Self>;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Into::<i32>::into(value).try_into()
}
}
impl TryFrom<i32> for Facility {
type Error = IntToEnumError<Self>;
fn try_from(value: i32) -> Result<Self, Self::Error> {
let variant = match value {
0 => Self::Kern,
1 => Self::User,
2 => Self::Mail,
3 => Self::Daemon,
4 => Self::Auth,
5 => Self::Syslog,
6 => Self::Lpr,
7 => Self::News,
8 => Self::Uucp,
9 => Self::Cron,
10 => Self::Authpriv,
11 => Self::Ftp,
16 => Self::Local0,
17 => Self::Local1,
18 => Self::Local2,
19 => Self::Local3,
20 => Self::Local4,
21 => Self::Local5,
22 => Self::Local6,
23 => Self::Local7,
_ => {
return Err(IntToEnumError {
value,
target: PhantomData,
})
}
};
Ok(variant)
}
}
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub enum Severity {
Emerg,
Alert,
Crit,
Err,
Warning,
Notice,
Info,
Debug,
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Severity::Emerg => "Emerg",
Severity::Alert => "Alert",
Severity::Crit => "Crit",
Severity::Err => "Err",
Severity::Warning => "Warning",
Severity::Notice => "Notice",
Severity::Info => "Info",
Severity::Debug => "Debug",
};
f.write_str(s)
}
}
impl TryFrom<u8> for Severity {
type Error = IntToEnumError<Self>;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Into::<i32>::into(value).try_into()
}
}
impl TryFrom<i32> for Severity {
type Error = IntToEnumError<Self>;
fn try_from(value: i32) -> Result<Self, Self::Error> {
let variant = match value {
0 => Self::Emerg,
1 => Self::Alert,
2 => Self::Crit,
3 => Self::Err,
4 => Self::Warning,
5 => Self::Notice,
6 => Self::Info,
7 => Self::Debug,
_ => {
return Err(IntToEnumError {
value,
target: PhantomData,
})
}
};
Ok(variant)
}
}
pub struct IntToEnumError<T> {
value: i32,
target: PhantomData<T>,
}
impl<T> fmt::Debug for IntToEnumError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IntToEnumError")
.field("value", &self.value)
.field("target", &std::any::type_name::<T>())
.finish()
}
}