use pinenut_derive::{Builder, Decode, Encode};
#[repr(u8)]
#[non_exhaustive]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Level {
Error = 1,
Warn,
Info,
Debug,
Verbose,
}
impl Level {
#[inline]
pub(crate) fn primitive(&self) -> u8 {
*self as u8
}
pub(crate) fn from_primitive(value: u8) -> Option<Self> {
match value {
1 => Some(Self::Error),
2 => Some(Self::Warn),
3 => Some(Self::Info),
4 => Some(Self::Debug),
5 => Some(Self::Verbose),
_ => None,
}
}
}
#[derive(Encode, Decode, Builder, Default, Clone, PartialEq, Eq, Debug)]
pub struct Location<'a> {
file: Option<&'a str>,
func: Option<&'a str>,
line: Option<u32>,
}
impl<'a> Location<'a> {
#[inline]
pub fn new(file: Option<&'a str>, func: Option<&'a str>, line: Option<u32>) -> Self {
Self { file, func, line }
}
#[inline]
pub fn file(&self) -> Option<&'a str> {
self.file
}
#[inline]
pub fn func(&self) -> Option<&'a str> {
self.func
}
#[inline]
pub fn line(&self) -> Option<u32> {
self.line
}
}
pub type DateTime = chrono::DateTime<chrono::Utc>;
#[derive(Encode, Decode, Builder, Clone, PartialEq, Eq, Debug)]
pub struct Meta<'a> {
level: Level,
datetime: DateTime,
location: Location<'a>,
tag: Option<&'a str>,
thread_id: Option<u64>,
}
impl<'a> Meta<'a> {
#[inline]
pub fn new(
level: Level,
datetime: DateTime,
location: Location<'a>,
tag: Option<&'a str>,
thread_id: Option<u64>,
) -> Self {
Self { level, datetime, location, tag, thread_id }
}
#[inline]
pub fn level(&self) -> Level {
self.level
}
#[inline]
pub fn datetime(&self) -> DateTime {
self.datetime
}
#[inline]
pub fn location(&self) -> &Location<'a> {
&self.location
}
#[inline]
pub fn tag(&self) -> Option<&'a str> {
self.tag
}
#[inline]
pub fn thread_id(&self) -> Option<u64> {
self.thread_id
}
}
impl<'a> Default for Meta<'a> {
#[inline]
fn default() -> Self {
Meta::new(Level::Info, chrono::Utc::now(), Location::default(), None, None)
}
}
#[derive(Encode, Decode, Builder, Default, Clone, PartialEq, Eq, Debug)]
pub struct Record<'a> {
meta: Meta<'a>,
content: &'a str,
}
impl<'a> Record<'a> {
#[inline]
pub fn new(meta: Meta<'a>, content: &'a str) -> Self {
Self { meta, content }
}
#[inline]
pub fn meta(&self) -> &Meta<'a> {
&self.meta
}
#[inline]
pub fn content(&self) -> &'a str {
self.content
}
}