From b74e8e5bc13056ce3bf9108f96a51e060751fe8e Mon Sep 17 00:00:00 2001 From: Jonathan Kingston Date: Sat, 24 Nov 2018 13:21:41 +0000 Subject: [PATCH] Handle bool attributes. Fixes #13 --- examples/rocket/src/main.rs | 3 +- macros/src/html.rs | 1 + typed-html/src/elements.rs | 68 ++++++++++++++++++------------------- typed-html/src/types/mod.rs | 18 ++++++++++ 4 files changed, 55 insertions(+), 35 deletions(-) diff --git a/examples/rocket/src/main.rs b/examples/rocket/src/main.rs index 97630d7..7b3c118 100644 --- a/examples/rocket/src/main.rs +++ b/examples/rocket/src/main.rs @@ -26,6 +26,7 @@ impl<'r> Responder<'r> for Html { #[get("/")] fn index() -> Html { + let a = false; Html(html!( @@ -44,7 +45,7 @@ fn index() -> Html { }) }

""

- + )) diff --git a/macros/src/html.rs b/macros/src/html.rs index 03ab10b..f506ad3 100644 --- a/macros/src/html.rs +++ b/macros/src/html.rs @@ -154,6 +154,7 @@ impl Element { .collect::, TokenStream>>()?; let mut body = TokenStream::new(); + for (attr_str, key, value) in attrs { match value { TokenTree::Literal(lit) if is_string_literal(lit) => { diff --git a/typed-html/src/elements.rs b/typed-html/src/elements.rs index e4a32da..e735f95 100644 --- a/typed-html/src/elements.rs +++ b/typed-html/src/elements.rs @@ -79,11 +79,11 @@ declare_elements!{ article in [FlowContent, SectioningContent] with FlowContent; aside in [FlowContent, SectioningContent] with FlowContent; audio { - autoplay: bool, - controls: bool, + autoplay: Bool, + controls: Bool, crossorigin: CrossOrigin, - loop: bool, - muted: bool, + loop: Bool, + muted: Bool, preload: Preload, src: Uri, } in [FlowContent, PhrasingContent, EmbeddedContent] with MediaContent; @@ -95,13 +95,13 @@ declare_elements!{ } in [FlowContent] with FlowContent; br in [FlowContent, PhrasingContent]; button { - autofocus: bool, - disabled: bool, + autofocus: Bool, + disabled: Bool, form: Id, formaction: Uri, formenctype: FormEncodingType, formmethod: FormMethod, - formnovalidate: bool, + formnovalidate: Bool, formtarget: Target, name: Id, type: ButtonType, @@ -122,7 +122,7 @@ declare_elements!{ datetime: Datetime, } in [FlowContent, PhrasingContent] with FlowContent; details { - open: bool, + open: Bool, } in [FlowContent, SectioningContent, InteractiveContent] with [summary] FlowContent; dfn in [FlowContent, PhrasingContent] with PhrasingContent; div in [FlowContent] with FlowContent; @@ -146,7 +146,7 @@ declare_elements!{ enctype: FormEncodingType, method: FormMethod, name: Id, - novalidate: bool, + novalidate: Bool, target: Target, } in [FlowContent] with FlowContent; h1 in [FlowContent, HeadingContent, HGroupContent] with PhrasingContent; @@ -161,8 +161,8 @@ declare_elements!{ i in [FlowContent, PhrasingContent] with PhrasingContent; iframe { allow: FeaturePolicy, - allowfullscreen: bool, - allowpaymentrequest: bool, + allowfullscreen: Bool, + allowpaymentrequest: Bool, height: usize, name: Id, referrerpolicy: ReferrerPolicy, @@ -176,7 +176,7 @@ declare_elements!{ crossorigin: CrossOrigin, decoding: ImageDecoding, height: usize, - ismap: bool, + ismap: Bool, sizes: SpacedList, // FIXME it's not really just a string src: Uri, srcset: String, // FIXME this is much more complicated @@ -185,12 +185,12 @@ declare_elements!{ } in [FlowContent, PhrasingContent, EmbeddedContent]; input { autocomplete: String, - autofocus: bool, - disabled: bool, + autofocus: Bool, + disabled: Bool, form: Id, list: Id, name: Id, - required: bool, + required: Bool, tabindex: usize, type: InputType, value: String, @@ -227,12 +227,12 @@ declare_elements!{ height: usize, name: Id, type: Mime, - typemustmatch: bool, + typemustmatch: Bool, usemap: String, // TODO should be a fragment starting with '#' width: usize, } in [FlowContent, PhrasingContent, EmbeddedContent, InteractiveContent, FormContent] with param; ol { - reversed: bool, + reversed: Bool, start: isize, type: OrderedListType, } in [FlowContent] with li; @@ -254,11 +254,11 @@ declare_elements!{ s in [FlowContent, PhrasingContent] with PhrasingContent; samp in [FlowContent, PhrasingContent] with PhrasingContent; script { - async: bool, + async: Bool, crossorigin: CrossOrigin, - defer: bool, + defer: Bool, integrity: Integrity, - nomodule: bool, + nomodule: Bool, nonce: Nonce, src: Uri, text: String, @@ -267,12 +267,12 @@ declare_elements!{ section in [FlowContent, SectioningContent] with FlowContent; select { autocomplete: String, - autofocus: bool, - disabled: bool, + autofocus: Bool, + disabled: Bool, form: Id, - multiple: bool, + multiple: Bool, name: Id, - required: bool, + required: Bool, size: usize, } in [FlowContent, PhrasingContent, InteractiveContent, FormContent] with SelectContent; small in [FlowContent, PhrasingContent] with PhrasingContent; @@ -284,16 +284,16 @@ declare_elements!{ template in [MetadataContent, FlowContent, PhrasingContent, TableColumnContent] with Node; textarea { autocomplete: OnOff, - autofocus: bool, + autofocus: Bool, cols: usize, - disabled: bool, + disabled: Bool, form: Id, maxlength: usize, minlength: usize, name: Id, placeholder: String, - readonly: bool, - required: bool, + readonly: Bool, + required: Bool, rows: usize, spellcheck: BoolOrDefault, wrap: Wrap, @@ -310,7 +310,7 @@ declare_elements!{ area { alt: String, coords: String, // TODO could perhaps be validated - download: bool, + download: Bool, href: Uri, hreflang: LanguageTag, ping: SpacedList, @@ -333,13 +333,13 @@ declare_elements!{ value: isize, } with FlowContent; option { - disabled: bool, + disabled: Bool, label: String, - selected: bool, + selected: Bool, value: String, } in [SelectContent] with TextNode; optgroup { - disabled: bool, + disabled: Bool, label: String, } in [SelectContent] with option; param { @@ -368,7 +368,7 @@ declare_elements!{ thead in [TableContent] with tr; tr in [TableContent] with TableColumnContent; track { - default: bool, + default: Bool, kind: VideoKind, label: String, src: Uri, @@ -386,7 +386,7 @@ declare_elements!{ loop: isize, scrollamount: usize, scrolldelay: usize, - truespeed: bool, + truespeed: Bool, vspace: String, // FIXME size width: String, // FIXME size } in [FlowContent, PhrasingContent] with PhrasingContent; diff --git a/typed-html/src/types/mod.rs b/typed-html/src/types/mod.rs index a1d157c..32cf0ea 100644 --- a/typed-html/src/types/mod.rs +++ b/typed-html/src/types/mod.rs @@ -58,6 +58,24 @@ pub enum ButtonType { Button, } +#[derive(EnumString, Display, PartialEq, Eq, PartialOrd, Ord, AsRefStr, AsStaticStr)] +pub enum Bool { + #[strum(to_string = "true")] + True, + #[strum(to_string = "")] + False, +} + +impl From for Bool { + fn from(v: bool) -> Self { + if (v) { + Bool::True + } else { + Bool::False + } + } +} + #[derive(EnumString, Display, PartialEq, Eq, PartialOrd, Ord, AsRefStr, AsStaticStr)] pub enum CrossOrigin { #[strum(to_string = "anonymous")]