@@ -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) ]
4242pub ( 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}
0 commit comments