@@ -7,35 +7,32 @@ use crate::signed_doc_spec::{self, IsRequired};
77
88/// Generating `RefRule` instantiation
99pub ( crate ) fn ref_rule ( ref_spec : & signed_doc_spec:: doc_ref:: Ref ) -> anyhow:: Result < TokenStream > {
10- match ref_spec. required {
11- IsRequired :: Yes => {
12- let doc_name = ref_spec. doc_type . as_ref ( ) . ok_or ( anyhow:: anyhow!(
13- "'type' field should exists for the required 'ref' metadata definition"
14- ) ) ?;
15- let const_type_name_ident = doc_name. ident ( ) ;
16- Ok ( quote ! {
17- crate :: validator:: rules:: RefRule :: Specified {
18- exp_ref_types: vec![ #const_type_name_ident ]
19- optional: false ,
20- }
21- } )
22- } ,
23- IsRequired :: Optional => {
24- let doc_name = ref_spec. doc_type . as_ref ( ) . ok_or ( anyhow:: anyhow!(
25- "'type' field should exists for the required 'ref' metadata definition"
26- ) ) ?;
27- let const_type_name_ident = doc_name. ident ( ) ;
28- Ok ( quote ! {
29- crate :: validator:: rules:: RefRule :: Specified {
30- exp_ref_types: vec![ #const_type_name_ident ]
31- optional: true ,
32- }
33- } )
34- } ,
10+ let optional = match ref_spec. required {
11+ IsRequired :: Yes => true ,
12+ IsRequired :: Optional => false ,
3513 IsRequired :: Excluded => {
36- Ok ( quote ! {
14+ return Ok ( quote ! {
3715 crate :: validator:: rules:: RefRule :: NotSpecified
38- } )
16+ } ) ;
3917 } ,
40- }
18+ } ;
19+
20+ anyhow:: ensure!( !ref_spec. doc_type. is_empty( ) , "'type' field should exists and has at least one entry for the required 'ref' metadata definition" ) ;
21+
22+ let const_type_name_idents = ref_spec. doc_type . iter ( ) . map ( |doc_name| {
23+ let const_type_name_ident = doc_name. ident ( ) ;
24+ quote ! {
25+ crate :: doc_types:: #const_type_name_ident
26+ }
27+ } ) ;
28+ let multiple = ref_spec. multiple . ok_or ( anyhow:: anyhow!(
29+ "'multiple' field should exists for the required 'ref' metadata definition"
30+ ) ) ?;
31+ Ok ( quote ! {
32+ crate :: validator:: rules:: RefRule :: Specified {
33+ exp_ref_types: vec![ #( #const_type_name_idents, ) * ] ,
34+ multiple: #multiple,
35+ optional: #optional,
36+ }
37+ } )
4138}
0 commit comments