hyprstream_core/cli/commands/
config.rs1use clap::Args;
2use serde::Deserialize;
3
4pub trait ConfigOption {
6 fn env_var(&self) -> Option<&'static str>;
8
9 fn cli_flag(&self) -> Option<&'static str>;
11
12 fn cli_short(&self) -> Option<char>;
14
15 fn config_path(&self) -> &'static str;
17
18 fn help_text(&self) -> &'static str;
20}
21
22#[derive(Debug, Clone)]
24pub struct ConfigOptionDef<T> {
25 pub value: Option<T>,
27 pub env_var: Option<&'static str>,
29 pub cli_flag: Option<&'static str>,
31 pub cli_short: Option<char>,
33 pub config_path: &'static str,
35 pub help_text: &'static str,
37}
38
39impl<T> ConfigOptionDef<T> {
40 pub fn new(config_path: &'static str, help_text: &'static str) -> Self {
41 Self {
42 value: None,
43 env_var: None,
44 cli_flag: None,
45 cli_short: None,
46 config_path,
47 help_text,
48 }
49 }
50
51 pub fn with_env(mut self, env_var: &'static str) -> Self {
52 self.env_var = Some(env_var);
53 self
54 }
55
56 pub fn with_cli(mut self, flag: &'static str) -> Self {
57 self.cli_flag = Some(flag);
58 self
59 }
60
61 pub fn with_short(mut self, short: char) -> Self {
62 self.cli_short = Some(short);
63 self
64 }
65}
66
67impl<T> ConfigOption for ConfigOptionDef<T> {
68 fn env_var(&self) -> Option<&'static str> {
69 self.env_var
70 }
71
72 fn cli_flag(&self) -> Option<&'static str> {
73 self.cli_flag
74 }
75
76 fn cli_short(&self) -> Option<char> {
77 self.cli_short
78 }
79
80 fn config_path(&self) -> &'static str {
81 self.config_path
82 }
83
84 fn help_text(&self) -> &'static str {
85 self.help_text
86 }
87}
88
89pub trait ConfigSection: Sized + Default {
91 fn options() -> Vec<ConfigOptionDef<String>>;
93
94 fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
96 where
97 D: serde::Deserializer<'de>;
98}
99
100#[derive(Debug, Clone, Default, Args, Deserialize)]
102pub struct LoggingConfig {
103 #[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count)]
105 #[serde(skip)]
106 pub verbose: u8,
107
108 #[arg(long = "log-level", env = "HYPRSTREAM_LOG_LEVEL")]
110 pub log_level: Option<String>,
111
112 #[arg(long = "log-filter", env = "HYPRSTREAM_LOG_FILTER")]
114 pub log_filter: Option<String>,
115}
116
117impl LoggingConfig {
118 pub fn get_effective_level(&self) -> &str {
119 match (self.verbose, self.log_level.as_deref()) {
120 (2, _) => "trace", (1, _) => "debug", (0, Some(level)) => level, _ => "info", }
125 }
126}
127
128impl ConfigSection for LoggingConfig {
129 fn options() -> Vec<ConfigOptionDef<String>> {
130 vec![
131 ConfigOptionDef::new(
132 "logging.level",
133 "Log level (trace, debug, info, warn, error)",
134 )
135 .with_env("HYPRSTREAM_LOG_LEVEL")
136 .with_cli("log-level"),
137 ConfigOptionDef::new("logging.filter", "Log filter directives")
138 .with_env("HYPRSTREAM_LOG_FILTER")
139 .with_cli("log-filter"),
140 ]
141 }
142
143 fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
144 where
145 D: serde::Deserializer<'de>,
146 {
147 #[derive(Deserialize)]
148 struct LoggingConfigFile {
149 level: Option<String>,
150 filter: Option<String>,
151 }
152
153 let config = LoggingConfigFile::deserialize(deserializer)?;
154 Ok(LoggingConfig {
155 verbose: 0,
156 log_level: config.level,
157 log_filter: config.filter,
158 })
159 }
160}
161
162#[derive(Debug, Clone, Default, Args, Deserialize)]
164pub struct ServicesConfigCli {
165 #[arg(
170 long = "services-startup",
171 env = "HYPRSTREAM_SERVICES_STARTUP",
172 value_delimiter = ','
173 )]
174 #[serde(skip)]
175 pub startup: Option<Vec<String>>,
176}
177
178impl ConfigSection for ServicesConfigCli {
179 fn options() -> Vec<ConfigOptionDef<String>> {
180 vec![
181 ConfigOptionDef::new(
182 "services.startup",
183 "Comma-separated list of services to start at startup (registry, policy, worker, event)",
184 )
185 .with_env("HYPRSTREAM_SERVICES_STARTUP")
186 .with_cli("services-startup"),
187 ]
188 }
189
190 fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
191 where
192 D: serde::Deserializer<'de>,
193 {
194 #[derive(Deserialize)]
195 struct ServicesConfigFile {
196 startup: Option<Vec<String>>,
197 }
198
199 let config = ServicesConfigFile::deserialize(deserializer)?;
200 Ok(ServicesConfigCli {
201 startup: config.startup,
202 })
203 }
204}
205
206#[derive(Debug, Clone, Deserialize)]
208pub struct Credentials {
209 pub username: String,
211 pub password: String,
213}