Skip to content

Commit 135d279

Browse files
committed
Rename method in ValidationContext
This renames function `extend_context_with_struct_field to `with_struct`. Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent 9867f9b commit 135d279

21 files changed

+148
-155
lines changed

cyclonedx-bom/src/models/advisory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ impl Validate for Advisory {
4949
let mut results: Vec<ValidationResult> = vec![];
5050

5151
if let Some(title) = &self.title {
52-
let context = context.extend_context_with_struct_field("Advisory", "title");
52+
let context = context.with_struct("Advisory", "title");
5353

5454
results.push(title.validate_with_context(context));
5555
}
5656

57-
let url_context = context.extend_context_with_struct_field("Advisory", "url");
57+
let url_context = context.with_struct("Advisory", "url");
5858
results.push(self.url.validate_with_context(url_context));
5959

6060
results

cyclonedx-bom/src/models/attached_text.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Validate for AttachedText {
4949
let mut results: Vec<ValidationResult> = vec![];
5050

5151
if let Some(content_type) = &self.content_type {
52-
let context = context.extend_context_with_struct_field("AttachedText", "content_type");
52+
let context = context.with_struct("AttachedText", "content_type");
5353

5454
results.push(content_type.validate_with_context(context));
5555
}
@@ -58,8 +58,7 @@ impl Validate for AttachedText {
5858
match (encoding, STANDARD.decode(self.content.clone())) {
5959
(Encoding::Base64, Ok(_)) => results.push(ValidationResult::Passed),
6060
(Encoding::Base64, Err(_)) => {
61-
let context =
62-
context.extend_context_with_struct_field("AttachedText", "content");
61+
let context = context.with_struct("AttachedText", "content");
6362

6463
results.push(ValidationResult::Failed {
6564
reasons: vec![FailureReason {
@@ -69,8 +68,7 @@ impl Validate for AttachedText {
6968
})
7069
}
7170
(Encoding::UnknownEncoding(_), _) => {
72-
let context =
73-
context.extend_context_with_struct_field("AttachedText", "encoding");
71+
let context = context.with_struct("AttachedText", "encoding");
7472

7573
results.push(encoding.validate_with_context(context));
7674
}
@@ -124,7 +122,7 @@ impl Validate for Encoding {
124122

125123
#[cfg(test)]
126124
mod test {
127-
use crate::{models::attached_text::Encoding, validation::ValidationPathComponent};
125+
use crate::models::attached_text::Encoding;
128126

129127
use super::*;
130128
use pretty_assertions::assert_eq;
@@ -174,17 +172,12 @@ mod test {
174172
message:
175173
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
176174
.to_string(),
177-
context: ValidationContext(vec![ValidationPathComponent::Struct {
178-
struct_name: "AttachedText".to_string(),
179-
field_name: "content_type".to_string()
180-
}])
175+
context: ValidationContext::new()
176+
.with_struct("AttachedText", "content_type")
181177
},
182178
FailureReason {
183179
message: "Content is not Base64 encoded".to_string(),
184-
context: ValidationContext(vec![ValidationPathComponent::Struct {
185-
struct_name: "AttachedText".to_string(),
186-
field_name: "content".to_string()
187-
}])
180+
context: ValidationContext::new().with_struct("AttachedText", "content")
188181
}
189182
]
190183
}
@@ -202,16 +195,11 @@ mod test {
202195

203196
assert_eq!(
204197
validation_result,
205-
ValidationResult::Failed {
206-
reasons: vec![FailureReason {
207-
message: "Unknown encoding".to_string(),
208-
context: ValidationContext(vec![ValidationPathComponent::Struct {
209-
struct_name: "AttachedText".to_string(),
210-
field_name: "encoding".to_string()
211-
}])
212-
}]
213-
}
214-
);
198+
ValidationResult::failure(
199+
"Unknown encoding",
200+
ValidationContext::new().with_struct("AttachedText", "encoding")
201+
)
202+
)
215203
}
216204

217205
#[test]

cyclonedx-bom/src/models/bom.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ impl Validate for Bom {
226226
let mut bom_refs_context = BomReferencesContext::default();
227227

228228
if let Some(serial_number) = &self.serial_number {
229-
let context = context.extend_context_with_struct_field("Bom", "serial_number");
229+
let context = context.with_struct("Bom", "serial_number");
230230

231231
results.push(serial_number.validate_with_context(context));
232232
}
233233

234234
if let Some(metadata) = &self.metadata {
235-
let context = context.extend_context_with_struct_field("Bom", "metadata");
235+
let context = context.with_struct("Bom", "metadata");
236236
let component_bom_ref_context =
237-
context.extend_context_with_struct_field("Metadata", "component");
237+
context.with_struct("Metadata", "component");
238238

239239
results.push(metadata.validate_with_context(context));
240240

@@ -249,7 +249,7 @@ impl Validate for Bom {
249249
}
250250

251251
if let Some(components) = &self.components {
252-
let context = context.extend_context_with_struct_field("Bom", "components");
252+
let context = context.with_struct("Bom", "components");
253253
let component_bom_ref_context = context.clone();
254254

255255
results.push(components.validate_with_context(context));
@@ -264,7 +264,7 @@ impl Validate for Bom {
264264
}
265265

266266
if let Some(services) = &self.services {
267-
let context = context.extend_context_with_struct_field("Bom", "services");
267+
let context = context.with_struct("Bom", "services");
268268
let service_bom_ref_context = context.clone();
269269

270270
results.push(services.validate_with_context(context));
@@ -279,21 +279,21 @@ impl Validate for Bom {
279279
}
280280

281281
if let Some(external_references) = &self.external_references {
282-
let context = context.extend_context_with_struct_field("Bom", "external_references");
282+
let context = context.with_struct("Bom", "external_references");
283283

284284
results.push(external_references.validate_with_context(context));
285285
}
286286

287287
if let Some(dependencies) = &self.dependencies {
288-
let context = context.extend_context_with_struct_field("Bom", "dependencies");
288+
let context = context.with_struct("Bom", "dependencies");
289289

290290
for (dependency_index, dependency) in dependencies.0.iter().enumerate() {
291291
let context = context.extend_context(vec![ValidationPathComponent::Array {
292292
index: dependency_index,
293293
}]);
294294
if !bom_refs_context.contains(&dependency.dependency_ref) {
295295
let dependency_context =
296-
context.extend_context_with_struct_field("Dependency", "dependency_ref");
296+
context.with_struct("Dependency", "dependency_ref");
297297

298298
results.push(ValidationResult::Failed {
299299
reasons: vec![FailureReason {
@@ -330,7 +330,7 @@ impl Validate for Bom {
330330
}
331331

332332
if let Some(compositions) = &self.compositions {
333-
let context = context.extend_context_with_struct_field("Bom", "compositions");
333+
let context = context.with_struct("Bom", "compositions");
334334
let compositions_context = context.clone();
335335

336336
results.push(compositions.validate_with_context(context));
@@ -343,7 +343,7 @@ impl Validate for Bom {
343343

344344
if let Some(assemblies) = &composition.assemblies {
345345
let compositions_context = compositions_context
346-
.extend_context_with_struct_field("Composition", "assemblies");
346+
.with_struct("Composition", "assemblies");
347347
for (assembly_index, BomReference(assembly)) in assemblies.iter().enumerate() {
348348
if !bom_refs_context.contains(assembly) {
349349
let compositions_context = compositions_context.extend_context(vec![
@@ -364,7 +364,7 @@ impl Validate for Bom {
364364

365365
if let Some(dependencies) = &composition.dependencies {
366366
let compositions_context = compositions_context
367-
.extend_context_with_struct_field("Composition", "dependencies");
367+
.with_struct("Composition", "dependencies");
368368
for (dependency_index, BomReference(dependency)) in
369369
dependencies.iter().enumerate()
370370
{
@@ -388,13 +388,13 @@ impl Validate for Bom {
388388
}
389389

390390
if let Some(properties) = &self.properties {
391-
let context = context.extend_context_with_struct_field("Bom", "properties");
391+
let context = context.with_struct("Bom", "properties");
392392

393393
results.push(properties.validate_with_context(context));
394394
}
395395

396396
if let Some(vulnerabilities) = &self.vulnerabilities {
397-
let context = context.extend_context_with_struct_field("Bom", "vulnerabilities");
397+
let context = context.with_struct("Bom", "vulnerabilities");
398398
results.push(vulnerabilities.validate_with_context(context));
399399
}
400400

@@ -432,7 +432,7 @@ fn validate_component_bom_refs(
432432
) {
433433
if let Some(bom_ref) = &component.bom_ref {
434434
if bom_refs.contains(bom_ref) {
435-
let context = context.extend_context_with_struct_field("Component", "bom_ref");
435+
let context = context.with_struct("Component", "bom_ref");
436436
results.push(ValidationResult::Failed {
437437
reasons: vec![FailureReason {
438438
message: format!(r#"Bom ref "{bom_ref}" is not unique"#),
@@ -444,7 +444,7 @@ fn validate_component_bom_refs(
444444
}
445445

446446
if let Some(components) = &component.components {
447-
let context = context.extend_context_with_struct_field("Component", "components");
447+
let context = context.with_struct("Component", "components");
448448
validate_components(components, bom_refs, &context, results);
449449
}
450450
}
@@ -473,7 +473,7 @@ fn validate_service_bom_refs(
473473
) {
474474
if let Some(bom_ref) = &service.bom_ref {
475475
if bom_refs.contains(bom_ref) {
476-
let context = context.extend_context_with_struct_field("Service", "bom_ref");
476+
let context = context.with_struct("Service", "bom_ref");
477477
results.push(ValidationResult::Failed {
478478
reasons: vec![FailureReason {
479479
message: format!(r#"Bom ref "{bom_ref}" is not unique"#),
@@ -485,7 +485,7 @@ fn validate_service_bom_refs(
485485
}
486486

487487
if let Some(services) = &service.services {
488-
let context = context.extend_context_with_struct_field("Service", "services");
488+
let context = context.with_struct("Service", "services");
489489
validate_services(services, bom_refs, &context, results);
490490
}
491491
}

cyclonedx-bom/src/models/code.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ impl Validate for Commit {
3939
let mut results: Vec<ValidationResult> = vec![];
4040

4141
if let Some(uid) = &self.uid {
42-
let context = context.extend_context_with_struct_field("Commit", "uid");
42+
let context = context.with_struct("Commit", "uid");
4343

4444
results.push(uid.validate_with_context(context));
4545
}
4646

4747
if let Some(url) = &self.url {
48-
let context = context.extend_context_with_struct_field("Commit", "url");
48+
let context = context.with_struct("Commit", "url");
4949

5050
results.push(url.validate_with_context(context));
5151
}
5252

5353
if let Some(author) = &self.author {
54-
let context = context.extend_context_with_struct_field("Commit", "author");
54+
let context = context.with_struct("Commit", "author");
5555

5656
results.push(author.validate_with_context(context));
5757
}
5858

5959
if let Some(committer) = &self.committer {
60-
let context = context.extend_context_with_struct_field("Commit", "committer");
60+
let context = context.with_struct("Commit", "committer");
6161

6262
results.push(committer.validate_with_context(context));
6363
}
6464

6565
if let Some(message) = &self.message {
66-
let context = context.extend_context_with_struct_field("Commit", "message");
66+
let context = context.with_struct("Commit", "message");
6767

6868
results.push(message.validate_with_context(context));
6969
}
@@ -104,13 +104,13 @@ impl Validate for Diff {
104104
let mut results: Vec<ValidationResult> = vec![];
105105

106106
if let Some(text) = &self.text {
107-
let context = context.extend_context_with_struct_field("Diff", "text");
107+
let context = context.with_struct("Diff", "text");
108108

109109
results.push(text.validate_with_context(context));
110110
}
111111

112112
if let Some(url) = &self.url {
113-
let context = context.extend_context_with_struct_field("Diff", "url");
113+
let context = context.with_struct("Diff", "url");
114114

115115
results.push(url.validate_with_context(context));
116116
}
@@ -134,19 +134,19 @@ impl Validate for IdentifiableAction {
134134

135135
if let Some(timestamp) = &self.timestamp {
136136
let context =
137-
context.extend_context_with_struct_field("IdentifiableAction", "timestamp");
137+
context.with_struct("IdentifiableAction", "timestamp");
138138

139139
results.push(timestamp.validate_with_context(context));
140140
}
141141

142142
if let Some(name) = &self.name {
143-
let context = context.extend_context_with_struct_field("IdentifiableAction", "name");
143+
let context = context.with_struct("IdentifiableAction", "name");
144144

145145
results.push(name.validate_with_context(context));
146146
}
147147

148148
if let Some(email) = &self.email {
149-
let context = context.extend_context_with_struct_field("IdentifiableAction", "email");
149+
let context = context.with_struct("IdentifiableAction", "email");
150150

151151
results.push(email.validate_with_context(context));
152152
}
@@ -171,30 +171,30 @@ impl Validate for Issue {
171171
fn validate_with_context(&self, context: ValidationContext) -> ValidationResult {
172172
let mut results: Vec<ValidationResult> = vec![];
173173

174-
let issue_context = context.extend_context_with_struct_field("Issue", "issue_type");
174+
let issue_context = context.with_struct("Issue", "issue_type");
175175

176176
results.push(self.issue_type.validate_with_context(issue_context));
177177

178178
if let Some(id) = &self.id {
179-
let context = context.extend_context_with_struct_field("Issue", "id");
179+
let context = context.with_struct("Issue", "id");
180180

181181
results.push(id.validate_with_context(context));
182182
}
183183

184184
if let Some(name) = &self.name {
185-
let context = context.extend_context_with_struct_field("Issue", "name");
185+
let context = context.with_struct("Issue", "name");
186186

187187
results.push(name.validate_with_context(context));
188188
}
189189

190190
if let Some(description) = &self.description {
191-
let context = context.extend_context_with_struct_field("Issue", "description");
191+
let context = context.with_struct("Issue", "description");
192192

193193
results.push(description.validate_with_context(context));
194194
}
195195

196196
if let Some(source) = &self.source {
197-
let context = context.extend_context_with_struct_field("Issue", "source");
197+
let context = context.with_struct("Issue", "source");
198198

199199
results.push(source.validate_with_context(context));
200200
}
@@ -275,12 +275,12 @@ impl Validate for Patch {
275275
fn validate_with_context(&self, context: ValidationContext) -> ValidationResult {
276276
let mut results: Vec<ValidationResult> = vec![];
277277

278-
let patch_type_context = context.extend_context_with_struct_field("Patch", "patch_type");
278+
let patch_type_context = context.with_struct("Patch", "patch_type");
279279

280280
results.push(self.patch_type.validate_with_context(patch_type_context));
281281

282282
if let Some(diff) = &self.diff {
283-
let context = context.extend_context_with_struct_field("Patch", "diff");
283+
let context = context.with_struct("Patch", "diff");
284284

285285
results.push(diff.validate_with_context(context));
286286
}
@@ -382,13 +382,13 @@ impl Validate for Source {
382382
let mut results: Vec<ValidationResult> = vec![];
383383

384384
if let Some(name) = &self.name {
385-
let context = context.extend_context_with_struct_field("Source", "name");
385+
let context = context.with_struct("Source", "name");
386386

387387
results.push(name.validate_with_context(context));
388388
}
389389

390390
if let Some(url) = &self.url {
391-
let context = context.extend_context_with_struct_field("Source", "url");
391+
let context = context.with_struct("Source", "url");
392392

393393
results.push(url.validate_with_context(context));
394394
}

0 commit comments

Comments
 (0)