mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
2c797fd41f
commit
7c348c3b1d
1 changed files with 62 additions and 22 deletions
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue