Skip to content

Commit 58491b9

Browse files
committed
refactor: new structure
1 parent 0bca859 commit 58491b9

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! `RefRule` generation
2+
3+
use proc_macro2::TokenStream;
4+
use quote::quote;
5+
6+
use crate::signed_doc_spec::{self, IsRequired};
7+
8+
/// Generating `RefRule` instantiation
9+
pub(crate) fn rule(
10+
content_type: &signed_doc_spec::content_type::ContentType
11+
) -> anyhow::Result<TokenStream> {
12+
if matches!(content_type.required, IsRequired::Excluded) {
13+
anyhow::ensure!(
14+
content_type.value.is_empty(),
15+
"'value' field must not exist when 'required' is 'excluded'"
16+
);
17+
18+
return Ok(quote! {
19+
crate::validator::rules::ContentTypeRule::Unspecified
20+
});
21+
}
22+
23+
if matches!(content_type.required, IsRequired::Yes) {
24+
anyhow::ensure!(!content_type.value.is_empty(), "'value' field must exist");
25+
}
26+
27+
let exp = match content_type.value.as_str() {
28+
"application/cbor" => quote! { ContentTypeRule::Cbor },
29+
"application/cddl" => quote! { ContentTypeRule::Cddl },
30+
"application/json" => quote! { ContentTypeRule::Json },
31+
"application/json+schema" => quote! { ContentTypeRule::JsonSchema },
32+
"text/css; charset=utf-8" => quote! { ContentTypeRule::Css },
33+
"text/css; charset=utf-8; template=handlebars" => {
34+
quote! { ContentTypeRule::CssHandlebars }
35+
},
36+
"text/html; charset=utf-8" => quote! { ContentTypeRule::Html },
37+
"text/html; charset=utf-8; template=handlebars" => {
38+
quote! { ContentTypeRule::HtmlHandlebars }
39+
},
40+
"text/markdown; charset=utf-8" => quote! { ContentTypeRule::Markdown },
41+
"text/markdown; charset=utf-8; template=handlebars" => {
42+
quote! { ContentTypeRule::MarkdownHandlebars }
43+
},
44+
"text/plain; charset=utf-8" => quote! { ContentTypeRule::Plain },
45+
"text/plain; charset=utf-8; template=handlebars" => {
46+
quote! { ContentTypeRule::PlainHandlebars }
47+
},
48+
_ => {
49+
return Err(anyhow::anyhow!(
50+
"Unsupported Content Type: {}",
51+
content_type.value
52+
))
53+
},
54+
};
55+
56+
Ok(quote! {
57+
crate::validator::rules::ContentTypeRule::Specified {
58+
exp: crate::validator::rules::#exp,
59+
}
60+
})
61+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! `catalyst_signed_documents_rules!` macro implementation
22
3+
pub(crate) mod content_type;
34
pub(crate) mod doc_ref;
45

56
use proc_macro2::TokenStream;
@@ -15,13 +16,14 @@ pub(crate) fn catalyst_signed_documents_rules_impl() -> anyhow::Result<TokenStre
1516
for (doc_name, doc_spec) in spec.docs {
1617
let const_type_name_ident = doc_name.ident();
1718

19+
let content_type_rule = content_type::rule(&doc_spec.headers.content_type)?;
1820
let ref_rule = doc_ref::ref_rule(&doc_spec.metadata.doc_ref)?;
1921
// TODO: implement a proper initialization for all specific validation rules
2022
let rules = quote! {
2123
crate::validator::rules::Rules {
2224
id: crate::validator::rules::IdRule,
2325
ver: crate::validator::rules::VerRule,
24-
content_type: crate::validator::rules::ContentTypeRule::NotSpecified,
26+
content_type: #content_type_rule,
2527
content_encoding: crate::validator::rules::ContentEncodingRule::Specified {
2628
exp: ContentEncoding::Brotli,
2729
optional: false,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! `signed_doc.json` headers content type field JSON definition
2+
3+
/// `signed_doc.json` "content type" field JSON object
4+
#[derive(serde::Deserialize)]
5+
#[allow(clippy::missing_docs_in_private_items)]
6+
pub(crate) struct ContentType {
7+
#[allow(dead_code)]
8+
pub(crate) required: super::IsRequired,
9+
pub(crate) value: String,
10+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Catalyst Signed Document spec type
22
3+
pub(crate) mod content_type;
34
pub(crate) mod doc_ref;
4-
pub(crate) mod field;
55

66
use std::{collections::HashMap, ops::Deref};
77

@@ -46,7 +46,7 @@ pub(crate) struct DocSpec {
4646
#[serde(rename = "type")]
4747
pub(crate) doc_type: String,
4848
/// `headers` field
49-
pub(crate) headers: field::Headers,
49+
pub(crate) headers: Headers,
5050
/// Document type metadata definitions
5151
pub(crate) metadata: Metadata,
5252
}
@@ -59,6 +59,14 @@ pub(crate) struct Metadata {
5959
pub(crate) doc_ref: doc_ref::Ref,
6060
}
6161

62+
/// Document's metadata fields definition
63+
#[derive(serde::Deserialize)]
64+
#[allow(clippy::missing_docs_in_private_items)]
65+
pub(crate) struct Headers {
66+
#[serde(rename = "content type")]
67+
pub(crate) content_type: content_type::ContentType,
68+
}
69+
6270
/// "required" field definition
6371
#[derive(serde::Deserialize)]
6472
#[serde(rename_all = "lowercase")]

0 commit comments

Comments
 (0)