1use super::changes::{
4 ComponentChangeComputer, DependencyChangeComputer, LicenseChangeComputer,
5 VulnerabilityChangeComputer,
6};
7pub use super::engine_config::LargeSbomConfig;
8use super::engine_matching::{match_components, ComponentMatchResult};
9use super::engine_rules::{apply_rules, remap_match_result};
10use super::traits::ChangeComputer;
11use super::{diff_dependency_graph, CostModel, DiffResult, GraphDiffConfig, MatchInfo};
12use crate::error::SbomDiffError;
13use crate::matching::{
14 ComponentMatcher, FuzzyMatchConfig, FuzzyMatcher, MatchingRulesConfig, RuleEngine,
15};
16use crate::model::NormalizedSbom;
17use std::borrow::Cow;
18
19#[must_use]
21pub struct DiffEngine {
22 cost_model: CostModel,
23 fuzzy_config: FuzzyMatchConfig,
24 include_unchanged: bool,
25 graph_diff_config: Option<GraphDiffConfig>,
26 rule_engine: Option<RuleEngine>,
27 custom_matcher: Option<Box<dyn ComponentMatcher>>,
28 large_sbom_config: LargeSbomConfig,
29}
30
31impl DiffEngine {
32 pub fn new() -> Self {
34 Self {
35 cost_model: CostModel::default(),
36 fuzzy_config: FuzzyMatchConfig::balanced(),
37 include_unchanged: false,
38 graph_diff_config: None,
39 rule_engine: None,
40 custom_matcher: None,
41 large_sbom_config: LargeSbomConfig::default(),
42 }
43 }
44
45 pub const fn with_cost_model(mut self, cost_model: CostModel) -> Self {
47 self.cost_model = cost_model;
48 self
49 }
50
51 pub const fn with_fuzzy_config(mut self, config: FuzzyMatchConfig) -> Self {
53 self.fuzzy_config = config;
54 self
55 }
56
57 pub const fn include_unchanged(mut self, include: bool) -> Self {
59 self.include_unchanged = include;
60 self
61 }
62
63 pub const fn with_graph_diff(mut self, config: GraphDiffConfig) -> Self {
65 self.graph_diff_config = Some(config);
66 self
67 }
68
69 pub fn with_matching_rules(mut self, config: MatchingRulesConfig) -> Result<Self, String> {
71 self.rule_engine = Some(RuleEngine::new(config)?);
72 Ok(self)
73 }
74
75 pub fn with_rule_engine(mut self, engine: RuleEngine) -> Self {
77 self.rule_engine = Some(engine);
78 self
79 }
80
81 pub fn with_matcher(mut self, matcher: Box<dyn ComponentMatcher>) -> Self {
83 self.custom_matcher = Some(matcher);
84 self
85 }
86
87 pub const fn with_large_sbom_config(mut self, config: LargeSbomConfig) -> Self {
89 self.large_sbom_config = config;
90 self
91 }
92
93 #[must_use]
95 pub const fn large_sbom_config(&self) -> &LargeSbomConfig {
96 &self.large_sbom_config
97 }
98
99 #[must_use]
101 pub fn has_custom_matcher(&self) -> bool {
102 self.custom_matcher.is_some()
103 }
104
105 #[must_use]
107 pub const fn graph_diff_enabled(&self) -> bool {
108 self.graph_diff_config.is_some()
109 }
110
111 #[must_use]
113 pub const fn has_matching_rules(&self) -> bool {
114 self.rule_engine.is_some()
115 }
116
117 #[must_use = "diff result contains all changes and should not be discarded"]
119 pub fn diff(&self, old: &NormalizedSbom, new: &NormalizedSbom) -> Result<DiffResult, SbomDiffError> {
120 let mut result = DiffResult::new();
121
122 if old.content_hash == new.content_hash && old.content_hash != 0 {
124 return Ok(result);
125 }
126
127 let (old_filtered, new_filtered, canonical_maps) =
130 if let Some(rule_result) = apply_rules(self.rule_engine.as_ref(), old, new) {
131 result.rules_applied = rule_result.rules_count;
132 (
133 Cow::Owned(rule_result.old_filtered),
134 Cow::Owned(rule_result.new_filtered),
135 Some((rule_result.old_canonical, rule_result.new_canonical)),
136 )
137 } else {
138 (Cow::Borrowed(old), Cow::Borrowed(new), None)
139 };
140
141 let default_matcher = FuzzyMatcher::new(self.fuzzy_config.clone());
143 let matcher: &dyn ComponentMatcher = self
144 .custom_matcher
145 .as_ref()
146 .map_or(&default_matcher as &dyn ComponentMatcher, |m| m.as_ref());
147
148 let mut component_matches = match_components(
149 &old_filtered,
150 &new_filtered,
151 matcher,
152 &self.fuzzy_config,
153 &self.large_sbom_config,
154 );
155
156 if let Some((old_canonical, new_canonical)) = &canonical_maps {
158 component_matches = remap_match_result(&component_matches, old_canonical, new_canonical);
159 }
160
161 self.compute_all_changes(&old_filtered, &new_filtered, &component_matches, matcher, &mut result);
163
164 if let Some(ref graph_config) = self.graph_diff_config {
166 let (graph_changes, graph_summary) =
167 diff_dependency_graph(&old_filtered, &new_filtered, &component_matches.matches, graph_config);
168 result.graph_changes = graph_changes;
169 result.graph_summary = Some(graph_summary);
170 }
171
172 result.semantic_score = self.cost_model.calculate_semantic_score(
174 result.components.added.len(),
175 result.components.removed.len(),
176 result.components.modified.len(),
177 result.licenses.component_changes.len(),
178 result.vulnerabilities.introduced.len(),
179 result.vulnerabilities.resolved.len(),
180 result.dependencies.added.len(),
181 result.dependencies.removed.len(),
182 );
183
184 result.calculate_summary();
185 Ok(result)
186 }
187
188 fn compute_all_changes(
190 &self,
191 old: &NormalizedSbom,
192 new: &NormalizedSbom,
193 match_result: &ComponentMatchResult,
194 matcher: &dyn ComponentMatcher,
195 result: &mut DiffResult,
196 ) {
197 let comp_computer = ComponentChangeComputer::new(self.cost_model.clone());
199 let comp_changes = comp_computer.compute(old, new, &match_result.matches);
200 result.components.added = comp_changes.added;
201 result.components.removed = comp_changes.removed;
202 result.components.modified = comp_changes
203 .modified
204 .into_iter()
205 .map(|mut change| {
206 if let (Some(old_id), Some(new_id)) =
209 (&change.old_canonical_id, &change.canonical_id)
210 && let (Some(old_comp), Some(new_comp)) =
211 (old.components.get(old_id), new.components.get(new_id))
212 {
213 let explanation = matcher.explain_match(old_comp, new_comp);
214 let mut match_info = MatchInfo::from_explanation(&explanation);
215
216 if let Some(&score) =
218 match_result.pairs.get(&(old_id.clone(), new_id.clone()))
219 {
220 match_info.score = score;
221 }
222
223 change = change.with_match_info(match_info);
224 }
225 change
226 })
227 .collect();
228
229 let dep_computer = DependencyChangeComputer::new();
231 let dep_changes = dep_computer.compute(old, new, &match_result.matches);
232 result.dependencies.added = dep_changes.added;
233 result.dependencies.removed = dep_changes.removed;
234
235 let lic_computer = LicenseChangeComputer::new();
237 let lic_changes = lic_computer.compute(old, new, &match_result.matches);
238 result.licenses.new_licenses = lic_changes.new_licenses;
239 result.licenses.removed_licenses = lic_changes.removed_licenses;
240
241 let vuln_computer = VulnerabilityChangeComputer::new();
243 let vuln_changes = vuln_computer.compute(old, new, &match_result.matches);
244 result.vulnerabilities.introduced = vuln_changes.introduced;
245 result.vulnerabilities.resolved = vuln_changes.resolved;
246 result.vulnerabilities.persistent = vuln_changes.persistent;
247 }
248}
249
250impl Default for DiffEngine {
251 fn default() -> Self {
252 Self::new()
253 }
254}
255
256#[cfg(test)]
257mod tests {
258 use super::*;
259
260 #[test]
261 fn test_empty_diff() {
262 let engine = DiffEngine::new();
263 let sbom = NormalizedSbom::default();
264 let result = engine.diff(&sbom, &sbom).expect("diff should succeed");
265 assert!(!result.has_changes());
266 }
267}