11//! `ref` rule type impl.
22
3- use std:: collections:: HashMap ;
4-
3+ use catalyst_signed_doc_spec:: { is_required:: IsRequired , metadata:: doc_ref:: Ref , DocSpecs } ;
54use catalyst_types:: problem_report:: ProblemReport ;
65
76use crate :: {
@@ -14,8 +13,8 @@ use crate::{
1413pub ( crate ) enum RefRule {
1514 /// Is 'ref' specified
1615 Specified {
17- /// expected `type` field of the referenced doc
18- exp_ref_types : Vec < DocType > ,
16+ /// allowed `type` field of the referenced doc
17+ allowed_type : Vec < DocType > ,
1918 /// allows multiple document references or only one
2019 multiple : bool ,
2120 /// optional flag for the `ref` field
@@ -27,20 +26,24 @@ pub(crate) enum RefRule {
2726impl RefRule {
2827 /// Generating `RefRule` from specs
2928 pub ( crate ) fn new (
30- docs : & HashMap < catalyst_signed_doc_spec :: DocumentName , catalyst_signed_doc_spec :: DocSpec > ,
31- ref_spec : & catalyst_signed_doc_spec :: metadata :: doc_ref :: Ref ,
29+ docs : & DocSpecs ,
30+ spec : & Ref ,
3231 ) -> anyhow:: Result < Self > {
33- let optional = match ref_spec. required {
34- catalyst_signed_doc_spec:: is_required:: IsRequired :: Yes => false ,
35- catalyst_signed_doc_spec:: is_required:: IsRequired :: Optional => true ,
36- catalyst_signed_doc_spec:: is_required:: IsRequired :: Excluded => {
32+ let optional = match spec. required {
33+ IsRequired :: Yes => false ,
34+ IsRequired :: Optional => true ,
35+ IsRequired :: Excluded => {
36+ anyhow:: ensure!(
37+ spec. doc_type. is_empty( ) && spec. multiple. is_none( ) ,
38+ "'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'ref' metadata definition"
39+ ) ;
3740 return Ok ( Self :: NotSpecified ) ;
3841 } ,
3942 } ;
4043
41- anyhow:: ensure!( !ref_spec . doc_type. is_empty( ) , "'type' field should exists and has at least one entry for the required 'ref' metadata definition" ) ;
44+ anyhow:: ensure!( !spec . doc_type. is_empty( ) , "'type' field should exists and has at least one entry for the required 'ref' metadata definition" ) ;
4245
43- let exp_ref_types = ref_spec . doc_type . iter ( ) . try_fold (
46+ let exp_ref_types = spec . doc_type . iter ( ) . try_fold (
4447 Vec :: new ( ) ,
4548 |mut res, doc_name| -> anyhow:: Result < _ > {
4649 let docs_spec = docs. get ( doc_name) . ok_or ( anyhow:: anyhow!(
@@ -51,12 +54,12 @@ impl RefRule {
5154 } ,
5255 ) ?;
5356
54- let multiple = ref_spec . multiple . ok_or ( anyhow:: anyhow!(
57+ let multiple = spec . multiple . ok_or ( anyhow:: anyhow!(
5558 "'multiple' field should exists for the required 'ref' metadata definition"
5659 ) ) ?;
5760
5861 Ok ( Self :: Specified {
59- exp_ref_types,
62+ allowed_type : exp_ref_types,
6063 multiple,
6164 optional,
6265 } )
@@ -73,7 +76,7 @@ impl RefRule {
7376 {
7477 let context: & str = "Ref rule check" ;
7578 if let Self :: Specified {
76- exp_ref_types,
79+ allowed_type : exp_ref_types,
7780 multiple,
7881 optional,
7982 } = self
@@ -462,7 +465,7 @@ mod tests {
462465 let doc = doc_gen ( & exp_types, & mut provider) ;
463466
464467 let non_optional_res = RefRule :: Specified {
465- exp_ref_types : exp_types. to_vec ( ) ,
468+ allowed_type : exp_types. to_vec ( ) ,
466469 multiple : true ,
467470 optional : false ,
468471 }
@@ -471,7 +474,7 @@ mod tests {
471474 . unwrap ( ) ;
472475
473476 let optional_res = RefRule :: Specified {
474- exp_ref_types : exp_types. to_vec ( ) ,
477+ allowed_type : exp_types. to_vec ( ) ,
475478 multiple : true ,
476479 optional : true ,
477480 }
@@ -564,7 +567,7 @@ mod tests {
564567 let doc = doc_gen ( & exp_types, & mut provider) ;
565568
566569 let non_optional_res = RefRule :: Specified {
567- exp_ref_types : exp_types. to_vec ( ) ,
570+ allowed_type : exp_types. to_vec ( ) ,
568571 multiple : false ,
569572 optional : false ,
570573 }
@@ -573,7 +576,7 @@ mod tests {
573576 . unwrap ( ) ;
574577
575578 let optional_res = RefRule :: Specified {
576- exp_ref_types : exp_types. to_vec ( ) ,
579+ allowed_type : exp_types. to_vec ( ) ,
577580 multiple : false ,
578581 optional : true ,
579582 }
@@ -589,7 +592,7 @@ mod tests {
589592 async fn ref_specified_optional_test ( ) {
590593 let provider = TestCatalystProvider :: default ( ) ;
591594 let rule = RefRule :: Specified {
592- exp_ref_types : vec ! [ UuidV4 :: new( ) . into( ) ] ,
595+ allowed_type : vec ! [ UuidV4 :: new( ) . into( ) ] ,
593596 multiple : true ,
594597 optional : true ,
595598 } ;
@@ -599,7 +602,7 @@ mod tests {
599602
600603 let provider = TestCatalystProvider :: default ( ) ;
601604 let rule = RefRule :: Specified {
602- exp_ref_types : vec ! [ UuidV4 :: new( ) . into( ) ] ,
605+ allowed_type : vec ! [ UuidV4 :: new( ) . into( ) ] ,
603606 multiple : true ,
604607 optional : false ,
605608 } ;
0 commit comments