use crate::*; #[derive(Debug, Clone)] pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl); #[derive(Debug, Clone)] pub(crate) struct ViewMeta { pub(crate) output: Ident } #[derive(Debug, Clone)] pub(crate) struct ViewImpl { block: ItemImpl, exposed: BTreeMap, } impl Parse for ViewMeta { fn parse (input: ParseStream) -> Result { Ok(Self { output: input.parse::()?, }) } } impl Parse for ViewImpl { fn parse (input: ParseStream) -> Result { let block = input.parse::()?; let mut exposed: BTreeMap = Default::default(); for item in block.items.iter() { if let ImplItem::Fn(ImplItemFn { sig: Signature { ident, .. }, .. }) = item { let key = format!(":{}", AsKebabCase(format!("{}", &ident))); if exposed.contains_key(&key) { return Err(input.error(format!("already defined: {ident}"))); } exposed.insert(key, ident.clone()); } } Ok(Self { block, exposed }) } } impl ToTokens for ViewDef { fn to_tokens (&self, out: &mut TokenStream2) { let Self(_, ViewImpl { block, .. }) = self; let generated = self.generated(); write_quote_to(out, quote! { #block #generated }) } } impl ViewDef { fn generated (&self) -> impl ToTokens { let Self(ViewMeta { output }, ViewImpl { block, .. }) = self; let self_ty = &block.self_ty; let builtins = self.builtins(); let exposed = self.exposed(); quote! { /// Generated by [tengri_proc]. /// /// Makes [#self_ty] able to construct the [Render]able /// which might correspond to a given [TokenStream], /// while taking [#self_ty]'s state into consideration. impl<'state> ::tengri::dsl::DslInto< Box + 'state> > for #self_ty { fn try_dsl_into (&self, dsl: &impl ::tengri::dsl::Dsl) -> Perhaps + 'state>> { Ok(match dsl.val() { #builtins #exposed _ => return Ok(None) }) } } } } /// Expressions are handled by built-in functions /// that operate over constants and symbols. fn builtins (&self) -> impl ToTokens { let Self(ViewMeta { output }, ViewImpl { .. }) = self; let builtins = builtins_with_boxes_output(quote! { #output }).map(|builtin|quote! { ::tengri::dsl::Val::Exp(_, expr) => return Ok(Some( #builtin::from_dsl(self, expr, ||Box::new("failed to load builtin".into()))? .boxed() )), }); quote! { #(#builtins)* } } /// Symbols are handled by user-taked functions that take no parameters but `&self`. fn exposed (&self) -> impl ToTokens { let Self(ViewMeta { .. }, ViewImpl { exposed, .. }) = self; let exposed = exposed.iter().map(|(key, value)|write_quote(quote! { #key => return Ok(Some(self.#value().boxed())), })); quote! { ::tengri::dsl::Val::Sym(key) => match key.as_ref() { #(#exposed)* } } } } fn _builtins_with_holes () -> impl Iterator { builtins_with(quote! { _ }, quote! { _ }) } fn _builtins_with_boxes () -> impl Iterator { builtins_with(quote! { _ }, quote! { Box> }) } fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator { builtins_with(quote! { _ }, quote! { Box> }) } fn builtins_with (n: TokenStream2, c: TokenStream2) -> impl Iterator { [ quote! { When::< #c > }, quote! { Either::< #c, #c> }, quote! { Align::< #c > }, quote! { Bsp::< #c, #c> }, quote! { Fill::< #c > }, quote! { Fixed::<#n, #c > }, quote! { Min::<#n, #c > }, quote! { Max::<#n, #c > }, quote! { Shrink::<#n, #c > }, quote! { Expand::<#n, #c > }, quote! { Push::<#n, #c > }, quote! { Pull::<#n, #c > }, quote! { Margin::<#n, #c > }, quote! { Padding::<#n, #c > }, ].into_iter() }