diff --git a/proc/Cargo.toml b/proc/Cargo.toml index 2462993..aa2cd24 100644 --- a/proc/Cargo.toml +++ b/proc/Cargo.toml @@ -8,6 +8,6 @@ edition = { workspace = true } proc-macro = true [dependencies] -syn = { version = "2", features = ["full"] } +syn = { version = "2", features = ["full", "extra-traits"] } quote = { version = "1" } proc-macro2 = { version = "1", features = ["span-locations"] } diff --git a/proc/src/proc_view.rs b/proc/src/proc_view.rs index b9737d1..f28a801 100644 --- a/proc/src/proc_view.rs +++ b/proc/src/proc_view.rs @@ -1,23 +1,30 @@ use proc_macro::TokenStream; -use proc_macro2::{TokenStream as TokenStream2}; +use proc_macro2::{TokenStream as TokenStream2, TokenTree, Group, Ident}; +use syn::{parse_macro_input, Token}; +use syn::{Expr, Attribute, Meta, MetaList, Path, PathSegment, PathArguments, ImplItem}; +use syn::parse::{Parse, ParseStream, Result}; +use syn::token::{PathSep, Brace}; +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, attrs, .. } = parse_macro_input!(meta as ViewMeta); + let ViewImpl { target, mapped, items, .. } = parse_macro_input!(item as ViewImpl); 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)) + self.size.of(::tengri::View(self, self.config.view)) } } + /// Generated by [tengri_proc]. impl<'a> ::tengri::ViewContext<'a, #output> for #target { fn get_content_sym (&'a self, value: &Value<'a>) -> Option> { match value { - #mapped + #(#mapped),* _ => panic!("expected Sym(content), got: {value:?}") } //if let Value::Sym(s) = value { @@ -34,45 +41,121 @@ pub(crate) fn view_impl (meta: TokenStream, item: TokenStream) -> TokenStream { } struct ViewMeta { - attrs: &'static str, - output: &'static str, - define: &'static str, + output: Option, + attrs: Vec, } -impl syn::parse::Parse for ViewMeta { - fn parse (input: syn::parse::ParseStream) -> syn::parse::Result { +impl Parse for ViewMeta { + fn parse (input: ParseStream) -> Result { + let mut output = None; Ok(Self { - attrs: "", - output: "", - define: "", + 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::Ident(ident)) = tokens.clone().into_iter().next() + { + output = Some(ident); + return false + } + true + }).collect(), + output, }) } } -struct ViewItem { - items: &'static str, - target: &'static str, - mapped: &'static str, +fn nth_segment_is (segments: &Punctuated, n: usize, x: &str) -> bool { + if let Some(PathSegment { arguments: PathArguments::None, ident, .. }) = segments.get(n) { + if format!("{ident}") == x { + return true + } + } + return false } -impl syn::parse::Parse for ViewItem { - fn parse (input: syn::parse::ParseStream) -> syn::parse::Result { - Ok(Self { - items: "", - target: "", - mapped: "", - }) +struct ViewImpl { + target: Ident, + mapped: Vec, + items: Vec, +} + +impl Parse for ViewImpl { + fn parse (input: ParseStream) -> Result { + let _ = input.parse::()?; + let target = input.parse::()?; + let mut mapped = vec![]; + let mut items = vec![]; + let group = input.parse::()?.stream(); + Ok(Self { target, items, mapped, }) + } +} + +enum ViewItem { + Item(Ident), + Expr { + attrs: Vec, + value: Expr, + }, +} + +impl Parse for ViewItem { + fn parse (input: ParseStream) -> Result { + todo!() } } #[cfg(test)] #[test] fn test_view () { - let _: syn::ItemImpl = syn::parse_quote! { + use syn::{ItemImpl, parse_quote}; + + let x: ViewMeta = syn::parse_str("#[foo] #[bar]").unwrap(); + assert_eq!(x.output, None); + assert_eq!(x.attrs.len(), 2); + + let x: ViewMeta = syn::parse_str("#[foo] #[tengri::view(Tui)] #[bar]").unwrap(); + assert_eq!(x.output, Some(parse_quote! { Tui })); + assert_eq!(x.attrs.len(), 2); + + let x: ViewImpl = syn::parse_str(" + impl Foo { + #[view(\":foo\")] #[bar] 1 + #[baz] #[baz] fn baz () {} + } + ").unwrap(); + let expected_target: Ident = parse_quote! { Foo }; + assert_eq!(x.target, expected_target); + assert_eq!(x.items, vec![parse_quote! { #[baz] #[baz] fn baz () {} }]); + assert_eq!(x.mapped, vec![ + ViewItem::Expr { + attrs: vec![parse_quote! { #[view(":foo")] }, parse_quote! { #[bar] }], + value: parse_quote! { 1 } + }, + ViewItem::Item(parse_quote! { baz }) + ]); + + let x: proc_macro2::TokenStream = syn::parse_str(" + impl Foo { + #[foo] #[bar] 1 + + #[baz] #[baz] fn baz () {} + } + ").unwrap(); + + panic!("{x:?}"); + + let _: ItemImpl = parse_quote! { #[tengri::view(Tui)] impl SomeView { - #[tengri::view(":view")] + #[tengri::view] fn view (&self) -> impl Content + use<'_> { - "view" + "view-1" + } + + #[tengri::view(":view-1")] + fn view_1 (&self) -> impl Content + use<'_> { + "view-1" } } };