Skip to content

Commit f9640c4

Browse files
committed
feat: full implementation
1 parent daa60cb commit f9640c4

File tree

5 files changed

+42
-46
lines changed

5 files changed

+42
-46
lines changed

rust/catalyst-signed-doc-macro/src/field.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,50 @@ impl TryInto<TokenStream> for IsRequired {
3131
/// Document's metadata fields definition
3232
#[derive(serde::Deserialize)]
3333
#[allow(clippy::missing_docs_in_private_items)]
34-
pub(crate) struct MetadataNode {
35-
#[serde(rename = "ref")]
34+
pub(crate) struct Metadata {
35+
#[serde(rename = "content type")]
3636
pub(crate) content_type: ContentType,
3737
}
3838

3939
/// `signed_doc.json` "ref" field JSON object
4040
#[derive(serde::Deserialize)]
4141
#[allow(clippy::missing_docs_in_private_items)]
4242
pub(crate) struct ContentType {
43+
#[allow(dead_code)]
4344
pub(crate) required: IsRequired,
44-
pub(crate) value: Vec<String>,
45+
pub(crate) value: String,
46+
}
47+
48+
impl TryInto<TokenStream> for ContentType {
49+
type Error = anyhow::Error;
50+
51+
fn try_into(self) -> Result<TokenStream, Self::Error> {
52+
let exp = match self.value.as_str() {
53+
"application/cbor" => quote! { ContentType::Cbor },
54+
"application/cddl" => quote! { ContentType::Cddl },
55+
"application/json" => quote! { ContentType::Json },
56+
"application/json+schema" => quote! { ContentType::JsonSchema },
57+
"text/css; charset=utf-8" => quote! { ContentType::Css },
58+
"text/css; charset=utf-8; template=handlebars" => quote! { ContentType::CssHandlebars },
59+
"text/html; charset=utf-8" => quote! { ContentType::Html },
60+
"text/html; charset=utf-8; template=handlebars" => {
61+
quote! { ContentType::HtmlHandlebars }
62+
},
63+
"text/markdown; charset=utf-8" => quote! { ContentType::Markdown },
64+
"text/markdown; charset=utf-8; template=handlebars" => {
65+
quote! { ContentType::MarkdownHandlebars }
66+
},
67+
"text/plain; charset=utf-8" => quote! { ContentType::Plain },
68+
"text/plain; charset=utf-8; template=handlebars" => {
69+
quote! { ContentType::PlainHandlebars }
70+
},
71+
_ => return Err(anyhow::anyhow!("Unsupported Content Type: {}", self.value)),
72+
};
73+
74+
Ok(quote! {
75+
crate::validator::rules::ContentTypeRule {
76+
exp: #exp,
77+
}
78+
})
79+
}
4580
}

rust/catalyst-signed-doc-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! spec.
33
44
mod error;
5-
mod rules;
65
mod field;
6+
mod rules;
77
mod signed_doc_spec;
88
mod types_consts;
99

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! `catalyst_signed_documents_rules!` macro implementation
22
3-
pub(crate) mod content_type;
4-
53
use proc_macro2::TokenStream;
64
use quote::quote;
75

@@ -15,16 +13,14 @@ pub(crate) fn catalyst_signed_documents_rules_impl() -> anyhow::Result<TokenStre
1513
for (doc_name, doc_spec) in spec.docs {
1614
let const_type_name_ident = doc_name.ident();
1715

18-
let content_type_rule = content_type::ref_rule(&doc_spec.metadata.content_type)?;
16+
let content_type_rule: TokenStream = doc_spec.metadata.content_type.try_into()?;
1917

2018
// TODO: implement a proper initialization for all specific validation rules
2119
let rules = quote! {
2220
crate::validator::rules::Rules {
2321
id: crate::validator::rules::IdRule,
2422
ver: crate::validator::rules::VerRule,
25-
content_type: crate::validator::rules::ContentTypeRule {
26-
exp: ContentType::Json,
27-
},
23+
content_type: #content_type_rule,
2824
content_encoding: crate::validator::rules::ContentEncodingRule {
2925
exp: ContentEncoding::Brotli,
3026
optional: false,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(crate) struct DocSpec {
4646
#[serde(rename = "type")]
4747
pub(crate) doc_type: String,
4848
/// Document type metadata definitions
49-
pub(crate) metadata: field::MetadataNode,
49+
pub(crate) metadata: field::Metadata,
5050
}
5151

5252
impl CatalystSignedDocSpec {

0 commit comments

Comments
 (0)