dlog_core/
models.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3use std::fmt::{Display, Formatter};
4
5#[derive(Debug, Serialize, Deserialize)]
6pub enum Priority {
7    Critical,
8    Error,
9    Warning,
10    Info,
11    Debug,
12    Trace,
13}
14
15impl Display for Priority {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        match self {
18            Self::Critical => write!(f, "Critical"),
19            Self::Error => write!(f, "Error"),
20            Self::Warning => write!(f, "Warning"),
21            Self::Info => write!(f, "Informational"),
22            Self::Debug => write!(f, "Debug"),
23            Self::Trace => write!(f, "Trace"),
24        }
25    }
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct Log {
30    #[serde(with = "time::serde::rfc3339")]
31    pub timestamp: OffsetDateTime,
32
33    pub priority: Priority,
34
35    pub text: String,
36}
37
38impl Log {
39    pub fn new(priority: Priority, message: impl Into<String>) -> Self {
40        Self {
41            timestamp: OffsetDateTime::now_utc(),
42            priority,
43            text: message.into(),
44        }
45    }
46}
47
48#[derive(Serialize)]
49pub struct LogRequest<'a> {
50    pub logs: &'a [Log],
51}
52
53impl<'a> LogRequest<'a> {
54    pub fn new(logs: &'a [Log]) -> Self {
55        Self { logs }
56    }
57}