22
33pub ( crate ) mod utils;
44
5- use catalyst_types:: { id_uri:: IdUri , problem_report:: ProblemReport } ;
6- use rbac_registration:: cardano:: cip509:: SimplePublicKeyType ;
5+ use catalyst_types:: problem_report:: ProblemReport ;
76
87use crate :: {
98 doc_types:: { CommentDocument , DocumentType , ProposalDocument } ,
109 error:: CatalystSignedDocError ,
1110 CatalystSignedDocument , DocumentRef ,
1211} ;
1312
14- /// Trait for getting a necessary data needed during the validation process.
15- pub trait ValidationDataProvider {
16- /// Get public keys
17- fn get_public_key ( & self , kid : & IdUri ) -> Option < SimplePublicKeyType > ;
18- /// Get signed document by document reference
19- fn get_doc ( & self , doc_ref : & DocumentRef ) -> Option < CatalystSignedDocument > ;
20- }
21-
2213/// Stateless validation function rule type
2314pub ( crate ) type StatelessRule = fn ( & CatalystSignedDocument , & ProblemReport ) -> bool ;
2415/// Statefull validation function rule type
25- pub ( crate ) type StatefullRule < T > = fn ( & T , & dyn ValidationDataProvider , & ProblemReport ) -> bool ;
16+ pub ( crate ) type StatefullRule < DocType , DocProvider > =
17+ fn ( & DocType , & DocProvider , & ProblemReport ) -> bool ;
18+
19+ /// Trait for defining a statefull validation rules.
20+ pub trait StatefullValidation < DocProvider >
21+ where
22+ Self : ' static ,
23+ DocProvider : ' static + Fn ( & DocumentRef ) -> Option < CatalystSignedDocument > ,
24+ {
25+ /// Statefull validation rules
26+ const STATEFULL_RULES : & [ StatefullRule < Self , DocProvider > ] ;
2627
27- /// Trait for defining a validation rules.
28- pub trait Validator
28+ /// Perform a statefull validation, collecting a problem report
29+ fn validate ( & self , provider : & DocProvider , report : & ProblemReport ) -> bool {
30+ Self :: STATEFULL_RULES
31+ . iter ( )
32+ . map ( |rule| rule ( self , provider, report) )
33+ . all ( |res| res)
34+ }
35+ }
36+
37+ /// Trait for defining a stateless validation rules.
38+ pub trait StatelessValidation
2939where Self : ' static
3040{
3141 /// Stateless validation rules
3242 const STATELESS_RULES : & [ StatelessRule ] ;
33- /// Statefull validation rules
34- const STATEFULL_RULES : & [ StatefullRule < Self > ] ;
3543
3644 /// Perform a stateless validation, collecting a problem report
37- fn stateless_validation ( doc : & CatalystSignedDocument , report : & ProblemReport ) -> bool {
45+ fn validate ( doc : & CatalystSignedDocument , report : & ProblemReport ) -> bool {
3846 Self :: STATELESS_RULES
3947 . iter ( )
4048 . map ( |rule| rule ( doc, report) )
4149 . all ( |res| res)
4250 }
43-
44- /// Perform a statefull validation, collecting a problem report
45- fn statefull_validation (
46- & self , provider : & impl ValidationDataProvider , report : & ProblemReport ,
47- ) -> bool {
48- Self :: STATEFULL_RULES
49- . iter ( )
50- . map ( |rule| rule ( self , provider, report) )
51- . all ( |res| res)
52- }
5351}
5452
5553/// A comprehensive validation of the `CatalystSignedDocument`,
@@ -58,9 +56,10 @@ where Self: 'static
5856/// # Errors
5957///
6058/// Returns a report of validation failures and the source error.
61- pub fn validate (
62- doc : & CatalystSignedDocument , doc_provider : & impl ValidationDataProvider ,
63- ) -> Result < ( ) , CatalystSignedDocError > {
59+ pub fn validate < DocProvider > (
60+ doc : & CatalystSignedDocument , provider : & DocProvider ,
61+ ) -> Result < ( ) , CatalystSignedDocError >
62+ where DocProvider : ' static + Fn ( & DocumentRef ) -> Option < CatalystSignedDocument > {
6463 let report = ProblemReport :: new ( "Catalyst Signed Document Validation" ) ;
6564
6665 let doc_type: DocumentType = match doc. doc_type ( ) . try_into ( ) {
@@ -83,13 +82,13 @@ pub fn validate(
8382 match doc_type {
8483 DocumentType :: ProposalDocument => {
8584 if let Ok ( proposal_doc) = ProposalDocument :: from_signed_doc ( doc, & report) {
86- proposal_doc. statefull_validation ( doc_provider , & report) ;
85+ proposal_doc. validate ( provider , & report) ;
8786 }
8887 } ,
8988 DocumentType :: ProposalTemplate => { } ,
9089 DocumentType :: CommentDocument => {
9190 if let Ok ( comment_doc) = CommentDocument :: from_signed_doc ( doc, & report) {
92- comment_doc. statefull_validation ( doc_provider , & report) ;
91+ comment_doc. validate ( provider , & report) ;
9392 }
9493 } ,
9594 DocumentType :: CommentTemplate => { } ,
0 commit comments