proc, view: fix usage of builtins

This commit is contained in:
🪞👃🪞 2025-05-21 13:57:03 +03:00
parent 776cea6f1b
commit 7516517078
4 changed files with 83 additions and 78 deletions

View file

@ -42,50 +42,53 @@ impl Parse for ViewImpl {
impl ToTokens for ViewDef {
fn to_tokens (&self, out: &mut TokenStream2) {
let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self;
let view = &block.self_ty;
let builtins = builtins().iter().map(|ty|write_quote(quote! {
if let Some(value) = Namespace::<#ty>::take_from(state, &mut exp.clone())? {
return Ok(Some(value.boxed()))
}
})).collect::<Vec<_>>();
let mut available = vec![];
let exposed: Vec<_> = exposed.iter().map(|(key, value)|{
available.push(key.clone());
write_quote(quote! { #key => Some(#view::#value(state).boxed()), })
}).collect();
let available: String = available.join(", ");
let error_msg = LitStr::new(
&format!("expected Sym(content), got: {{token:?}}, available: {available}"),
Span::call_site()
);
let self_ty = &block.self_ty;
let builtins: Vec<_> = builtins_with_types()
.iter()
.map(|ty|write_quote(quote! {
let value: Option<#ty> = Dsl::take(state, &mut exp.clone())?;
if let Some(value) = value {
return Ok(Some(value.boxed()))
}
}))
.collect();
let exposed: Vec<_> = exposed
.iter()
.map(|(key, value)|write_quote(quote! {
#key => Some(#self_ty::#value(state).boxed()),
})).collect();
write_quote_to(out, quote! {
#block
/// Generated by [tengri_proc].
///
/// Delegates the rendering of [#view] to the [#view::view} method,
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
/// which you will need to implement, e.g. passing a [TokenIter]
/// containing a layout and keybindings config from user dirs.
impl ::tengri::output::Content<#output> for #view {
impl ::tengri::output::Content<#output> for #self_ty {
fn content (&self) -> impl Render<#output> {
self.view()
#self_ty::view(self)
}
}
/// Generated by [tengri_proc].
///
/// Gives [#view] the ability to construct the [Render]able
/// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream],
/// while taking [#view]'s state into consideration.
impl ::tengri::dsl::Namespace<#view> for Box<dyn Render<#output>> {
/// while taking [#self_ty]'s state into consideration.
impl ::tengri::dsl::Namespace<#self_ty> for Box<dyn Render<#output> + '_> {
fn take_from <'source> (
state: &#view,
state: &#self_ty,
words: &mut ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
match value {
// Expressions are handled by built-in functions
// that operate over constants and symbols.
::tengri::dsl::Value::Exp(_, exp) => {
//#(#builtins)*
#(#builtins)*
None
},
// Symbols are handled by user-provided functions
// that take no parameters but `&self`.
::tengri::dsl::Value::Sym(sym) => match sym {
#(#exposed)*
_ => None
@ -101,21 +104,21 @@ impl ToTokens for ViewDef {
}
}
fn builtins () -> [TokenStream2;14] {
fn builtins_with_types () -> [TokenStream2;14] {
[
quote! { When::<_> },
quote! { Either::<_, _> },
quote! { Align::<_> },
quote! { Bsp::<_, _> },
quote! { Fill::<_> },
quote! { Fixed::<_, _> },
quote! { Min::<_, _> },
quote! { Max::<_, _> },
quote! { Shrink::<_, _> },
quote! { Expand::<_, _> },
quote! { Push::<_, _> },
quote! { Pull::<_, _> },
quote! { Margin::<_, _> },
quote! { Padding::<_, _> },
quote! { When< Box<dyn Render<_> + '_> > },
quote! { Either< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
quote! { Align< Box<dyn Render<_> + '_> > },
quote! { Bsp< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
quote! { Fill< Box<dyn Render<_> + '_> > },
quote! { Fixed<_, Box<dyn Render<_> + '_> > },
quote! { Min<_, Box<dyn Render<_> + '_> > },
quote! { Max<_, Box<dyn Render<_> + '_> > },
quote! { Shrink<_, Box<dyn Render<_> + '_> > },
quote! { Expand<_, Box<dyn Render<_> + '_> > },
quote! { Push<_, Box<dyn Render<_> + '_> > },
quote! { Pull<_, Box<dyn Render<_> + '_> > },
quote! { Margin<_, Box<dyn Render<_> + '_> > },
quote! { Padding<_, Box<dyn Render<_> + '_> > },
]
}