euv-macros 0.48.0

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 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 #ident #ty_generics #where_clause`.
            impl #impl_generics #ident #ty_generics #where_clause {
                /// Registers or replaces this value in `GlobalState`.
                ///
                /// # Returns
                ///
                /// - `::euv::Signal<Self>` - Root-lifetime reactive signal bound to `Self`.
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn provide(self) -> ::euv::Signal<Self> {
                    ::euv::GlobalState::provide(self)
                }

                /// Looks up the registered global `Signal<Self>` if present.
                ///
                /// # Returns
                ///
                /// - `::core::option::Option<::euv::Signal<Self>>` - Registered global signal if present.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn use_global() -> ::core::option::Option<::euv::Signal<Self>> {
                    ::euv::GlobalState::use_state::<Self>()
                }

                /// Reactively reads a clone of this global state if registered.
                ///
                /// # Returns
                ///
                /// - `::core::option::Option<Self>` - Current global state value if registered.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn get_global() -> ::core::option::Option<Self> {
                    ::euv::GlobalState::get::<Self>()
                }

                /// Replaces the registered global state value and notifies active subscribers.
                ///
                /// # Arguments
                ///
                /// - `Self` - Replacement global state value.
                ///
                /// # Returns
                ///
                /// - `bool` - `true` when updated.
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn set_global(value: Self) -> bool {
                    ::euv::GlobalState::set(value)
                }

                /// Mutates the registered global state value in place via `updater`.
                ///
                /// # Arguments
                ///
                /// - `__EuvStF` - In-place mutation closure.
                ///
                /// # Returns
                ///
                /// - `bool` - `true` when updated.
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn update_global<__EuvStF>(updater: __EuvStF) -> bool
                where
                    __EuvStF: ::core::ops::FnOnce(&mut Self),
                {
                    ::euv::GlobalState::update::<Self, __EuvStF>(updater)
                }

                /// Subscribes a scope-owned watcher receiving `(&prev, &next)` whenever this global state changes.
                ///
                /// # Arguments
                ///
                /// - `__EuvStW` - Change observer callback.
                ///
                /// # Returns
                ///
                /// - `::core::option::Option<::euv::Effect>` - Watcher effect handle if registered.
                #[must_use]
                #[cfg_attr(not(target_arch = "wasm32"), inline)]
                pub fn watch_global<__EuvStW>(
                    handler: __EuvStW,
                ) -> ::core::option::Option<::euv::Effect>
                where
                    __EuvStW: ::core::ops::FnMut(&Self, &Self) + 'static,
                {
                    ::euv::GlobalState::watch::<Self, __EuvStW>(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(expr) => {
            let span: Span = expr.span();
            quote_spanned! {span=>
                ::euv::LocalState::new(#expr)
            }
        }
        StateInput::GlobalLookup(ty) => {
            let span: Span = ty.span();
            quote_spanned! {span=>
                ::euv::GlobalState::use_state::<#ty>()
            }
        }
        StateInput::GlobalInit { ty, init } => {
            if let Expr::Call(call) = init.as_ref()
                && call.attrs.is_empty()
                && call.args.is_empty()
            {
                let func: &Expr = call.func.as_ref();
                quote! {
                    ::euv::GlobalState::use_or_init::<#ty, _>(#func)
                }
            } else {
                quote! {
                    ::euv::GlobalState::use_or_init::<#ty, _>(|| #init)
                }
            }
        }
    }
}