panoramix_derive/lib.rs
1extern crate proc_macro;
2
3mod component;
4
5// ---
6
7use proc_macro::TokenStream;
8use syn::parse_macro_input;
9use tracing::trace;
10
11/// Attribute used to declare a component
12///
13/// The attribute applies to a function, but will transform that function into a type, with a `new()` method. By convention, the name of your component should be in UpperCamelCase, and the compiler will warn you if it isn't.
14///
15/// Example:
16///
17/// ```rust
18/// # use panoramix::{component, CompCtx, Element, NoEvent};
19/// # type EventType = NoEvent;
20/// # type PropsType = ();
21/// # type LocalState = ();
22/// #
23/// #[component]
24/// fn MyComponent(ctx: &CompCtx, props: PropsType) -> impl Element<EventType, LocalState> {
25/// // ...
26/// # panoramix::elements::EmptyElement::new()
27/// }
28/// ```
29#[proc_macro_attribute]
30pub fn component(attr: TokenStream, item: TokenStream) -> TokenStream {
31 trace!("attr: {:?}", attr);
32 trace!("item: {:?}", item);
33
34 let attr = proc_macro2::TokenStream::from(attr);
35 let fn_item = parse_macro_input!(item as syn::ItemFn);
36
37 trace!("fn_item: {:?}", fn_item);
38
39 proc_macro::TokenStream::from(component::component(attr, fn_item))
40}