@@ -25,6 +25,43 @@ pub(crate) enum RefRule {
2525 NotSpecified ,
2626}
2727impl RefRule {
28+ /// Generating `RefRule` from specs
29+ 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 ,
32+ ) -> 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 => {
37+ return Ok ( Self :: NotSpecified ) ;
38+ } ,
39+ } ;
40+
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" ) ;
42+
43+ let exp_ref_types = ref_spec. doc_type . iter ( ) . try_fold (
44+ Vec :: new ( ) ,
45+ |mut res, doc_name| -> anyhow:: Result < _ > {
46+ let docs_spec = docs. get ( doc_name) . ok_or ( anyhow:: anyhow!(
47+ "cannot find a document definition {doc_name}"
48+ ) ) ?;
49+ res. push ( docs_spec. doc_type . as_str ( ) . parse ( ) ?) ;
50+ Ok ( res)
51+ } ,
52+ ) ?;
53+
54+ let multiple = ref_spec. multiple . ok_or ( anyhow:: anyhow!(
55+ "'multiple' field should exists for the required 'ref' metadata definition"
56+ ) ) ?;
57+
58+ Ok ( Self :: Specified {
59+ exp_ref_types,
60+ multiple,
61+ optional,
62+ } )
63+ }
64+
2865 /// Field validation rule
2966 pub ( crate ) async fn check < Provider > (
3067 & self ,
@@ -71,43 +108,6 @@ impl RefRule {
71108
72109 Ok ( true )
73110 }
74-
75- /// Generating `RefRule` from specs
76- pub ( crate ) fn new (
77- docs : & HashMap < catalyst_signed_doc_spec:: DocumentName , catalyst_signed_doc_spec:: DocSpec > ,
78- ref_spec : & catalyst_signed_doc_spec:: metadata:: doc_ref:: Ref ,
79- ) -> anyhow:: Result < Self > {
80- let optional = match ref_spec. required {
81- catalyst_signed_doc_spec:: is_required:: IsRequired :: Yes => false ,
82- catalyst_signed_doc_spec:: is_required:: IsRequired :: Optional => true ,
83- catalyst_signed_doc_spec:: is_required:: IsRequired :: Excluded => {
84- return Ok ( Self :: NotSpecified ) ;
85- } ,
86- } ;
87-
88- anyhow:: ensure!( !ref_spec. doc_type. is_empty( ) , "'type' field should exists and has at least one entry for the required 'ref' metadata definition" ) ;
89-
90- let exp_ref_types = ref_spec. doc_type . iter ( ) . try_fold (
91- Vec :: new ( ) ,
92- |mut res, doc_name| -> anyhow:: Result < _ > {
93- let docs_spec = docs. get ( doc_name) . ok_or ( anyhow:: anyhow!(
94- "cannot find a document definition {doc_name}"
95- ) ) ?;
96- res. push ( docs_spec. doc_type . as_str ( ) . parse ( ) ?) ;
97- Ok ( res)
98- } ,
99- ) ?;
100-
101- let multiple = ref_spec. multiple . ok_or ( anyhow:: anyhow!(
102- "'multiple' field should exists for the required 'ref' metadata definition"
103- ) ) ?;
104-
105- Ok ( Self :: Specified {
106- exp_ref_types,
107- multiple,
108- optional,
109- } )
110- }
111111}
112112
113113/// Validate all the document references by the defined validation rules,
0 commit comments