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::{AppConfig, MatchingConfig, OutputConfig, FilterConfig, BehaviorConfig, GraphAwareDiffConfig, MatchingRulesPathConfig, EcosystemRulesConfig, TuiConfig, EnrichmentConfig};
6
7// ============================================================================
8// Configuration Presets
9// ============================================================================
10
11/// Named configuration presets for common use cases.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum ConfigPreset {
14    /// Default balanced settings suitable for most cases
15    Default,
16    /// Security-focused: strict matching, fail on vulnerabilities
17    Security,
18    /// CI/CD: machine-readable output, fail on changes
19    CiCd,
20    /// Permissive: loose matching for messy SBOMs
21    Permissive,
22    /// Strict: exact matching for well-maintained SBOMs
23    Strict,
24}
25
26impl ConfigPreset {
27    /// Get the preset name as a string.
28    #[must_use] 
29    pub const fn name(&self) -> &'static str {
30        match self {
31            Self::Default => "default",
32            Self::Security => "security",
33            Self::CiCd => "ci-cd",
34            Self::Permissive => "permissive",
35            Self::Strict => "strict",
36        }
37    }
38
39    /// Parse a preset from a string name.
40    #[must_use] 
41    pub fn from_name(name: &str) -> Option<Self> {
42        match name.to_lowercase().as_str() {
43            "default" | "balanced" => Some(Self::Default),
44            "security" | "security-focused" => Some(Self::Security),
45            "ci-cd" | "ci" | "cd" | "pipeline" => Some(Self::CiCd),
46            "permissive" | "loose" => Some(Self::Permissive),
47            "strict" | "exact" => Some(Self::Strict),
48            _ => None,
49        }
50    }
51
52    /// Get a description of this preset.
53    #[must_use] 
54    pub const fn description(&self) -> &'static str {
55        match self {
56            Self::Default => "Balanced settings suitable for most SBOM comparisons",
57            Self::Security => {
58                "Strict matching with vulnerability detection and CI failure modes"
59            }
60            Self::CiCd => "Machine-readable output optimized for CI/CD pipelines",
61            Self::Permissive => "Loose matching for SBOMs with inconsistent naming",
62            Self::Strict => "Exact matching for well-maintained, consistent SBOMs",
63        }
64    }
65
66    /// Get all available presets.
67    #[must_use] 
68    pub const fn all() -> &'static [Self] {
69        &[
70            Self::Default,
71            Self::Security,
72            Self::CiCd,
73            Self::Permissive,
74            Self::Strict,
75        ]
76    }
77}
78
79impl std::fmt::Display for ConfigPreset {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(f, "{}", self.name())
82    }
83}
84
85// ============================================================================
86// Preset Implementations
87// ============================================================================
88
89impl AppConfig {
90    /// Create an `AppConfig` from a named preset.
91    #[must_use] 
92    pub fn from_preset(preset: ConfigPreset) -> Self {
93        match preset {
94            ConfigPreset::Default => Self::default(),
95            ConfigPreset::Security => Self::security_preset(),
96            ConfigPreset::CiCd => Self::ci_cd_preset(),
97            ConfigPreset::Permissive => Self::permissive_preset(),
98            ConfigPreset::Strict => Self::strict_preset(),
99        }
100    }
101
102    /// Security-focused preset.
103    ///
104    /// - Strict matching to avoid false negatives
105    /// - Fail on new vulnerabilities
106    /// - Enable typosquat detection
107    #[must_use] 
108    pub fn security_preset() -> Self {
109        Self {
110            matching: MatchingConfig {
111                fuzzy_preset: "strict".to_string(),
112                threshold: Some(0.9),
113                include_unchanged: false,
114            },
115            output: OutputConfig::default(),
116            filtering: FilterConfig::default(),
117            behavior: BehaviorConfig {
118                fail_on_vuln: true,
119                fail_on_change: false,
120                quiet: false,
121                explain_matches: false,
122                recommend_threshold: false,
123            },
124            graph_diff: GraphAwareDiffConfig::enabled(),
125            rules: MatchingRulesPathConfig::default(),
126            ecosystem_rules: EcosystemRulesConfig {
127                config_file: None,
128                disabled: false,
129                detect_typosquats: true,
130            },
131            tui: TuiConfig::default(),
132            enrichment: Some(EnrichmentConfig::default()),
133        }
134    }
135
136    /// CI/CD pipeline preset.
137    ///
138    /// - JSON output for machine parsing
139    /// - Fail on any changes
140    /// - Quiet mode to reduce noise
141    #[must_use] 
142    pub fn ci_cd_preset() -> Self {
143        use crate::reports::ReportFormat;
144
145        Self {
146            matching: MatchingConfig {
147                fuzzy_preset: "balanced".to_string(),
148                threshold: None,
149                include_unchanged: false,
150            },
151            output: OutputConfig {
152                format: ReportFormat::Json,
153                file: None,
154                report_types: crate::reports::ReportType::All,
155                no_color: true,
156                streaming: super::types::StreamingConfig::default(),
157            },
158            filtering: FilterConfig {
159                only_changes: true,
160                min_severity: None,
161                exclude_vex_resolved: false,
162            },
163            behavior: BehaviorConfig {
164                fail_on_vuln: true,
165                fail_on_change: true,
166                quiet: true,
167                explain_matches: false,
168                recommend_threshold: false,
169            },
170            graph_diff: GraphAwareDiffConfig::enabled(),
171            rules: MatchingRulesPathConfig::default(),
172            ecosystem_rules: EcosystemRulesConfig::default(),
173            tui: TuiConfig::default(),
174            enrichment: Some(EnrichmentConfig::default()),
175        }
176    }
177
178    /// Permissive preset for messy SBOMs.
179    ///
180    /// - Low matching threshold
181    /// - Include unchanged for full picture
182    /// - No fail modes
183    #[must_use] 
184    pub fn permissive_preset() -> Self {
185        Self {
186            matching: MatchingConfig {
187                fuzzy_preset: "permissive".to_string(),
188                threshold: Some(0.6),
189                include_unchanged: true,
190            },
191            output: OutputConfig::default(),
192            filtering: FilterConfig::default(),
193            behavior: BehaviorConfig::default(),
194            graph_diff: GraphAwareDiffConfig::default(),
195            rules: MatchingRulesPathConfig::default(),
196            ecosystem_rules: EcosystemRulesConfig::default(),
197            tui: TuiConfig::default(),
198            enrichment: None,
199        }
200    }
201
202    /// Strict preset for well-maintained SBOMs.
203    ///
204    /// - High matching threshold
205    /// - Graph-aware diffing
206    /// - Detailed explanations available
207    #[must_use] 
208    pub fn strict_preset() -> Self {
209        Self {
210            matching: MatchingConfig {
211                fuzzy_preset: "strict".to_string(),
212                threshold: Some(0.95),
213                include_unchanged: false,
214            },
215            output: OutputConfig::default(),
216            filtering: FilterConfig::default(),
217            behavior: BehaviorConfig {
218                fail_on_vuln: false,
219                fail_on_change: false,
220                quiet: false,
221                explain_matches: false,
222                recommend_threshold: false,
223            },
224            graph_diff: GraphAwareDiffConfig::enabled(),
225            rules: MatchingRulesPathConfig::default(),
226            ecosystem_rules: EcosystemRulesConfig::default(),
227            tui: TuiConfig::default(),
228            enrichment: None,
229        }
230    }
231}
232
233// ============================================================================
234// Default Value Constants
235// ============================================================================
236
237/// Default matching threshold.
238pub const DEFAULT_MATCHING_THRESHOLD: f64 = 0.8;
239
240/// Default cluster threshold for matrix comparisons.
241pub const DEFAULT_CLUSTER_THRESHOLD: f64 = 0.7;
242
243/// Default cache TTL for enrichment in seconds.
244pub const DEFAULT_ENRICHMENT_CACHE_TTL: u64 = 3600;
245
246/// Default max concurrent requests for enrichment.
247pub const DEFAULT_ENRICHMENT_MAX_CONCURRENT: usize = 10;
248
249// ============================================================================
250// Tests
251// ============================================================================
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    #[test]
258    fn test_preset_names() {
259        assert_eq!(ConfigPreset::Default.name(), "default");
260        assert_eq!(ConfigPreset::Security.name(), "security");
261        assert_eq!(ConfigPreset::CiCd.name(), "ci-cd");
262    }
263
264    #[test]
265    fn test_preset_from_name() {
266        assert_eq!(
267            ConfigPreset::from_name("default"),
268            Some(ConfigPreset::Default)
269        );
270        assert_eq!(
271            ConfigPreset::from_name("security"),
272            Some(ConfigPreset::Security)
273        );
274        assert_eq!(
275            ConfigPreset::from_name("security-focused"),
276            Some(ConfigPreset::Security)
277        );
278        assert_eq!(ConfigPreset::from_name("ci-cd"), Some(ConfigPreset::CiCd));
279        assert_eq!(
280            ConfigPreset::from_name("pipeline"),
281            Some(ConfigPreset::CiCd)
282        );
283        assert_eq!(ConfigPreset::from_name("invalid"), None);
284    }
285
286    #[test]
287    fn test_security_preset() {
288        let config = AppConfig::security_preset();
289        assert_eq!(config.matching.fuzzy_preset, "strict");
290        assert!(config.behavior.fail_on_vuln);
291        assert!(config.ecosystem_rules.detect_typosquats);
292        assert!(config.enrichment.is_some());
293    }
294
295    #[test]
296    fn test_ci_cd_preset() {
297        let config = AppConfig::ci_cd_preset();
298        assert!(config.behavior.fail_on_vuln);
299        assert!(config.behavior.fail_on_change);
300        assert!(config.behavior.quiet);
301        assert!(config.output.no_color);
302    }
303
304    #[test]
305    fn test_permissive_preset() {
306        let config = AppConfig::permissive_preset();
307        assert_eq!(config.matching.fuzzy_preset, "permissive");
308        assert_eq!(config.matching.threshold, Some(0.6));
309        assert!(config.matching.include_unchanged);
310    }
311
312    #[test]
313    fn test_strict_preset() {
314        let config = AppConfig::strict_preset();
315        assert_eq!(config.matching.fuzzy_preset, "strict");
316        assert_eq!(config.matching.threshold, Some(0.95));
317        assert!(config.graph_diff.enabled);
318    }
319
320    #[test]
321    fn test_from_preset() {
322        let default = AppConfig::from_preset(ConfigPreset::Default);
323        let security = AppConfig::from_preset(ConfigPreset::Security);
324
325        assert_eq!(default.matching.fuzzy_preset, "balanced");
326        assert_eq!(security.matching.fuzzy_preset, "strict");
327    }
328
329    #[test]
330    fn test_all_presets() {
331        let all = ConfigPreset::all();
332        assert_eq!(all.len(), 5);
333    }
334}