WIP: Macro not working fully yet

This commit is contained in:
Nathan Sobo 2023-10-26 13:18:58 +02:00
parent 0285284ae1
commit f4cff69729
2 changed files with 25 additions and 13 deletions

View file

@ -1,9 +1,21 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
use syn::{parse_macro_input, DeriveInput}; use syn::{parse_macro_input, parse_quote, DeriveInput};
pub fn derive_component(input: TokenStream) -> TokenStream { pub fn derive_component(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput); let mut ast = parse_macro_input!(input as DeriveInput);
if !ast
.generics
.params
.iter()
.any(|param| matches!(param, syn::GenericParam::Type(_)))
{
ast.generics.params.push(parse_quote! {
V: 'static
});
}
let name = &ast.ident; let name = &ast.ident;
let generics = &ast.generics; let generics = &ast.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
@ -37,7 +49,7 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
if let Some(syn::GenericParam::Type(type_param)) = generics.params.first() { if let Some(syn::GenericParam::Type(type_param)) = generics.params.first() {
type_param.ident.clone() type_param.ident.clone()
} else { } else {
panic!("Expected first type parameter"); panic!("Expected first type parameter to be a view type");
} }
}); });
@ -50,5 +62,9 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
} }
}; };
if name == "AssistantPanelStory" {
println!("Expanded tokens: {}", expanded.to_string());
}
TokenStream::from(expanded) TokenStream::from(expanded)
} }

View file

@ -69,7 +69,7 @@ impl<S: 'static + Send + Sync> AssistantPanel<S> {
.overflow_y_scroll() .overflow_y_scroll()
.child(Label::new("Is this thing on?")), .child(Label::new("Is this thing on?")),
) )
.render()]) .renderinto_any()])
.side(self.current_side) .side(self.current_side)
.width(AbsoluteLength::Rems(rems(32.))) .width(AbsoluteLength::Rems(rems(32.)))
} }
@ -85,20 +85,16 @@ mod stories {
use super::*; use super::*;
#[derive(Component)] #[derive(Component)]
pub struct AssistantPanelStory<S: 'static + Send + Sync> { pub struct AssistantPanelStory {}
state_type: PhantomData<S>,
}
impl<S: 'static + Send + Sync> AssistantPanelStory<S> { impl AssistantPanelStory {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {}
state_type: PhantomData,
}
} }
fn render(self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Component<S> { fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, AssistantPanel<S>>(cx)) .child(Story::title_for::<_, AssistantPanel<V>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))
.child(AssistantPanel::new("assistant-panel")) .child(AssistantPanel::new("assistant-panel"))
} }