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                fail_on_vex_gap: false,
165            },
166            behavior: BehaviorConfig {
167                fail_on_vuln: true,
168                fail_on_change: true,
169                quiet: true,
170                explain_matches: false,
171                recommend_threshold: false,
172            },
173            graph_diff: GraphAwareDiffConfig::enabled(),
174            rules: MatchingRulesPathConfig::default(),
175            ecosystem_rules: EcosystemRulesConfig::default(),
176            tui: TuiConfig::default(),
177            enrichment: Some(EnrichmentConfig::default()),
178        }
179    }
180
181    /// Permissive preset for messy SBOMs.
182    ///
183    /// - Low matching threshold
184    /// - Include unchanged for full picture
185    /// - No fail modes
186    #[must_use]
187    pub fn permissive_preset() -> Self {
188        Self {
189            matching: MatchingConfig {
190                fuzzy_preset: "permissive".to_string(),
191                threshold: Some(0.6),
192                include_unchanged: true,
193            },
194            output: OutputConfig::default(),
195            filtering: FilterConfig::default(),
196            behavior: BehaviorConfig::default(),
197            graph_diff: GraphAwareDiffConfig::default(),
198            rules: MatchingRulesPathConfig::default(),
199            ecosystem_rules: EcosystemRulesConfig::default(),
200            tui: TuiConfig::default(),
201            enrichment: None,
202        }
203    }
204
205    /// Strict preset for well-maintained SBOMs.
206    ///
207    /// - High matching threshold
208    /// - Graph-aware diffing
209    /// - Detailed explanations available
210    #[must_use]
211    pub fn strict_preset() -> Self {
212        Self {
213            matching: MatchingConfig {
214                fuzzy_preset: "strict".to_string(),
215                threshold: Some(0.95),
216                include_unchanged: false,
217            },
218            output: OutputConfig::default(),
219            filtering: FilterConfig::default(),
220            behavior: BehaviorConfig {
221                fail_on_vuln: false,
222                fail_on_change: false,
223                quiet: false,
224                explain_matches: false,
225                recommend_threshold: false,
226            },
227            graph_diff: GraphAwareDiffConfig::enabled(),
228            rules: MatchingRulesPathConfig::default(),
229            ecosystem_rules: EcosystemRulesConfig::default(),
230            tui: TuiConfig::default(),
231            enrichment: None,
232        }
233    }
234}
235
236// ============================================================================
237// Default Value Constants
238// ============================================================================
239
240/// Default matching threshold.
241pub const DEFAULT_MATCHING_THRESHOLD: f64 = 0.8;
242
243/// Default cluster threshold for matrix comparisons.
244pub const DEFAULT_CLUSTER_THRESHOLD: f64 = 0.7;
245
246/// Default cache TTL for enrichment in seconds.
247pub const DEFAULT_ENRICHMENT_CACHE_TTL: u64 = 3600;
248
249/// Default max concurrent requests for enrichment.
250pub const DEFAULT_ENRICHMENT_MAX_CONCURRENT: usize = 10;
251
252// ============================================================================
253// Tests
254// ============================================================================
255
256#[cfg(test)]
257mod tests {
258    use super::*;
259
260    #[test]
261    fn test_preset_names() {
262        assert_eq!(ConfigPreset::Default.name(), "default");
263        assert_eq!(ConfigPreset::Security.name(), "security");
264        assert_eq!(ConfigPreset::CiCd.name(), "ci-cd");
265    }
266
267    #[test]
268    fn test_preset_from_name() {
269        assert_eq!(
270            ConfigPreset::from_name("default"),
271            Some(ConfigPreset::Default)
272        );
273        assert_eq!(
274            ConfigPreset::from_name("security"),
275            Some(ConfigPreset::Security)
276        );
277        assert_eq!(
278            ConfigPreset::from_name("security-focused"),
279            Some(ConfigPreset::Security)
280        );
281        assert_eq!(ConfigPreset::from_name("ci-cd"), Some(ConfigPreset::CiCd));
282        assert_eq!(
283            ConfigPreset::from_name("pipeline"),
284            Some(ConfigPreset::CiCd)
285        );
286        assert_eq!(ConfigPreset::from_name("invalid"), None);
287    }
288
289    #[test]
290    fn test_security_preset() {
291        let config = AppConfig::security_preset();
292        assert_eq!(config.matching.fuzzy_preset, "strict");
293        assert!(config.behavior.fail_on_vuln);
294        assert!(config.ecosystem_rules.detect_typosquats);
295        assert!(config.enrichment.is_some());
296    }
297
298    #[test]
299    fn test_ci_cd_preset() {
300        let config = AppConfig::ci_cd_preset();
301        assert!(config.behavior.fail_on_vuln);
302        assert!(config.behavior.fail_on_change);
303        assert!(config.behavior.quiet);
304        assert!(config.output.no_color);
305    }
306
307    #[test]
308    fn test_permissive_preset() {
309        let config = AppConfig::permissive_preset();
310        assert_eq!(config.matching.fuzzy_preset, "permissive");
311        assert_eq!(config.matching.threshold, Some(0.6));
312        assert!(config.matching.include_unchanged);
313    }
314
315    #[test]
316    fn test_strict_preset() {
317        let config = AppConfig::strict_preset();
318        assert_eq!(config.matching.fuzzy_preset, "strict");
319        assert_eq!(config.matching.threshold, Some(0.95));
320        assert!(config.graph_diff.enabled);
321    }
322
323    #[test]
324    fn test_from_preset() {
325        let default = AppConfig::from_preset(ConfigPreset::Default);
326        let security = AppConfig::from_preset(ConfigPreset::Security);
327
328        assert_eq!(default.matching.fuzzy_preset, "balanced");
329        assert_eq!(security.matching.fuzzy_preset, "strict");
330    }
331
332    #[test]
333    fn test_all_presets() {
334        let all = ConfigPreset::all();
335        assert_eq!(all.len(), 5);
336    }
337}