raz_config/
builder.rs

1use crate::{
2    CommandConfig, FilterConfig, GlobalConfig, ProviderConfig, UiConfig,
3    override_config::{CommandOverride, OverrideCollection, OverrideMode},
4    schema::ConfigVersion,
5};
6use std::path::PathBuf;
7
8pub struct ConfigBuilder {
9    config: GlobalConfig,
10}
11
12impl ConfigBuilder {
13    pub fn new() -> Self {
14        let mut config = GlobalConfig::new();
15        config.raz.providers.clear(); // Start with empty providers
16        Self { config }
17    }
18
19    pub fn version(mut self, version: ConfigVersion) -> Self {
20        self.config.raz.version = version;
21        self
22    }
23
24    pub fn enabled(mut self, enabled: bool) -> Self {
25        self.config.raz.enabled = enabled;
26        self
27    }
28
29    pub fn provider(mut self, provider: impl Into<String>) -> Self {
30        self.config.raz.providers.push(provider.into());
31        self
32    }
33
34    pub fn providers(mut self, providers: Vec<String>) -> Self {
35        self.config.raz.providers = providers;
36        self
37    }
38
39    pub fn cache_dir(mut self, path: PathBuf) -> Self {
40        self.config.raz.cache_dir = Some(path);
41        self
42    }
43
44    pub fn cache_ttl(mut self, seconds: u64) -> Self {
45        self.config.raz.cache_ttl = Some(seconds);
46        self
47    }
48
49    pub fn parallel_execution(mut self, enabled: bool) -> Self {
50        self.config.raz.parallel_execution = Some(enabled);
51        self
52    }
53
54    pub fn max_concurrent_jobs(mut self, jobs: usize) -> Self {
55        self.config.raz.max_concurrent_jobs = Some(jobs);
56        self
57    }
58
59    pub fn provider_config(mut self, config: ProviderConfig) -> Self {
60        self.config.providers_config = Some(config);
61        self
62    }
63
64    pub fn filter_config(mut self, config: FilterConfig) -> Self {
65        self.config.filters = Some(config);
66        self
67    }
68
69    pub fn ui_config(mut self, config: UiConfig) -> Self {
70        self.config.ui = Some(config);
71        self
72    }
73
74    pub fn command(mut self, command: CommandConfig) -> Self {
75        if self.config.commands.is_none() {
76            self.config.commands = Some(Vec::new());
77        }
78        self.config.commands.as_mut().unwrap().push(command);
79        self
80    }
81
82    pub fn override_config(mut self, override_config: CommandOverride) -> Self {
83        if self.config.saved_overrides.is_none() {
84            self.config.saved_overrides = Some(OverrideCollection::new());
85        }
86        self.config
87            .saved_overrides
88            .as_mut()
89            .unwrap()
90            .add(override_config);
91        self
92    }
93
94    pub fn build(self) -> GlobalConfig {
95        self.config
96    }
97}
98
99impl Default for ConfigBuilder {
100    fn default() -> Self {
101        Self::new()
102    }
103}
104
105pub struct CommandConfigBuilder {
106    config: CommandConfig,
107}
108
109impl CommandConfigBuilder {
110    pub fn new(name: impl Into<String>, command: impl Into<String>) -> Self {
111        Self {
112            config: CommandConfig {
113                name: name.into(),
114                command: command.into(),
115                args: Vec::new(),
116                env: None,
117                working_dir: None,
118                description: None,
119            },
120        }
121    }
122
123    pub fn arg(mut self, arg: impl Into<String>) -> Self {
124        self.config.args.push(arg.into());
125        self
126    }
127
128    pub fn args(mut self, args: Vec<String>) -> Self {
129        self.config.args = args;
130        self
131    }
132
133    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
134        if self.config.env.is_none() {
135            self.config.env = Some(indexmap::IndexMap::new());
136        }
137        self.config
138            .env
139            .as_mut()
140            .unwrap()
141            .insert(key.into(), value.into());
142        self
143    }
144
145    pub fn working_dir(mut self, path: PathBuf) -> Self {
146        self.config.working_dir = Some(path);
147        self
148    }
149
150    pub fn description(mut self, desc: impl Into<String>) -> Self {
151        self.config.description = Some(desc.into());
152        self
153    }
154
155    pub fn build(self) -> CommandConfig {
156        self.config
157    }
158}
159
160pub struct OverrideBuilder {
161    override_config: CommandOverride,
162}
163
164impl OverrideBuilder {
165    pub fn new(key: impl Into<String>) -> Self {
166        Self {
167            override_config: CommandOverride::new(key.into()),
168        }
169    }
170
171    pub fn file(mut self, path: PathBuf) -> Self {
172        self.override_config.file_path = Some(path);
173        self
174    }
175
176    pub fn function(mut self, name: impl Into<String>) -> Self {
177        self.override_config.function_name = Some(name.into());
178        self
179    }
180
181    pub fn line_range(mut self, start: usize, end: usize) -> Self {
182        self.override_config.line_range = Some((start, end));
183        self
184    }
185
186    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
187        self.override_config.env.insert(key.into(), value.into());
188        self
189    }
190
191    pub fn cargo_option(mut self, option: impl Into<String>) -> Self {
192        self.override_config.cargo_options.push(option.into());
193        self
194    }
195
196    pub fn rustc_option(mut self, option: impl Into<String>) -> Self {
197        self.override_config.rustc_options.push(option.into());
198        self
199    }
200
201    pub fn arg(mut self, arg: impl Into<String>) -> Self {
202        self.override_config.args.push(arg.into());
203        self
204    }
205
206    pub fn mode(mut self, mode: OverrideMode) -> Self {
207        self.override_config.mode = mode;
208        self
209    }
210
211    pub fn description(mut self, desc: impl Into<String>) -> Self {
212        self.override_config.description = Some(desc.into());
213        self
214    }
215
216    pub fn build(self) -> CommandOverride {
217        self.override_config
218    }
219}