mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
This commit is contained in:
parent
2c797fd41f
commit
55e7adfca1
2 changed files with 113 additions and 30 deletions
|
|
@ -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"] }
|
||||
|
|
|
|||
|
|
@ -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<RenderBox<'a, #output>> {
|
||||
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<Ident>,
|
||||
attrs: Vec<Attribute>,
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for ViewMeta {
|
||||
fn parse (input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
||||
impl Parse for ViewMeta {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
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<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
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for ViewItem {
|
||||
fn parse (input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
||||
Ok(Self {
|
||||
items: "",
|
||||
target: "",
|
||||
mapped: "",
|
||||
})
|
||||
struct ViewImpl {
|
||||
target: Ident,
|
||||
mapped: Vec<ViewItem>,
|
||||
items: Vec<ImplItem>,
|
||||
}
|
||||
|
||||
impl Parse for ViewImpl {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
let _ = input.parse::<Token![impl]>()?;
|
||||
let target = input.parse::<Ident>()?;
|
||||
let mut mapped = vec![];
|
||||
let mut items = vec![];
|
||||
let group = input.parse::<Group>()?.stream();
|
||||
Ok(Self { target, items, mapped, })
|
||||
}
|
||||
}
|
||||
|
||||
enum ViewItem {
|
||||
Item(Ident),
|
||||
Expr {
|
||||
attrs: Vec<Attribute>,
|
||||
value: Expr,
|
||||
},
|
||||
}
|
||||
|
||||
impl Parse for ViewItem {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
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<TuiOut> + use<'_> {
|
||||
"view"
|
||||
"view-1"
|
||||
}
|
||||
|
||||
#[tengri::view(":view-1")]
|
||||
fn view_1 (&self) -> impl Content<TuiOut> + use<'_> {
|
||||
"view-1"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue