@@ -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 ) ]
3636pub 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
4147impl 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
161159impl 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 \t tabs" . 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 \t tabs" . 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
0 commit comments