wip: proc: view macro implementation
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-03 18:11:10 +03:00
parent 2c797fd41f
commit 7c348c3b1d

View file

@ -1,19 +1,26 @@
use proc_macro::TokenStream;
use proc_macro2::{TokenStream as TokenStream2};
use proc_macro2::{TokenStream as TokenStream2, TokenTree, Ident};
use syn::parse_macro_input;
use syn::{Expr, Attribute, Meta, MetaList, Path, PathSegment, PathArguments, ImplItem};
use syn::parse::{Parse, ParseStream, Result};
use syn::token::PathSep;
use syn::punctuated::Punctuated;
pub(crate) fn view_impl (meta: TokenStream, item: TokenStream) -> TokenStream {
let ViewMeta { output, define, attrs } = syn::parse_macro_input!(meta as ViewMeta);
let ViewItem { target, mapped, items } = syn::parse_macro_input!(item as ViewItem);
let ViewMeta { output, define, attrs, .. } = parse_macro_input!(meta as ViewMeta);
let ViewItem { target, mapped, items, .. } = parse_macro_input!(item as ViewItem);
quote::quote! {
#attrs
#(#attrs)*
impl #target {
#items
#(#items)*
}
/// Generated by [tengri_proc].
impl ::tengri::Content<#output> for #target {
fn content (&self) -> impl Render<#output> {
self.size.of(::tengri::View(self, #define))
}
}
/// Generated by [tengri_proc].
impl<'a> ::tengri::ViewContext<'a, #output> for #target {
fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, #output>> {
match value {
@ -34,31 +41,57 @@ pub(crate) fn view_impl (meta: TokenStream, item: TokenStream) -> TokenStream {
}
struct ViewMeta {
attrs: &'static str,
output: &'static str,
define: &'static str,
attrs: Vec<Attribute>,
output: Option<Ident>,
define: Expr,
}
impl syn::parse::Parse for ViewMeta {
fn parse (input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
Ok(Self {
attrs: "",
output: "",
define: "",
})
impl Parse for ViewMeta {
fn parse (input: ParseStream) -> Result<Self> {
let mut output = None;
let mut define = None;
let mut attrs = input.call(Attribute::parse_outer)?.into_iter().filter(|attr| {
if let Attribute { meta: Meta::List(MetaList { path, tokens, .. }), .. } = attr
&& path.segments.len() == 2
&& nth_segment_is(&path.segments, 0, "tengri")
&& nth_segment_is(&path.segments, 1, "view")
&& let Some(TokenTree::Group(group)) = tokens.clone().into_iter().next()
&& let Some(TokenTree::Ident(ident)) = group.stream().into_iter().next()
{
output = Some(ident);
return false
}
true
}).collect();
if let Some(define) = define {
Ok(Self { attrs, output, define })
} else {
Err(input.error("Missing view definition."))
}
}
}
fn nth_segment_is (segments: &Punctuated<PathSegment, PathSep>, n: usize, x: &str) -> bool {
if let Some(PathSegment { arguments: PathArguments::None, ident, .. }) = segments.get(n) {
if format!("{ident}") != x {
return true
}
}
return false
}
struct ViewItem {
items: &'static str,
items: Vec<ImplItem>,
target: &'static str,
mapped: &'static str,
}
impl syn::parse::Parse for ViewItem {
fn parse (input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
impl Parse for ViewItem {
fn parse (input: ParseStream) -> Result<Self> {
let items = vec![];
Ok(Self {
items: "",
items,
target: "",
mapped: "",
})
@ -67,12 +100,19 @@ impl syn::parse::Parse for ViewItem {
#[cfg(test)] #[test] fn test_view () {
let _: syn::ItemImpl = syn::parse_quote! {
use syn::{ItemImpl, parse_quote};
let _: ItemImpl = parse_quote! {
#[tengri::view(Tui)]
impl SomeView {
#[tengri::view(":view")]
#[tengri::view]
fn view (&self) -> impl Content<TuiOut> + use<'_> {
"view"
"view-1"
}
#[tengri::view(":view-1")]
fn view_1 (&self) -> impl Content<TuiOut> + use<'_> {
"view-1"
}
}
};