@@ -21,7 +21,7 @@ use std::convert::TryFrom;
2121use spdx:: { Expression , ParseMode } ;
2222use thiserror:: Error ;
2323
24- use crate :: validation:: { Validate , ValidationResult } ;
24+ use crate :: validation:: ValidationError ;
2525
2626/// An identifier for a single, specific license
2727///
@@ -80,15 +80,10 @@ impl ToString for SpdxIdentifier {
8080 }
8181}
8282
83- impl Validate for SpdxIdentifier {
84- fn validate_with_context (
85- & self ,
86- context : crate :: validation:: ValidationContext ,
87- ) -> ValidationResult {
88- match Self :: try_from ( self . 0 . clone ( ) ) {
89- Ok ( _) => ValidationResult :: Passed ,
90- Err ( _) => ValidationResult :: failure ( "SPDX identifier is not valid" , context) ,
91- }
83+ pub fn validate_spdx_identifier ( identifier : & SpdxIdentifier ) -> Result < ( ) , ValidationError > {
84+ match SpdxIdentifier :: try_from ( identifier. 0 . to_string ( ) ) {
85+ Err ( _error) => Err ( ValidationError :: new ( "SPDX identifier is not valid" ) ) ,
86+ _ => Ok ( ( ) ) ,
9287 }
9388}
9489
@@ -175,16 +170,11 @@ impl ToString for SpdxExpression {
175170 }
176171}
177172
178- impl Validate for SpdxExpression {
179- fn validate_with_context (
180- & self ,
181- context : crate :: validation:: ValidationContext ,
182- ) -> ValidationResult {
183- match SpdxExpression :: try_from ( self . 0 . clone ( ) ) {
184- Ok ( _) => ValidationResult :: Passed ,
185- Err ( _) => ValidationResult :: failure ( "SPDX expression is not valid" , context) ,
186- }
173+ pub fn validate_spdx_expression ( expression : & SpdxExpression ) -> Result < ( ) , ValidationError > {
174+ if Expression :: parse ( & expression. 0 ) . is_err ( ) {
175+ return Err ( ValidationError :: new ( "SPDX expression is not valid" ) ) ;
187176 }
177+ Ok ( ( ) )
188178}
189179
190180#[ derive( Debug , Error , PartialEq , Eq ) ]
@@ -198,8 +188,6 @@ pub enum SpdxExpressionError {
198188
199189#[ cfg( test) ]
200190mod test {
201- use crate :: validation:: { ValidationContext , ValidationResult } ;
202-
203191 use super :: * ;
204192 use pretty_assertions:: assert_eq;
205193
@@ -247,18 +235,19 @@ mod test {
247235
248236 #[ test]
249237 fn valid_spdx_identifiers_should_pass_validation ( ) {
250- let validation_result = SpdxIdentifier ( "MIT" . to_string ( ) ) . validate ( ) ;
238+ let validaton_result = validate_spdx_identifier ( & SpdxIdentifier ( "MIT" . to_string ( ) ) ) ;
251239
252- assert_eq ! ( validation_result , ValidationResult :: Passed ) ;
240+ assert ! ( validaton_result . is_ok ( ) ) ;
253241 }
254242
255243 #[ test]
256244 fn invalid_spdx_identifiers_should_fail_validation ( ) {
257- let validation_result = SpdxIdentifier ( "MIT OR Apache-2.0" . to_string ( ) ) . validate ( ) ;
245+ let validation_result =
246+ validate_spdx_identifier ( & SpdxIdentifier ( "MIT OR Apache-2.0" . to_string ( ) ) ) ;
258247
259248 assert_eq ! (
260249 validation_result,
261- ValidationResult :: failure ( "SPDX identifier is not valid" , ValidationContext :: default ( ) ) ,
250+ Err ( "SPDX identifier is not valid" . into ( ) ) ,
262251 ) ;
263252 }
264253
@@ -288,18 +277,20 @@ mod test {
288277
289278 #[ test]
290279 fn valid_spdx_expressions_should_pass_validation ( ) {
291- let validation_result = SpdxExpression ( "MIT OR Apache-2.0" . to_string ( ) ) . validate ( ) ;
280+ let validation_result =
281+ validate_spdx_expression ( & SpdxExpression ( "MIT OR Apache-2.0" . to_string ( ) ) ) ;
292282
293- assert_eq ! ( validation_result, ValidationResult :: Passed ) ;
283+ assert ! ( validation_result. is_ok ( ) ) ;
294284 }
295285
296286 #[ test]
297287 fn invalid_spdx_expressions_should_fail_validation ( ) {
298- let validation_result = SpdxExpression ( "not a real license" . to_string ( ) ) . validate ( ) ;
288+ let validation_result =
289+ validate_spdx_expression ( & SpdxExpression ( "not a real license" . to_string ( ) ) ) ;
299290
300291 assert_eq ! (
301292 validation_result,
302- ValidationResult :: failure ( "SPDX expression is not valid" , ValidationContext :: default ( ) )
293+ Err ( "SPDX expression is not valid" . into ( ) ) ,
303294 ) ;
304295 }
305296}
0 commit comments