ifc_template_catalog/overlay/
apply.rs1use std::collections::BTreeSet;
4
5use crate::catalog::{Catalog, CatalogProfile};
6use crate::definition::SetTemplateKind;
7
8use super::{Advisory, AppliedPatch, Patch, PatchError, PatchOperation};
9
10impl Catalog {
11 pub fn with_patches(
13 &self,
14 profile: CatalogProfile,
15 patches: &[Patch],
16 ) -> Result<Self, PatchError> {
17 if patches.is_empty() {
18 return Err(PatchError::EmptyLedger);
19 }
20 if profile == CatalogProfile::Official
21 || (profile == CatalogProfile::Corrected
22 && (self.profile() != CatalogProfile::Official
23 || !self.applied_patches().is_empty()))
24 {
25 return Err(PatchError::InvalidProfileTransition {
26 from: self.profile(),
27 to: profile,
28 });
29 }
30 let mut templates = self.clone().into_templates();
31 let mut applied = self.applied_patches().to_vec();
32 let mut advisories = self.advisories().to_vec();
33 let mut ids: BTreeSet<String> = applied.iter().map(|patch| patch.id.clone()).collect();
34 let mut replaced_applicability: BTreeSet<String> = applied
35 .iter()
36 .filter(|patch| {
37 matches!(
38 &patch.operation,
39 PatchOperation::ReplaceApplicability { .. }
40 )
41 })
42 .map(|patch| patch.target_template.clone())
43 .collect();
44 let mut added_applicability: BTreeSet<String> = applied
45 .iter()
46 .filter(|patch| matches!(&patch.operation, PatchOperation::AddApplicability(_)))
47 .map(|patch| patch.target_template.clone())
48 .collect();
49
50 for patch in patches {
51 if !ids.insert(patch.id.clone()) {
52 return Err(PatchError::DuplicateId(patch.id.clone()));
53 }
54 if patch.edition != self.manifest().edition {
55 return Err(PatchError::EditionMismatch {
56 patch_id: patch.id.clone(),
57 patch_edition: patch.edition,
58 catalog_edition: self.manifest().edition,
59 });
60 }
61 let template = templates
62 .iter_mut()
63 .find(|template| template.name == patch.target_template)
64 .ok_or_else(|| PatchError::UnknownTemplate {
65 patch_id: patch.id.clone(),
66 template: patch.target_template.clone(),
67 })?;
68
69 match &patch.operation {
70 PatchOperation::AddApplicability(selector) => {
71 if replaced_applicability.contains(&template.name) {
72 return Err(PatchError::ConflictingApplicability {
73 template: template.name.clone(),
74 });
75 }
76 if template
77 .applicability
78 .iter()
79 .any(|existing| same_selector(existing, selector))
80 {
81 return Err(PatchError::AlreadyApplied {
82 patch_id: patch.id.clone(),
83 template: template.name.clone(),
84 });
85 }
86 template.applicability.push(selector.clone());
87 added_applicability.insert(template.name.clone());
88 }
89 PatchOperation::ReplaceApplicability {
90 expected,
91 replacement,
92 } => {
93 if added_applicability.contains(&template.name)
94 || !replaced_applicability.insert(template.name.clone())
95 {
96 return Err(PatchError::ConflictingApplicability {
97 template: template.name.clone(),
98 });
99 }
100 if template.applicability != *expected {
101 return Err(PatchError::StaleTarget {
102 patch_id: patch.id.clone(),
103 template: template.name.clone(),
104 });
105 }
106 template.applicability = replacement.clone();
107 }
108 PatchOperation::AddProperty(property) => {
109 let SetTemplateKind::Property { properties, .. } = &mut template.kind else {
110 return Err(PatchError::NotAPropertySet {
111 patch_id: patch.id.clone(),
112 template: template.name.clone(),
113 });
114 };
115 if properties
116 .iter()
117 .any(|existing| existing.name == property.name)
118 {
119 return Err(PatchError::AlreadyApplied {
120 patch_id: patch.id.clone(),
121 template: template.name.clone(),
122 });
123 }
124 properties.push(property.clone());
125 }
126 PatchOperation::ReplaceQuantityKind {
127 member,
128 expected,
129 replacement,
130 } => {
131 let SetTemplateKind::Quantity { quantities, .. } = &mut template.kind else {
132 return Err(PatchError::NotAQuantitySet {
133 patch_id: patch.id.clone(),
134 template: template.name.clone(),
135 });
136 };
137 let quantity = quantities
138 .iter_mut()
139 .find(|quantity| quantity.name == *member)
140 .ok_or_else(|| PatchError::UnknownMember {
141 patch_id: patch.id.clone(),
142 template: template.name.clone(),
143 member: member.clone(),
144 })?;
145 if quantity.kind != *expected {
146 return Err(PatchError::StaleTarget {
147 patch_id: patch.id.clone(),
148 template: template.name.clone(),
149 });
150 }
151 quantity.kind = *replacement;
152 }
153 PatchOperation::ReplacePropertyKind {
154 member,
155 expected,
156 replacement,
157 } => {
158 let SetTemplateKind::Property { properties, .. } = &mut template.kind else {
159 return Err(PatchError::NotAPropertySet {
160 patch_id: patch.id.clone(),
161 template: template.name.clone(),
162 });
163 };
164 let property = properties
165 .iter_mut()
166 .find(|property| property.name == *member)
167 .ok_or_else(|| PatchError::UnknownMember {
168 patch_id: patch.id.clone(),
169 template: template.name.clone(),
170 member: member.clone(),
171 })?;
172 if property.kind != *expected {
173 return Err(PatchError::StaleTarget {
174 patch_id: patch.id.clone(),
175 template: template.name.clone(),
176 });
177 }
178 property.kind = replacement.clone();
179 }
180 PatchOperation::AddAdvisory { severity, message } => {
181 advisories.push(Advisory {
182 patch_id: patch.id.clone(),
183 target_template: template.name.clone(),
184 severity: *severity,
185 message: message.clone(),
186 evidence: patch.evidence.clone(),
187 });
188 }
189 }
190 applied.push(AppliedPatch {
191 id: patch.id.clone(),
192 target_template: patch.target_template.clone(),
193 rationale: patch.rationale.clone(),
194 evidence: patch.evidence.clone(),
195 operation: patch.operation.clone(),
196 });
197 }
198
199 let catalog = Catalog::try_new_with_profile(self.manifest().clone(), profile, templates)?;
200 Ok(catalog.with_overlay_state(profile, applied, advisories))
201 }
202}
203
204fn same_selector(
205 left: &crate::definition::Applicability,
206 right: &crate::definition::Applicability,
207) -> bool {
208 left.entity.eq_ignore_ascii_case(&right.entity)
209 && match (&left.predefined_type, &right.predefined_type) {
210 (None, None) => true,
211 (Some(left), Some(right)) => left.eq_ignore_ascii_case(right),
212 _ => false,
213 }
214}