1use super::types::{
6 AppConfig, BehaviorConfig, EcosystemRulesConfig, EnrichmentConfig, FilterConfig,
7 GraphAwareDiffConfig, MatchingConfig, MatchingRulesPathConfig, OutputConfig, TuiConfig,
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ConfigPreset {
17 Default,
19 Security,
21 CiCd,
23 Permissive,
25 Strict,
27}
28
29impl ConfigPreset {
30 #[must_use]
32 pub const fn name(&self) -> &'static str {
33 match self {
34 Self::Default => "default",
35 Self::Security => "security",
36 Self::CiCd => "ci-cd",
37 Self::Permissive => "permissive",
38 Self::Strict => "strict",
39 }
40 }
41
42 #[must_use]
44 pub fn from_name(name: &str) -> Option<Self> {
45 match name.to_lowercase().as_str() {
46 "default" | "balanced" => Some(Self::Default),
47 "security" | "security-focused" => Some(Self::Security),
48 "ci-cd" | "ci" | "cd" | "pipeline" => Some(Self::CiCd),
49 "permissive" | "loose" => Some(Self::Permissive),
50 "strict" | "exact" => Some(Self::Strict),
51 _ => None,
52 }
53 }
54
55 #[must_use]
57 pub const fn description(&self) -> &'static str {
58 match self {
59 Self::Default => "Balanced settings suitable for most SBOM comparisons",
60 Self::Security => "Strict matching with vulnerability detection and CI failure modes",
61 Self::CiCd => "Machine-readable output optimized for CI/CD pipelines",
62 Self::Permissive => "Loose matching for SBOMs with inconsistent naming",
63 Self::Strict => "Exact matching for well-maintained, consistent SBOMs",
64 }
65 }
66
67 #[must_use]
69 pub const fn all() -> &'static [Self] {
70 &[
71 Self::Default,
72 Self::Security,
73 Self::CiCd,
74 Self::Permissive,
75 Self::Strict,
76 ]
77 }
78}
79
80impl std::fmt::Display for ConfigPreset {
81 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82 write!(f, "{}", self.name())
83 }
84}
85
86impl AppConfig {
91 #[must_use]
93 pub fn from_preset(preset: ConfigPreset) -> Self {
94 match preset {
95 ConfigPreset::Default => Self::default(),
96 ConfigPreset::Security => Self::security_preset(),
97 ConfigPreset::CiCd => Self::ci_cd_preset(),
98 ConfigPreset::Permissive => Self::permissive_preset(),
99 ConfigPreset::Strict => Self::strict_preset(),
100 }
101 }
102
103 #[must_use]
109 pub fn security_preset() -> Self {
110 Self {
111 matching: MatchingConfig {
112 fuzzy_preset: "strict".to_string(),
113 threshold: Some(0.9),
114 include_unchanged: false,
115 },
116 output: OutputConfig::default(),
117 filtering: FilterConfig::default(),
118 behavior: BehaviorConfig {
119 fail_on_vuln: true,
120 fail_on_change: false,
121 quiet: false,
122 explain_matches: false,
123 recommend_threshold: false,
124 },
125 graph_diff: GraphAwareDiffConfig::enabled(),
126 rules: MatchingRulesPathConfig::default(),
127 ecosystem_rules: EcosystemRulesConfig {
128 config_file: None,
129 disabled: false,
130 detect_typosquats: true,
131 },
132 tui: TuiConfig::default(),
133 enrichment: Some(EnrichmentConfig::default()),
134 }
135 }
136
137 #[must_use]
143 pub fn ci_cd_preset() -> Self {
144 use crate::reports::ReportFormat;
145
146 Self {
147 matching: MatchingConfig {
148 fuzzy_preset: "balanced".to_string(),
149 threshold: None,
150 include_unchanged: false,
151 },
152 output: OutputConfig {
153 format: ReportFormat::Json,
154 file: None,
155 report_types: crate::reports::ReportType::All,
156 no_color: true,
157 streaming: super::types::StreamingConfig::default(),
158 export_template: None,
159 },
160 filtering: FilterConfig {
161 only_changes: true,
162 min_severity: None,
163 exclude_vex_resolved: false,
164 },
165 behavior: BehaviorConfig {
166 fail_on_vuln: true,
167 fail_on_change: true,
168 quiet: true,
169 explain_matches: false,
170 recommend_threshold: false,
171 },
172 graph_diff: GraphAwareDiffConfig::enabled(),
173 rules: MatchingRulesPathConfig::default(),
174 ecosystem_rules: EcosystemRulesConfig::default(),
175 tui: TuiConfig::default(),
176 enrichment: Some(EnrichmentConfig::default()),
177 }
178 }
179
180 #[must_use]
186 pub fn permissive_preset() -> Self {
187 Self {
188 matching: MatchingConfig {
189 fuzzy_preset: "permissive".to_string(),
190 threshold: Some(0.6),
191 include_unchanged: true,
192 },
193 output: OutputConfig::default(),
194 filtering: FilterConfig::default(),
195 behavior: BehaviorConfig::default(),
196 graph_diff: GraphAwareDiffConfig::default(),
197 rules: MatchingRulesPathConfig::default(),
198 ecosystem_rules: EcosystemRulesConfig::default(),
199 tui: TuiConfig::default(),
200 enrichment: None,
201 }
202 }
203
204 #[must_use]
210 pub fn strict_preset() -> Self {
211 Self {
212 matching: MatchingConfig {
213 fuzzy_preset: "strict".to_string(),
214 threshold: Some(0.95),
215 include_unchanged: false,
216 },
217 output: OutputConfig::default(),
218 filtering: FilterConfig::default(),
219 behavior: BehaviorConfig {
220 fail_on_vuln: false,
221 fail_on_change: false,
222 quiet: false,
223 explain_matches: false,
224 recommend_threshold: false,
225 },
226 graph_diff: GraphAwareDiffConfig::enabled(),
227 rules: MatchingRulesPathConfig::default(),
228 ecosystem_rules: EcosystemRulesConfig::default(),
229 tui: TuiConfig::default(),
230 enrichment: None,
231 }
232 }
233}
234
235pub const DEFAULT_MATCHING_THRESHOLD: f64 = 0.8;
241
242pub const DEFAULT_CLUSTER_THRESHOLD: f64 = 0.7;
244
245pub const DEFAULT_ENRICHMENT_CACHE_TTL: u64 = 3600;
247
248pub const DEFAULT_ENRICHMENT_MAX_CONCURRENT: usize = 10;
250
251#[cfg(test)]
256mod tests {
257 use super::*;
258
259 #[test]
260 fn test_preset_names() {
261 assert_eq!(ConfigPreset::Default.name(), "default");
262 assert_eq!(ConfigPreset::Security.name(), "security");
263 assert_eq!(ConfigPreset::CiCd.name(), "ci-cd");
264 }
265
266 #[test]
267 fn test_preset_from_name() {
268 assert_eq!(
269 ConfigPreset::from_name("default"),
270 Some(ConfigPreset::Default)
271 );
272 assert_eq!(
273 ConfigPreset::from_name("security"),
274 Some(ConfigPreset::Security)
275 );
276 assert_eq!(
277 ConfigPreset::from_name("security-focused"),
278 Some(ConfigPreset::Security)
279 );
280 assert_eq!(ConfigPreset::from_name("ci-cd"), Some(ConfigPreset::CiCd));
281 assert_eq!(
282 ConfigPreset::from_name("pipeline"),
283 Some(ConfigPreset::CiCd)
284 );
285 assert_eq!(ConfigPreset::from_name("invalid"), None);
286 }
287
288 #[test]
289 fn test_security_preset() {
290 let config = AppConfig::security_preset();
291 assert_eq!(config.matching.fuzzy_preset, "strict");
292 assert!(config.behavior.fail_on_vuln);
293 assert!(config.ecosystem_rules.detect_typosquats);
294 assert!(config.enrichment.is_some());
295 }
296
297 #[test]
298 fn test_ci_cd_preset() {
299 let config = AppConfig::ci_cd_preset();
300 assert!(config.behavior.fail_on_vuln);
301 assert!(config.behavior.fail_on_change);
302 assert!(config.behavior.quiet);
303 assert!(config.output.no_color);
304 }
305
306 #[test]
307 fn test_permissive_preset() {
308 let config = AppConfig::permissive_preset();
309 assert_eq!(config.matching.fuzzy_preset, "permissive");
310 assert_eq!(config.matching.threshold, Some(0.6));
311 assert!(config.matching.include_unchanged);
312 }
313
314 #[test]
315 fn test_strict_preset() {
316 let config = AppConfig::strict_preset();
317 assert_eq!(config.matching.fuzzy_preset, "strict");
318 assert_eq!(config.matching.threshold, Some(0.95));
319 assert!(config.graph_diff.enabled);
320 }
321
322 #[test]
323 fn test_from_preset() {
324 let default = AppConfig::from_preset(ConfigPreset::Default);
325 let security = AppConfig::from_preset(ConfigPreset::Security);
326
327 assert_eq!(default.matching.fuzzy_preset, "balanced");
328 assert_eq!(security.matching.fuzzy_preset, "strict");
329 }
330
331 #[test]
332 fn test_all_presets() {
333 let all = ConfigPreset::all();
334 assert_eq!(all.len(), 5);
335 }
336}