Skip to main content

sbom_tools/config/
defaults.rs

1//! Default configurations and presets for sbom-tools.
2//!
3//! Provides named presets for common use cases and default values.
4
5use super::types::{
6    AppConfig, BehaviorConfig, EcosystemRulesConfig, EnrichmentConfig, FilterConfig,
7    GraphAwareDiffConfig, MatchingConfig, MatchingRulesPathConfig, OutputConfig, TuiConfig,
8};
9
10// ============================================================================
11// Configuration Presets
12// ============================================================================
13
14/// Named configuration presets for common use cases.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ConfigPreset {
17    /// Default balanced settings suitable for most cases
18    Default,
19    /// Security-focused: strict matching, fail on vulnerabilities
20    Security,
21    /// CI/CD: machine-readable output, fail on changes
22    CiCd,
23    /// Permissive: loose matching for messy SBOMs
24    Permissive,
25    /// Strict: exact matching for well-maintained SBOMs
26    Strict,
27}
28
29impl ConfigPreset {
30    /// Get the preset name as a string.
31    #[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    /// Parse a preset from a string name.
43    #[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    /// Get a description of this preset.
56    #[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    /// Get all available presets.
68    #[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
86// ============================================================================
87// Preset Implementations
88// ============================================================================
89
90impl AppConfig {
91    /// Create an `AppConfig` from a named preset.
92    #[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    /// Security-focused preset.
104    ///
105    /// - Strict matching to avoid false negatives
106    /// - Fail on new vulnerabilities
107    /// - Enable typosquat detection
108    #[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    /// CI/CD pipeline preset.
138    ///
139    /// - JSON output for machine parsing
140    /// - Fail on any changes
141    /// - Quiet mode to reduce noise
142    #[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    /// Permissive preset for messy SBOMs.
181    ///
182    /// - Low matching threshold
183    /// - Include unchanged for full picture
184    /// - No fail modes
185    #[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    /// Strict preset for well-maintained SBOMs.
205    ///
206    /// - High matching threshold
207    /// - Graph-aware diffing
208    /// - Detailed explanations available
209    #[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
235// ============================================================================
236// Default Value Constants
237// ============================================================================
238
239/// Default matching threshold.
240pub const DEFAULT_MATCHING_THRESHOLD: f64 = 0.8;
241
242/// Default cluster threshold for matrix comparisons.
243pub const DEFAULT_CLUSTER_THRESHOLD: f64 = 0.7;
244
245/// Default cache TTL for enrichment in seconds.
246pub const DEFAULT_ENRICHMENT_CACHE_TTL: u64 = 3600;
247
248/// Default max concurrent requests for enrichment.
249pub const DEFAULT_ENRICHMENT_MAX_CONCURRENT: usize = 10;
250
251// ============================================================================
252// Tests
253// ============================================================================
254
255#[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}