File tree Expand file tree Collapse file tree 3 files changed +58
-10
lines changed
rust/catalyst-signed-doc-macro/src Expand file tree Collapse file tree 3 files changed +58
-10
lines changed Original file line number Diff line number Diff line change 33use proc_macro2:: TokenStream ;
44use quote:: quote;
55
6- /// `signed_doc.json` "ref" field JSON defintion
7- #[ derive( serde:: Deserialize ) ]
8- pub ( crate ) struct Ref { }
6+ use crate :: signed_doc_spec:: { self , IsRequired } ;
97
108/// Generating `RefRule` instantiation
11- pub ( crate ) fn ref_rule ( _ref_spec : & Ref ) -> anyhow:: Result < TokenStream > {
12- let res = quote ! {
13- crate :: validator:: rules:: RefRule :: NotSpecified
14- } ;
15- Ok ( res)
9+ pub ( 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+ } ,
35+ IsRequired :: Excluded => {
36+ Ok ( quote ! {
37+ crate :: validator:: rules:: RefRule :: NotSpecified
38+ } )
39+ } ,
40+ }
1641}
Original file line number Diff line number Diff line change 1+ //! `signed_doc.json` "ref" field JSON defintion
2+
3+ use crate :: signed_doc_spec:: { DocumentName , IsRequired } ;
4+
5+ /// `signed_doc.json` "ref" field JSON object
6+ #[ derive( serde:: Deserialize ) ]
7+ pub ( crate ) struct Ref {
8+ pub ( crate ) required : IsRequired ,
9+ #[ serde( rename = "type" ) ]
10+ pub ( crate ) doc_type : Option < DocumentName > ,
11+ // pub(crate) multiple: Option<bool>,
12+ }
Original file line number Diff line number Diff line change 11//! Catalyst Signed Document spec type
22
3+ pub ( crate ) mod doc_ref;
4+
35use std:: collections:: HashMap ;
46
57use proc_macro2:: Ident ;
@@ -51,14 +53,23 @@ pub(crate) struct DocSpec {
5153#[ derive( serde:: Deserialize ) ]
5254pub ( crate ) struct Metadata {
5355 #[ serde( rename = "ref" ) ]
54- pub ( crate ) doc_ref : crate :: rules:: doc_ref:: Ref ,
56+ pub ( crate ) doc_ref : doc_ref:: Ref ,
57+ }
58+
59+ /// "required" field defition
60+ #[ derive( serde:: Deserialize ) ]
61+ #[ serde( rename_all = "lowercase" ) ]
62+ pub ( crate ) enum IsRequired {
63+ Yes ,
64+ Excluded ,
65+ Optional ,
5566}
5667
5768impl CatalystSignedDocSpec {
5869 /// Loading a Catalyst Signed Documents spec from the `signed_doc.json`
5970 // #[allow(dependency_on_unit_never_type_fallback)]
6071 pub ( crate ) fn load_signed_doc_spec ( ) -> anyhow:: Result < CatalystSignedDocSpec > {
61- let signed_doc_str = include_str ! ( "../../../specs/signed_doc.json" ) ;
72+ let signed_doc_str = include_str ! ( "../../../../ specs/signed_doc.json" ) ;
6273 let signed_doc_spec = serde_json:: from_str ( signed_doc_str) ?;
6374 Ok ( signed_doc_spec)
6475 }
You can’t perform that action at this time.
0 commit comments