commit_wizard/engine/models/runtime/
resolution.rs1use crate::engine::{
2 config::{BaseConfig, RulesConfig},
3 constants::app_config_dir,
4 models::policy::Policy,
5};
6use std::{collections::BTreeMap, path::PathBuf};
7
8#[derive(Debug, Clone)]
9pub struct RuntimeResolution {
10 pub sources: AvailableConfigOptions,
12 pub config: Option<ResolvedConfig>,
14 pub policy: Policy,
16}
17
18impl Default for RuntimeResolution {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl RuntimeResolution {
25 pub fn new() -> Self {
26 Self {
27 sources: AvailableConfigOptions::new(),
28 config: None,
29 policy: Policy::default(),
30 }
31 }
32}
33
34#[derive(Debug, Clone)]
35pub struct ResolvedConfig {
36 pub path: Option<PathBuf>,
38 pub rules: RulesConfig,
41 pub base: BaseConfig,
45}
46
47#[derive(Debug, Clone)]
48pub struct RuntimePaths {
49 pub cwd: PathBuf,
51 pub global: RuntimeGlobalPaths,
53 pub in_git_repo: bool,
55 pub repo_root: Option<PathBuf>,
57 pub explicit_config_path: Option<PathBuf>,
59 pub explicit_registry: Option<String>,
61 pub explicit_registry_ref: Option<String>,
63 pub explicit_registry_section: Option<String>,
65}
66
67impl Default for RuntimePaths {
68 fn default() -> Self {
69 Self::new()
70 }
71}
72
73impl RuntimePaths {
74 pub fn new() -> Self {
75 let cwd = std::env::current_dir().unwrap_or_else(|e| {
76 eprintln!("[warn] Failed to determine current directory: {e} — using '.'");
77 PathBuf::from(".")
78 });
79 let global = RuntimeGlobalPaths::new().unwrap_or_else(|e| {
80 eprintln!(
81 "[warn] Failed to resolve global paths: {e} — using '.' for config/cache/state"
82 );
83 RuntimeGlobalPaths {
84 config: PathBuf::from("."),
85 cache: PathBuf::from("."),
86 state: PathBuf::from("."),
87 }
88 });
89 Self {
90 cwd,
91 global,
92 in_git_repo: false,
93 repo_root: None,
94 explicit_config_path: None,
95 explicit_registry: None,
96 explicit_registry_ref: None,
97 explicit_registry_section: None,
98 }
99 }
100}
101
102#[derive(Debug, Clone)]
103pub struct RuntimeGlobalPaths {
104 pub config: PathBuf,
106 pub cache: PathBuf,
108 pub state: PathBuf,
110}
111
112impl RuntimeGlobalPaths {
113 pub fn new() -> crate::engine::error::Result<Self> {
114 use crate::engine::constants::{app_cache_dir, app_state_dir};
115 let config = app_config_dir()?;
116 let cache = app_cache_dir()?;
117 let state = app_state_dir()?;
118 Ok(Self {
119 config,
120 cache,
121 state,
122 })
123 }
124}
125
126impl Default for RuntimeGlobalPaths {
127 fn default() -> Self {
128 Self::new().unwrap_or_else(|e| {
129 eprintln!(
130 "[warn] Failed to resolve global paths: {e} — using '.' for config/cache/state"
131 );
132 Self {
133 config: std::path::PathBuf::from("."),
134 cache: std::path::PathBuf::from("."),
135 state: std::path::PathBuf::from("."),
136 }
137 })
138 }
139}
140
141#[derive(Debug, Clone)]
142pub struct AvailableConfigOptions {
143 pub cli_config: Option<AvailableConfig>,
144 pub env_config: Option<AvailableConfig>,
146 pub repo_config: Option<AvailableConfig>,
147 pub global_config: Option<AvailableConfig>,
148 pub registries: Vec<RegistryOptions>,
150 pub registry_failures: Vec<String>,
152}
153
154impl Default for AvailableConfigOptions {
155 fn default() -> Self {
156 Self::new()
157 }
158}
159
160impl AvailableConfigOptions {
161 pub fn new() -> Self {
162 Self {
163 cli_config: None,
164 env_config: None,
165 repo_config: None,
166 global_config: None,
167 registries: vec![],
168 registry_failures: vec![],
169 }
170 }
171
172 pub fn is_empty(&self) -> bool {
173 self.cli_config.is_none()
174 && self.env_config.is_none()
175 && self.repo_config.is_none()
176 && self.global_config.is_none()
177 && self.registries.is_empty()
178 }
179
180 pub fn has_cli_config(&self) -> bool {
181 self.cli_config.is_some()
182 }
183
184 pub fn set_cli_config(&mut self, config: AvailableConfig) -> &mut Self {
185 self.cli_config = Some(config);
186 self
187 }
188
189 pub fn has_repo_config(&self) -> bool {
190 self.repo_config.is_some()
191 }
192
193 pub fn set_repo_config(&mut self, config: AvailableConfig) -> &mut Self {
194 self.repo_config = Some(config);
195 self
196 }
197
198 pub fn has_global_config(&self) -> bool {
199 self.global_config.is_some()
200 }
201
202 pub fn set_global_config(&mut self, config: AvailableConfig) -> &mut Self {
203 self.global_config = Some(config);
204 self
205 }
206
207 pub fn has_registries(&self) -> bool {
208 !self.registries.is_empty()
209 }
210
211 pub fn registry_count(&self) -> usize {
212 self.registries.len()
213 }
214}
215
216#[derive(Debug, Clone)]
217pub struct RegistryOptions {
218 pub id: String,
220 pub tag: String,
223 pub url: String,
225 pub r#ref: String,
227 pub section: Option<String>,
229 pub config: Option<AvailableConfig>,
231 pub sections: Option<BTreeMap<String, AvailableConfig>>,
233 pub is_active: bool,
235}
236
237#[derive(Debug, Clone)]
238pub struct AvailableConfig {
239 pub rules: Option<RulesConfig>,
240 pub base: Option<BaseConfig>,
241}
242
243pub fn resolve_available_config(
244 base: Option<BaseConfig>,
245 rules: Option<RulesConfig>,
246) -> AvailableConfig {
247 AvailableConfig { base, rules }
248}
249
250pub fn resolve_policy(config: Option<&ResolvedConfig>) -> Policy {
251 match config {
252 Some(cfg) => Policy::from_config(cfg),
253 None => Policy::default(),
254 }
255}