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(ViewMeta { output }, ViewImpl { block, exposed }) = self; 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 [#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 #self_ty { fn content (&self) -> impl Render<#output> { #self_ty::view(self) } } /// Generated by [tengri_proc]. /// /// Gives [#self_ty] the ability to construct the [Render]able /// which might corresponds to a given [TokenStream], /// while taking [#self_ty]'s state into consideration. impl ::tengri::dsl::Namespace<#self_ty> for Box + '_> { fn take_from <'source> ( state: &#self_ty, words: &mut ::tengri::dsl::TokenIter<'source> ) -> Perhaps { 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)* None }, // Symbols are handled by user-provided functions // that take no parameters but `&self`. ::tengri::dsl::Value::Sym(sym) => match sym { #(#exposed)* _ => None }, _ => None } } else { None }) } } }) } } fn builtins_with_types () -> [TokenStream2;14] { [ quote! { When< Box + '_> > }, quote! { Either< Box + '_>, Box + '_>> }, quote! { Align< Box + '_> > }, quote! { Bsp< Box + '_>, Box + '_>> }, quote! { Fill< Box + '_> > }, quote! { Fixed<_, Box + '_> > }, quote! { Min<_, Box + '_> > }, quote! { Max<_, Box + '_> > }, quote! { Shrink<_, Box + '_> > }, quote! { Expand<_, Box + '_> > }, quote! { Push<_, Box + '_> > }, quote! { Pull<_, Box + '_> > }, quote! { Margin<_, Box + '_> > }, quote! { Padding<_, Box + '_> > }, ] }