tengri/proc/src/proc_view.rs
unspeaker 7b67d29c01
Some checks are pending
/ build (push) Waiting to run
simplify
2025-07-19 08:42:08 +03:00

125 lines
4.4 KiB
Rust

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<String, Ident>,
}
impl Parse for ViewMeta {
fn parse (input: ParseStream) -> Result<Self> {
Ok(Self {
output: input.parse::<Ident>()?,
})
}
}
impl Parse for ViewImpl {
fn parse (input: ParseStream) -> Result<Self> {
let block = input.parse::<ItemImpl>()?;
let mut exposed: BTreeMap<String, Ident> = 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<dyn ::tengri::output::Render<#output> + 'state>
> for #self_ty {
fn try_dsl_into (&self, dsl: &impl ::tengri::dsl::Dsl)
-> Perhaps<Box<dyn ::tengri::output::Render<#output> + '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<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { _ })
}
fn _builtins_with_boxes () -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>> })
}
fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>> })
}
fn builtins_with (n: TokenStream2, c: TokenStream2) -> impl Iterator<Item=TokenStream2> {
[
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()
}