Skip to content

Commit 59d865e

Browse files
committed
wip
1 parent 6f970cf commit 59d865e

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

rust/catalyst-signed-doc-macro/src/rules/doc_ref.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,32 @@ use crate::signed_doc_spec::{self, IsRequired};
77

88
/// Generating `RefRule` instantiation
99
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-
},
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
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! `signed_doc.json` "ref" field JSON defintion
22
3-
use crate::signed_doc_spec::{DocumentName, IsRequired};
3+
use crate::signed_doc_spec::{DocTypes, IsRequired};
44

55
/// `signed_doc.json` "ref" field JSON object
66
#[derive(serde::Deserialize)]
77
pub(crate) struct Ref {
88
pub(crate) required: IsRequired,
99
#[serde(rename = "type")]
10-
pub(crate) doc_type: Option<DocumentName>,
11-
// pub(crate) multiple: Option<bool>,
10+
pub(crate) doc_type: DocTypes,
11+
pub(crate) multiple: Option<bool>,
1212
}

rust/catalyst-signed-doc-macro/src/signed_doc_spec/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
pub(crate) mod doc_ref;
44

5-
use std::collections::HashMap;
5+
use std::{collections::HashMap, ops::Deref};
66

77
use proc_macro2::Ident;
88
use quote::format_ident;
@@ -65,6 +65,35 @@ pub(crate) enum IsRequired {
6565
Optional,
6666
}
6767

68+
pub(crate) struct DocTypes(Vec<DocumentName>);
69+
70+
impl Deref for DocTypes {
71+
type Target = Vec<DocumentName>;
72+
73+
fn deref(&self) -> &Self::Target {
74+
&self.0
75+
}
76+
}
77+
78+
impl<'de> serde::Deserialize<'de> for DocTypes {
79+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
80+
where D: serde::Deserializer<'de> {
81+
#[derive(serde::Deserialize)]
82+
#[serde(untagged)]
83+
enum SingleOrVec {
84+
Single(DocumentName),
85+
Multiple(Vec<DocumentName>),
86+
}
87+
let value = Option::<SingleOrVec>::deserialize(deserializer)?;
88+
let result = match value {
89+
Some(SingleOrVec::Single(item)) => vec![item],
90+
Some(SingleOrVec::Multiple(items)) => items,
91+
None => vec![],
92+
};
93+
Ok(Self(result))
94+
}
95+
}
96+
6897
impl CatalystSignedDocSpec {
6998
/// Loading a Catalyst Signed Documents spec from the `signed_doc.json`
7099
// #[allow(dependency_on_unit_never_type_fallback)]

0 commit comments

Comments
 (0)