11//! `ContentTypeRule` generation
22
3+ use std:: collections:: HashMap ;
4+
35use proc_macro2:: TokenStream ;
46use quote:: quote;
57
6- use crate :: signed_doc_spec:: { self , IsRequired } ;
8+ use crate :: signed_doc_spec:: { self , ContentTypeSpec , ContentTypeTemplate , IsRequired } ;
79
810/// Generating `ContentTypeRule` instantiation
911pub ( crate ) fn into_rule (
10- content_type : & signed_doc_spec:: content_type:: ContentType
12+ content_types : & HashMap < ContentTypeTemplate , ContentTypeSpec > ,
13+ field : & signed_doc_spec:: content_type:: ContentType ,
1114) -> anyhow:: Result < TokenStream > {
12- if matches ! ( content_type . required, IsRequired :: Excluded ) {
15+ if matches ! ( field . required, IsRequired :: Excluded ) {
1316 anyhow:: ensure!(
14- content_type . value. is_empty( ) ,
17+ field . value. is_empty( ) ,
1518 "'value' field must not exist when 'required' is 'excluded'"
1619 ) ;
1720
@@ -20,42 +23,19 @@ pub(crate) fn into_rule(
2023 } ) ;
2124 }
2225
23- if matches ! ( content_type . required, IsRequired :: Yes ) {
24- anyhow:: ensure!( !content_type . value. is_empty( ) , "'value' field must exist" ) ;
26+ if matches ! ( field . required, IsRequired :: Yes ) {
27+ anyhow:: ensure!( !field . value. is_empty( ) , "'value' field must exist" ) ;
2528 }
2629
27- let exp = match content_type. value . as_str ( ) {
28- "application/cbor" => quote ! { ContentType :: Cbor } ,
29- "application/cddl" => quote ! { ContentType :: Cddl } ,
30- "application/json" => quote ! { ContentType :: Json } ,
31- "application/json+schema" => quote ! { ContentType :: JsonSchema } ,
32- "text/css; charset=utf-8" => quote ! { ContentType :: Css } ,
33- "text/css; charset=utf-8; template=handlebars" => {
34- quote ! { ContentType :: CssHandlebars }
35- } ,
36- "text/html; charset=utf-8" => quote ! { ContentType :: Html } ,
37- "text/html; charset=utf-8; template=handlebars" => {
38- quote ! { ContentType :: HtmlHandlebars }
39- } ,
40- "text/markdown; charset=utf-8" => quote ! { ContentType :: Markdown } ,
41- "text/markdown; charset=utf-8; template=handlebars" => {
42- quote ! { ContentType :: MarkdownHandlebars }
43- } ,
44- "text/plain; charset=utf-8" => quote ! { ContentType :: Plain } ,
45- "text/plain; charset=utf-8; template=handlebars" => {
46- quote ! { ContentType :: PlainHandlebars }
47- } ,
48- _ => {
49- return Err ( anyhow:: anyhow!(
50- "Unsupported Content Type: {}" ,
51- content_type. value
52- ) )
53- } ,
30+ let template = ContentTypeTemplate ( field. value . clone ( ) ) ;
31+ let Some ( _) = content_types. get ( & template) else {
32+ return Err ( anyhow:: anyhow!( "Unsupported Content Type: {}" , field. value) ) ;
5433 } ;
34+ let ident = template. ident ( ) ;
5535
5636 Ok ( quote ! {
5737 crate :: validator:: rules:: ContentTypeRule :: Specified {
58- exp: #exp ,
38+ exp: ContentType :: #ident ,
5939 }
6040 } )
6141}
0 commit comments