1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{
4 Expr, Ident, Token, braced,
5 parse::{Parse, ParseStream},
6};
7
8struct ViewMacro {
9 layout: Option<Ident>,
10 modifiers: Vec<(Ident, Option<Expr>)>,
11 children: Vec<Expr>,
12}
13
14impl Parse for ViewMacro {
15 fn parse(input: ParseStream) -> syn::Result<Self> {
16 if input.peek(syn::token::Paren) || input.peek(syn::token::Bracket) {
18 return Err(syn::Error::new(input.span(), "unexpected delimiters"));
19 }
20
21 let layout = if input.peek(Ident)
23 && (input.peek2(syn::token::Brace) || input.peek2(syn::token::Paren))
24 {
25 let ident: Ident = input.parse()?;
26 Some(ident)
27 } else {
28 None
29 };
30
31 let modifiers = if input.peek(syn::token::Paren) {
33 let content;
34 syn::parenthesized!(content in input);
35 let mut mods = Vec::new();
36 while !content.is_empty() {
37 let name: Ident = content.parse()?;
38 let value = if content.peek(Token![:]) {
39 content.parse::<Token![:]>()?;
40 Some(content.parse::<Expr>()?)
41 } else {
42 None
43 };
44 mods.push((name, value));
45 if content.peek(Token![,]) {
46 content.parse::<Token![,]>()?;
47 } else {
48 break;
49 }
50 }
51 mods
52 } else {
53 Vec::new()
54 };
55
56 let children = if input.peek(syn::token::Brace) {
58 let content;
59 braced!(content in input);
60 let mut kids = Vec::new();
61 while !content.is_empty() {
62 let expr: Expr = content.parse()?;
63 kids.push(expr);
64 if content.peek(Token![,]) {
65 content.parse::<Token![,]>()?;
66 } else {
67 break;
68 }
69 }
70 kids
71 } else {
72 Vec::new()
73 };
74
75 Ok(Self {
76 layout,
77 modifiers,
78 children,
79 })
80 }
81}
82
83#[proc_macro]
108pub fn View(input: TokenStream) -> TokenStream {
109 let cloned = input.clone();
111 if let Ok(m) = syn::parse::<ViewMacro>(cloned) {
112 return expand_view(m).into();
113 }
114
115 if let Ok(expr) = syn::parse::<Expr>(input.clone()) {
117 return quote!(#expr).into();
118 }
119
120 quote!({}).into()
121}
122
123fn expand_view(m: ViewMacro) -> proc_macro2::TokenStream {
124 let ViewMacro {
125 layout,
126 modifiers,
127 children,
128 } = m;
129
130 if children.is_empty() && modifiers.is_empty() {
131 return quote!({});
132 }
133
134 let layout_name = layout.as_ref().map(|i| i.to_string()).unwrap_or_default();
135
136 let mod_calls = modifiers.iter().map(|(name, value)| {
137 if let Some(val) = value {
138 quote!(.#name(#val))
139 } else {
140 quote!(.#name())
141 }
142 });
143
144 if children.is_empty() {
145 if modifiers.is_empty() {
147 quote!({})
148 } else {
149 quote! {
150 repose_ui::#layout_name(repose_core::Modifier::new() #(#mod_calls)*)
151 }
152 }
153 } else if layout.is_some() {
154 let child_exprs = &children;
156 quote! {
157 repose_ui::#layout_name(repose_core::Modifier::new() #(#mod_calls)*)
158 .child((#(#child_exprs,)*))
159 }
160 } else {
161 let child_exprs = &children;
163 quote! {
164 repose_ui::Column(repose_core::Modifier::new()).child((#(#child_exprs,)*))
165 }
166 }
167}