euv-macros 0.52.1

Zero-overhead procedural macros for the cross-platform euv UI framework, providing view!, style!, var!, watch!, computed!, state!, #[store], #[component], #[event], emit!, on!, and #[euv].
Documentation
use super::*;

/// Expands `#[store]` on a struct, generating ergonomic global singleton registration, read, write, and watch helpers.
///
/// # Arguments
///
/// - `ItemStruct` - Annotated global state store struct.
///
/// # Returns
///
/// - `proc_macro2::TokenStream` - Original struct plus global state methods.
pub(crate) fn expand_store_attr(item: ItemStruct) -> proc_macro2::TokenStream {
    let struct_ident: &Ident = &item.ident;
    let (impl_generics, ty_generics, where_clause): (
        ImplGenerics<'_>,
        TypeGenerics<'_>,
        Option<&WhereClause>,
    ) = item.generics.split_for_impl();
    let has_clone: bool = struct_has_derive(&item.attrs, STATE_DERIVE_CLONE);
    let extra_derive: proc_macro2::TokenStream = if has_clone {
        quote! {}
    } else {
        quote! { #[derive(Clone)] }
    };
    quote! {
            #extra_derive
            #item
    /// Implements `impl #impl_generics #struct_ident #ty_generics #where_clause`.
            impl #impl_generics #struct_ident #ty_generics #where_clause {
    /// Implements the `provide` operation on [`Self`].
    ///
    /// # Returns
    ///
    /// - `::euv::Signal<Self>` - A `::euv::Signal<Self>` value.
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn provide(self) -> ::euv::Signal<Self> {
                    ::euv::GlobalState::provide(self)
                }
    /// Implements the `use_global` operation on [`Self`].
    ///
    /// # Returns
    ///
    /// - `::core::option::Option<::euv::Signal<Self>>` - A `::core::option::Option<::euv::Signal<Self>>` value.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn use_global() -> ::core::option::Option<::euv::Signal<Self>> {
                    ::euv::GlobalState::use_state::<Self>()
                }
    /// Implements the `get_global` operation on [`Self`].
    ///
    /// # Returns
    ///
    /// - `::core::option::Option<Self>` - A `::core::option::Option<Self>` value.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn get_global() -> ::core::option::Option<Self> {
                    ::euv::GlobalState::get::<Self>()
                }
    /// Implements the `set_global` operation on [`Self`].
    ///
    /// # Arguments
    ///
    /// - `Self` - A `Self` parameter.
    ///
    /// # Returns
    ///
    /// - `bool` - A boolean.
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn set_global(value: Self) -> bool {
                    ::euv::GlobalState::set(value)
                }
    /// Implements the `watch_global` operation on [`Self`].
    ///
    /// # Arguments
    ///
    /// - `StoreWatcherFn` - A `StoreWatcherFn` parameter.
    ///
    /// # Returns
    ///
    /// - `::core::option::Option<::euv::Effect>` - A `::core::option::Option<::euv::Effect>` value.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn watch_global<StoreWatcherFn>(
                    handler: StoreWatcherFn,
                ) -> ::core::option::Option<::euv::Effect>
                where
                    StoreWatcherFn: ::core::ops::FnMut(&Self, &Self) + 'static,
                {
                    ::euv::GlobalState::watch::<Self, StoreWatcherFn>(handler)
                }
            }
        }
}

/// Expands unified `state!(...)` into `LocalState::new(...)` or `GlobalState::use_state` / `use_or_init`.
///
/// # Arguments
///
/// - `StateInput` - Parsed unified state form.
///
/// # Returns
///
/// - `proc_macro2::TokenStream` - Expanded reactive state expression.
pub(crate) fn expand_state(input: StateInput) -> proc_macro2::TokenStream {
    match input {
        StateInput::Local(init_expr) => {
            let expr_span: Span = init_expr.span();
            quote_spanned! {expr_span=>
                ::euv::LocalState::new(#init_expr)
            }
        }
        StateInput::GlobalLookup(store_type) => {
            let type_span: Span = store_type.span();
            quote_spanned! {type_span=>
                ::euv::GlobalState::use_state::<#store_type>()
            }
        }
        StateInput::GlobalInit {
            ty: store_type,
            init: init_expr,
        } => {
            if let Expr::Call(call_expr) = init_expr.as_ref()
                && call_expr.attrs.is_empty()
                && call_expr.args.is_empty()
            {
                let constructor_fn: &Expr = call_expr.func.as_ref();
                quote! {
                    ::euv::GlobalState::use_or_init::<#store_type, _>(#constructor_fn)
                }
            } else {
                quote! {
                    ::euv::GlobalState::use_or_init::<#store_type, _>(|| #init_expr)
                }
            }
        }
    }
}