Skip to main content

systemprompt_logging/models/
log_level.rs

1use serde::{Deserialize, Serialize};
2use sqlx::Type;
3
4use super::LoggingError;
5
6#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Type)]
7#[serde(rename_all = "UPPERCASE")]
8#[sqlx(type_name = "VARCHAR")]
9pub enum LogLevel {
10    Error,
11    Warn,
12    Info,
13    Debug,
14    Trace,
15}
16
17impl std::fmt::Display for LogLevel {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Error => write!(f, "ERROR"),
21            Self::Warn => write!(f, "WARN"),
22            Self::Info => write!(f, "INFO"),
23            Self::Debug => write!(f, "DEBUG"),
24            Self::Trace => write!(f, "TRACE"),
25        }
26    }
27}
28
29impl LogLevel {
30    #[must_use]
31    pub const fn as_str(self) -> &'static str {
32        match self {
33            Self::Error => "ERROR",
34            Self::Warn => "WARN",
35            Self::Info => "INFO",
36            Self::Debug => "DEBUG",
37            Self::Trace => "TRACE",
38        }
39    }
40}
41
42impl std::str::FromStr for LogLevel {
43    type Err = LoggingError;
44
45    fn from_str(s: &str) -> Result<Self, Self::Err> {
46        match s.to_uppercase().as_str() {
47            "ERROR" => Ok(Self::Error),
48            "WARN" => Ok(Self::Warn),
49            "INFO" => Ok(Self::Info),
50            "DEBUG" => Ok(Self::Debug),
51            "TRACE" => Ok(Self::Trace),
52            _ => Err(LoggingError::invalid_log_level(s)),
53        }
54    }
55}