Skip to main content

systemprompt_logging/models/
log_level.rs

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