yew_macro/html_tree/
html_block.rs1use proc_macro2::Delimiter;
2use quote::{quote, quote_spanned, ToTokens};
3use syn::buffer::Cursor;
4use syn::parse::{Parse, ParseStream};
5use syn::{braced, token};
6
7use super::{HtmlIterable, HtmlNode, ToNodeIterator};
8use crate::PeekValue;
9
10pub struct HtmlBlock {
11 pub content: BlockContent,
12 brace: token::Brace,
13}
14
15pub enum BlockContent {
16 Node(Box<HtmlNode>),
17 Iterable(Box<HtmlIterable>),
18}
19
20impl PeekValue<()> for HtmlBlock {
21 fn peek(cursor: Cursor) -> Option<()> {
22 cursor.group(Delimiter::Brace).map(|_| ())
23 }
24}
25
26impl Parse for HtmlBlock {
27 fn parse(input: ParseStream) -> syn::Result<Self> {
28 let content;
29 let brace = braced!(content in input);
30 let content = if HtmlIterable::peek(content.cursor()).is_some() {
31 BlockContent::Iterable(Box::new(content.parse()?))
32 } else {
33 BlockContent::Node(Box::new(content.parse()?))
34 };
35
36 Ok(HtmlBlock { content, brace })
37 }
38}
39
40impl ToTokens for HtmlBlock {
41 fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
42 let HtmlBlock { content, .. } = self;
43 let new_tokens = match content {
44 BlockContent::Iterable(html_iterable) => quote! {#html_iterable},
45 BlockContent::Node(html_node) => quote! {#html_node},
46 };
47
48 tokens.extend(quote! {#new_tokens});
49 }
50}
51
52impl ToNodeIterator for HtmlBlock {
53 fn to_node_iterator_stream(&self) -> Option<proc_macro2::TokenStream> {
54 let HtmlBlock { content, brace } = self;
55 let new_tokens = match content {
56 BlockContent::Iterable(iterable) => iterable.to_node_iterator_stream(),
57 BlockContent::Node(node) => node.to_node_iterator_stream(),
58 }?;
59
60 Some(quote_spanned! {brace.span=> #new_tokens})
61 }
62}