Skip to main content

systemprompt_models/profile/
runtime.rs

1//! Runtime configuration and enums.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, Serialize, Deserialize, schemars::JsonSchema)]
7#[serde(deny_unknown_fields)]
8pub struct RuntimeConfig {
9    #[serde(default)]
10    pub environment: Environment,
11
12    #[serde(default)]
13    pub log_level: LogLevel,
14
15    #[serde(default)]
16    pub output_format: OutputFormat,
17
18    #[serde(default)]
19    pub no_color: bool,
20
21    #[serde(default)]
22    pub non_interactive: bool,
23}
24
25impl Default for RuntimeConfig {
26    fn default() -> Self {
27        Self {
28            environment: Environment::Development,
29            log_level: LogLevel::Normal,
30            output_format: OutputFormat::Text,
31            no_color: false,
32            non_interactive: false,
33        }
34    }
35}
36
37#[derive(
38    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
39)]
40#[serde(rename_all = "lowercase")]
41pub enum Environment {
42    #[default]
43    Development,
44    Test,
45    Staging,
46    Production,
47}
48
49impl Environment {
50    pub const fn is_development(&self) -> bool {
51        matches!(self, Self::Development)
52    }
53
54    pub const fn is_production(&self) -> bool {
55        matches!(self, Self::Production)
56    }
57
58    pub const fn is_test(&self) -> bool {
59        matches!(self, Self::Test)
60    }
61}
62
63impl fmt::Display for Environment {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            Self::Development => write!(f, "development"),
67            Self::Test => write!(f, "test"),
68            Self::Staging => write!(f, "staging"),
69            Self::Production => write!(f, "production"),
70        }
71    }
72}
73
74impl std::str::FromStr for Environment {
75    type Err = String;
76
77    fn from_str(s: &str) -> Result<Self, Self::Err> {
78        match s.to_lowercase().as_str() {
79            "development" => Ok(Self::Development),
80            "test" => Ok(Self::Test),
81            "staging" => Ok(Self::Staging),
82            "production" => Ok(Self::Production),
83            _ => Err(format!(
84                "Invalid environment '{}'. Must be one of: development, test, staging, production",
85                s
86            )),
87        }
88    }
89}
90
91#[derive(
92    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
93)]
94#[serde(rename_all = "lowercase")]
95pub enum LogLevel {
96    Quiet,
97    #[default]
98    Normal,
99    Verbose,
100    Debug,
101}
102
103impl LogLevel {
104    pub const fn as_tracing_filter(&self) -> &'static str {
105        match self {
106            Self::Quiet => "error",
107            Self::Normal => "info",
108            Self::Verbose => "debug",
109            Self::Debug => "trace",
110        }
111    }
112}
113
114impl fmt::Display for LogLevel {
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        match self {
117            Self::Quiet => write!(f, "quiet"),
118            Self::Normal => write!(f, "normal"),
119            Self::Verbose => write!(f, "verbose"),
120            Self::Debug => write!(f, "debug"),
121        }
122    }
123}
124
125impl std::str::FromStr for LogLevel {
126    type Err = String;
127
128    fn from_str(s: &str) -> Result<Self, Self::Err> {
129        match s.to_lowercase().as_str() {
130            "quiet" => Ok(Self::Quiet),
131            "normal" => Ok(Self::Normal),
132            "verbose" => Ok(Self::Verbose),
133            "debug" => Ok(Self::Debug),
134            _ => Err(format!(
135                "Invalid log level '{}'. Must be one of: quiet, normal, verbose, debug",
136                s
137            )),
138        }
139    }
140}
141
142#[derive(
143    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
144)]
145#[serde(rename_all = "lowercase")]
146pub enum OutputFormat {
147    #[default]
148    Text,
149    Json,
150    Yaml,
151}
152
153impl fmt::Display for OutputFormat {
154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155        match self {
156            Self::Text => write!(f, "text"),
157            Self::Json => write!(f, "json"),
158            Self::Yaml => write!(f, "yaml"),
159        }
160    }
161}
162
163impl std::str::FromStr for OutputFormat {
164    type Err = String;
165
166    fn from_str(s: &str) -> Result<Self, Self::Err> {
167        match s.to_lowercase().as_str() {
168            "text" => Ok(Self::Text),
169            "json" => Ok(Self::Json),
170            "yaml" => Ok(Self::Yaml),
171            _ => Err(format!(
172                "Invalid output format '{}'. Must be one of: text, json, yaml",
173                s
174            )),
175        }
176    }
177}