Skip to content

Commit 3a583fd

Browse files
authored
Merge pull request #577 from justahero/sebastian/revert-license-handling
Revert Licenses & LicenseChoice changes
2 parents c9ba71e + a019e34 commit 3a583fd

File tree

10 files changed

+208
-549
lines changed

10 files changed

+208
-549
lines changed

cargo-cyclonedx/src/generator.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl SbomGenerator {
332332
}
333333

334334
fn get_licenses(&self, package: &Package) -> Option<Licenses> {
335-
let mut licenses: Option<LicenseChoice> = None;
335+
let mut licenses = vec![];
336336

337337
if let Some(license) = &package.license {
338338
let parse_mode = self
@@ -355,7 +355,7 @@ impl SbomGenerator {
355355
};
356356

357357
match result {
358-
Ok(expression) => licenses = Some(LicenseChoice::Expressions(vec![expression])),
358+
Ok(expression) => licenses.push(LicenseChoice::Expression(expression)),
359359
Err(err) => {
360360
let level = match &self.config.license_parser {
361361
Some(opts) if opts.accept_named.contains(license) => Level::Info,
@@ -368,19 +368,17 @@ impl SbomGenerator {
368368
license,
369369
err,
370370
);
371-
licenses = Some(LicenseChoice::Licenses(vec![License::named_license(
372-
license,
373-
)]));
371+
licenses.push(LicenseChoice::License(License::named_license(license)))
374372
}
375373
}
376374
}
377375

378-
if let Some(licenses) = licenses {
379-
Some(Licenses(licenses))
380-
} else {
376+
if licenses.is_empty() {
381377
log::trace!("Package {} has no licenses", package.name);
382-
None
378+
return None;
383379
}
380+
381+
Some(Licenses(licenses))
384382
}
385383

386384
fn create_metadata(&self, package: &Package) -> Result<Metadata, GeneratorError> {

cyclonedx-bom/src/models/component.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,9 @@ mod test {
637637
alg: HashAlgorithm::MD5,
638638
content: HashValue("a3bf1f3d584747e2569483783ddee45b".to_string()),
639639
}])),
640-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
640+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
641641
"MIT".to_string(),
642-
)]))),
642+
))])),
643643
copyright: Some(NormalizedString::new("copyright")),
644644
cpe: Some(Cpe("cpe:/a:example:mylibrary:1.0.0".to_string())),
645645
purl: Some(Purl("pkg:cargo/[email protected]".to_string())),
@@ -687,9 +687,9 @@ mod test {
687687
}])),
688688
components: Some(Components(vec![])),
689689
evidence: Some(ComponentEvidence {
690-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
690+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
691691
"MIT".to_string(),
692-
)]))),
692+
))])),
693693
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
694694
}),
695695
}])
@@ -721,9 +721,9 @@ mod test {
721721
alg: HashAlgorithm::MD5,
722722
content: HashValue("invalid hash content".to_string()),
723723
}])),
724-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
724+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
725725
"invalid license".to_string(),
726-
)]))),
726+
))])),
727727
copyright: Some(NormalizedString("invalid\tcopyright".to_string())),
728728
cpe: Some(Cpe("invalid cpe".to_string())),
729729
purl: Some(Purl("invalid purl".to_string())),
@@ -775,9 +775,9 @@ mod test {
775775
}])),
776776
components: Some(Components(vec![invalid_component()])),
777777
evidence: Some(ComponentEvidence {
778-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
778+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
779779
"invalid license".to_string(),
780-
)]))),
780+
))])),
781781
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
782782
}),
783783
}])

cyclonedx-bom/src/models/license.rs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ use crate::validation::{
3434
/// As defined via the [CycloneDX XML schema](https://cyclonedx.org/docs/1.3/xml/#type_licenseChoiceType)
3535
#[derive(Debug, PartialEq, Eq)]
3636
pub enum LicenseChoice {
37-
Licenses(Vec<License>),
38-
Expressions(Vec<SpdxExpression>),
37+
License(License),
38+
Expression(SpdxExpression),
39+
}
40+
41+
impl LicenseChoice {
42+
pub fn is_license(&self) -> bool {
43+
matches!(self, Self::License(_))
44+
}
3945
}
4046

4147
impl Validate for LicenseChoice {
@@ -46,31 +52,23 @@ impl Validate for LicenseChoice {
4652
let mut results: Vec<ValidationResult> = vec![];
4753

4854
match self {
49-
LicenseChoice::Licenses(licenses) => {
50-
for (index, license) in licenses.iter().enumerate() {
51-
let license_context = context.extend_context(vec![
52-
ValidationPathComponent::Array { index },
53-
ValidationPathComponent::EnumVariant {
54-
variant_name: "License".to_string(),
55-
},
56-
]);
57-
results.push(license.validate_with_context(license_context)?);
58-
}
55+
LicenseChoice::License(license) => {
56+
let license_context =
57+
context.extend_context(vec![ValidationPathComponent::EnumVariant {
58+
variant_name: "License".to_string(),
59+
}]);
60+
results.push(license.validate_with_context(license_context)?);
5961

6062
Ok(results
6163
.into_iter()
6264
.fold(ValidationResult::default(), |acc, result| acc.merge(result)))
6365
}
64-
LicenseChoice::Expressions(expressions) => {
65-
for (index, expression) in expressions.iter().enumerate() {
66-
let expression_context = context.extend_context(vec![
67-
ValidationPathComponent::Array { index },
68-
ValidationPathComponent::EnumVariant {
69-
variant_name: "Expression".to_string(),
70-
},
71-
]);
72-
results.push(expression.validate_with_context(expression_context)?);
73-
}
66+
LicenseChoice::Expression(expression) => {
67+
let expression_context =
68+
context.extend_context(vec![ValidationPathComponent::EnumVariant {
69+
variant_name: "Expression".to_string(),
70+
}]);
71+
results.push(expression.validate_with_context(expression_context)?);
7472

7573
Ok(results
7674
.into_iter()
@@ -156,14 +154,24 @@ impl Validate for License {
156154
}
157155

158156
#[derive(Debug, PartialEq, Eq)]
159-
pub struct Licenses(pub LicenseChoice);
157+
pub struct Licenses(pub Vec<LicenseChoice>);
160158

161159
impl Validate for Licenses {
162160
fn validate_with_context(
163161
&self,
164162
context: ValidationContext,
165163
) -> Result<ValidationResult, ValidationError> {
166-
self.0.validate_with_context(context)
164+
let mut results: Vec<ValidationResult> = vec![];
165+
166+
for (index, license_choice) in self.0.iter().enumerate() {
167+
let license_choice_context =
168+
context.extend_context(vec![ValidationPathComponent::Array { index }]);
169+
results.push(license_choice.validate_with_context(license_choice_context)?);
170+
}
171+
172+
Ok(results
173+
.into_iter()
174+
.fold(ValidationResult::default(), |acc, result| acc.merge(result)))
167175
}
168176
}
169177

@@ -208,9 +216,9 @@ mod test {
208216

209217
#[test]
210218
fn it_should_pass_validation() {
211-
let validation_result = Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
219+
let validation_result = Licenses(vec![LicenseChoice::Expression(SpdxExpression(
212220
"MIT OR Apache-2.0".to_string(),
213-
)]))
221+
))])
214222
.validate_with_context(ValidationContext::default())
215223
.expect("Error while validating");
216224

@@ -219,13 +227,13 @@ mod test {
219227

220228
#[test]
221229
fn it_should_fail_validation_for_license_name() {
222-
let validation_result = Licenses(LicenseChoice::Licenses(vec![License {
230+
let validation_result = Licenses(vec![LicenseChoice::License(License {
223231
license_identifier: LicenseIdentifier::Name(NormalizedString(
224232
"spaces and \ttabs".to_string(),
225233
)),
226234
text: None,
227235
url: None,
228-
}]))
236+
})])
229237
.validate_with_context(ValidationContext::default())
230238
.expect("Error while validating");
231239

@@ -255,11 +263,11 @@ mod test {
255263

256264
#[test]
257265
fn it_should_fail_validation_for_license_id() {
258-
let validation_result = Licenses(LicenseChoice::Licenses(vec![License {
266+
let validation_result = Licenses(vec![LicenseChoice::License(License {
259267
license_identifier: LicenseIdentifier::SpdxId(SpdxIdentifier("Apache=2.0".to_string())),
260268
text: None,
261269
url: None,
262-
}]))
270+
})])
263271
.validate_with_context(ValidationContext::default())
264272
.expect("Error while validating");
265273

@@ -288,9 +296,9 @@ mod test {
288296

289297
#[test]
290298
fn it_should_fail_validation_for_license_expression() {
291-
let validation_result = Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
299+
let validation_result = Licenses(vec![LicenseChoice::Expression(SpdxExpression(
292300
"MIT OR".to_string(),
293-
)]))
301+
))])
294302
.validate_with_context(ValidationContext::default())
295303
.expect("Error while validating");
296304

@@ -312,27 +320,27 @@ mod test {
312320

313321
#[test]
314322
fn it_should_merge_validations_correctly_license_choice_licenses() {
315-
let validation_result = Licenses(LicenseChoice::Licenses(vec![
316-
License {
323+
let validation_result = Licenses(vec![
324+
LicenseChoice::License(License {
317325
license_identifier: LicenseIdentifier::Name(NormalizedString("MIT".to_string())),
318326
text: None,
319327
url: None,
320-
},
321-
License {
328+
}),
329+
LicenseChoice::License(License {
322330
license_identifier: LicenseIdentifier::Name(NormalizedString(
323331
"spaces and \ttabs".to_string(),
324332
)),
325333
text: None,
326334
url: None,
327-
},
328-
License {
335+
}),
336+
LicenseChoice::License(License {
329337
license_identifier: LicenseIdentifier::SpdxId(SpdxIdentifier(
330338
"Apache=2.0".to_string(),
331339
)),
332340
text: None,
333341
url: None,
334-
},
335-
]))
342+
}),
343+
])
336344
.validate_with_context(ValidationContext::default())
337345
.expect("Error while validating");
338346

@@ -381,11 +389,11 @@ mod test {
381389

382390
#[test]
383391
fn it_should_merge_validations_correctly_license_choice_expressions() {
384-
let validation_result = Licenses(LicenseChoice::Expressions(vec![
385-
SpdxExpression("MIT OR Apache-2.0".to_string()),
386-
SpdxExpression("MIT OR".to_string()),
387-
SpdxExpression("MIT OR".to_string()),
388-
]))
392+
let validation_result = Licenses(vec![
393+
LicenseChoice::Expression(SpdxExpression("MIT OR Apache-2.0".to_string())),
394+
LicenseChoice::Expression(SpdxExpression("MIT OR".to_string())),
395+
LicenseChoice::Expression(SpdxExpression("MIT OR".to_string())),
396+
])
389397
.validate_with_context(ValidationContext::default())
390398
.expect("Error while validating");
391399

cyclonedx-bom/src/models/metadata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ mod test {
202202
url: None,
203203
contact: None,
204204
}),
205-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
205+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
206206
"MIT".to_string(),
207-
)]))),
207+
))])),
208208
properties: Some(Properties(vec![Property {
209209
name: "name".to_string(),
210210
value: NormalizedString::new("value"),
@@ -266,9 +266,9 @@ mod test {
266266
url: None,
267267
contact: None,
268268
}),
269-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
269+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
270270
"invalid license".to_string(),
271-
)]))),
271+
))])),
272272
properties: Some(Properties(vec![Property {
273273
name: "name".to_string(),
274274
value: NormalizedString("invalid\tvalue".to_string()),

cyclonedx-bom/src/models/service.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@ mod test {
310310
flow: DataFlowType::Inbound,
311311
classification: NormalizedString::new("classification"),
312312
}]),
313-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
313+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
314314
"MIT".to_string(),
315-
)]))),
315+
))])),
316316
external_references: Some(ExternalReferences(vec![ExternalReference {
317317
external_reference_type: ExternalReferenceType::Bom,
318318
url: Uri("https://www.example.com".to_string()),
@@ -351,9 +351,9 @@ mod test {
351351
flow: DataFlowType::UnknownDataFlow("unknown".to_string()),
352352
classification: NormalizedString("invalid\tclassification".to_string()),
353353
}]),
354-
licenses: Some(Licenses(LicenseChoice::Expressions(vec![SpdxExpression(
354+
licenses: Some(Licenses(vec![LicenseChoice::Expression(SpdxExpression(
355355
"invalid license".to_string(),
356-
)]))),
356+
))])),
357357
external_references: Some(ExternalReferences(vec![ExternalReference {
358358
external_reference_type: ExternalReferenceType::UnknownExternalReferenceType(
359359
"unknown".to_string(),

0 commit comments

Comments
 (0)