euv-macros 0.46.1

Zero-overhead procedural macros for the cross-platform euv UI framework, providing view!, style!, design_tokens!, watch!, computed!, state!, #[state_store], #[component], #[event], and #[euv].
Documentation
//! euv-macros
//!
//! Zero-overhead native procedural macros for the cross-platform euv UI framework,
//! compiling declarative `view!`, `style!`, `design_tokens!`, `watch!`, `computed!`,
//! `state!`, `#[state_store]`, and `#[euv]` directly into arena scene graph operations
//! with span-preserving IDE completion and actionable compile-time diagnostics.

mod component;
mod diagnostic;
mod entry;
mod event;
mod props;
mod reactive;
mod state;
mod style;
mod view;

pub(crate) use {
    component::*, diagnostic::*, entry::*, event::*, props::*, reactive::*, state::*, style::*,
    view::*,
};

use std::{collections::HashSet, mem::take};

use {
    proc_macro::TokenStream,
    proc_macro2::Span,
    quote::{quote, quote_spanned},
    syn::{
        Attribute, Data, DeriveInput, Error, Expr, ExprRange, Field, Fields, FnArg, Ident,
        ImplGenerics, ImplItem, ImplItemFn, ItemFn, ItemImpl, ItemStruct, LitBool, LitInt, LitStr,
        Meta, Pat, Path, PathArguments, PathSegment, RangeLimits, Result, ReturnType, Stmt, Token,
        Type, TypeGenerics, Visibility, WhereClause, braced, parenthesized,
        parse::{Parse, ParseBuffer, ParseStream},
        parse_macro_input,
        punctuated::Punctuated,
        spanned::Spanned,
        token::{Brace, Paren},
    },
};

/// Declarative zero-VDOM, zero-context UI construction macro managing `Context` automatically.
///
/// Component parameters must be passed as self-documenting named attributes inside `Component { prop: value }` blocks; positional `Component(...)` calls are rejected at compile time:
///
/// ```compile_fail
/// use euv::*;
///
/// #[derive(Props)]
/// struct DemoBtn {
///     label: String,
/// }
///
/// #[component]
/// impl DemoBtn {
///     fn render(&self) -> NodeId {
///         view! { text { "ok" } }
///     }
/// }
///
/// fn invalid() -> NodeId {
///     view! {
///         DemoBtn("positional")
///     }
/// }
/// ```
///
/// All styling on primitive `view!` elements must live inside `style: { ... }`, `style: style! { ... }`, or `style: expr`; loose top-level style properties are rejected at compile time:
///
/// ```compile_fail
/// use euv::*;
///
/// fn invalid_loose_style() -> NodeId {
///     view! {
///         column {
///             width: Length::Px(100.0)
///         }
///     }
/// }
/// ```
///
/// # Arguments
///
/// - `TokenStream` - Invocation tokens `view! { ... }`.
///
/// # Returns
///
/// - `TokenStream` - Expanded arena node construction and reactive binding tokens.
#[proc_macro]
pub fn view(input: TokenStream) -> TokenStream {
    let parsed: ViewMacroInput = parse_macro_input!(input as ViewMacroInput);
    expand_view(parsed).into()
}

/// Compile-time style macro compiling declarative newline-separated rules directly into typed `Style` constructor functions or inline `Style` expressions.
///
/// Every statement inside `style! { ... }` must occupy its own line without semicolons. Placing two rules on one line is a compile-time error:
///
/// ```compile_fail
/// use euv::*;
///
/// let _bad: Style = style! {
///     AlignItems::Center Overflow::Hidden
/// };
/// ```
///
/// Trailing semicolons are also rejected in favor of clean newline separation:
///
/// ```compile_fail
/// use euv::*;
///
/// let _bad: Style = style! {
///     AlignItems::Center;
///     Overflow::Hidden;
/// };
/// ```
///
/// # Arguments
///
/// - `TokenStream` - Style rule block tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded typed `Style` functions or inline `Style` expression.
#[proc_macro]
pub fn style(input: TokenStream) -> TokenStream {
    let parsed: StyleBlockInput = parse_macro_input!(input as StyleBlockInput);
    expand_style(parsed).into()
}

/// Compile-time design token macro generating span-preserving typed `const` items.
///
/// # Arguments
///
/// - `TokenStream` - Design token definition entries.
///
/// # Returns
///
/// - `TokenStream` - Expanded constant declarations.
#[proc_macro]
pub fn design_tokens(input: TokenStream) -> TokenStream {
    let parsed: DesignTokenBlockInput = parse_macro_input!(input as DesignTokenBlockInput);
    expand_design_tokens(parsed).into()
}

/// Ergonomic reactive side-effect macro watching one or more signals or a global state type.
///
/// # Arguments
///
/// - `TokenStream` - Signal list or `global Type` and effect closure tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `Effect::new` or `GlobalState::watch` expression.
#[proc_macro]
pub fn watch(input: TokenStream) -> TokenStream {
    let parsed: WatchInput = parse_macro_input!(input as WatchInput);
    expand_watch(parsed).into()
}

/// Ergonomic memoized derived signal macro with equality short-circuiting.
///
/// # Arguments
///
/// - `TokenStream` - Signal list, return type, and computation closure tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `Memo::new` expression.
#[proc_macro]
pub fn computed(input: TokenStream) -> TokenStream {
    let parsed: ComputedInput = parse_macro_input!(input as ComputedInput);
    expand_computed(parsed).into()
}

/// Unified cross-platform application entry-point attribute macro (`#[euv]` or `#[euv(title = "...", width = 1280, height = 800)]`).
///
/// Generates native desktop/TUI/CLI, WebAssembly/MiniApp/QuickApp, Android/WearOS, iOS/iPadOS,
/// HarmonyOS, and wearable/spatial entry points from a single annotated root function.
///
/// # Arguments
///
/// - `TokenStream` - Optional window/runtime attribute options (`title`, `width`, `height`, `render`, `headless`, `fullscreen`).
/// - `TokenStream` - Target root view, `App` builder, or `main` function item tokens.
///
/// # Returns
///
/// - `TokenStream` - Original entry function plus target-specific platform bootstrap entry points.
#[proc_macro_attribute]
pub fn euv(attr: TokenStream, item: TokenStream) -> TokenStream {
    let args: EuvAttrArgs = parse_macro_input!(attr as EuvAttrArgs);
    let func: ItemFn = parse_macro_input!(item as ItemFn);
    expand_euv_attr(args, func).into()
}

/// Attribute macro marking a struct as a typed cross-component `CustomEvent` with ergonomic `::emit` and `::on` helpers.
///
/// # Arguments
///
/// - `TokenStream` - Unused attribute arguments.
/// - `TokenStream` - Target struct item tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded struct plus `CustomEvent` implementation and helpers.
#[proc_macro_attribute]
pub fn event(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let parsed: ItemStruct = parse_macro_input!(item as ItemStruct);
    expand_event_attr(parsed).into()
}

/// Dispatches a typed cross-component custom event through `EventBus`.
///
/// # Arguments
///
/// - `TokenStream` - Event value expression tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `::euv::EventBus::emit(...)` call.
#[proc_macro]
pub fn emit(input: TokenStream) -> TokenStream {
    let expr: Expr = parse_macro_input!(input as Expr);
    expand_emit(expr).into()
}

/// Subscribes a scope-owned listener to a typed cross-component custom event (`on_event!(MyEvent => |ev| { ... })`).
///
/// # Arguments
///
/// - `TokenStream` - `EventType => handler` tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `::euv::EventBus::on::<EventType, _>(handler)` returning `::euv::Effect`.
#[proc_macro]
pub fn on_event(input: TokenStream) -> TokenStream {
    let parsed: OnEventInput = parse_macro_input!(input as OnEventInput);
    expand_on_event(parsed).into()
}

/// Attribute macro turning a struct into a typed app-wide `GlobalState` store with `.provide()`, `::use_global()`, `::get_global()`, `::set_global()`, `::update_global()`, and `::watch_global()`.
///
/// # Arguments
///
/// - `TokenStream` - Unused attribute arguments.
/// - `TokenStream` - Target struct item tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded struct plus global state store methods.
#[proc_macro_attribute]
pub fn state_store(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let parsed: ItemStruct = parse_macro_input!(item as ItemStruct);
    expand_state_store_attr(parsed).into()
}

/// Unified reactive state macro for scoped local state and typed app-wide global state.
///
/// - `state!(init_expr)` creates a scope-owned `::euv::LocalState::new(init_expr)`
/// - `state!(global Type)` looks up `::euv::GlobalState::use_state::<Type>()`
/// - `state!(global Type = init_expr)` lazily initializes or looks up `::euv::GlobalState::use_or_init::<Type, _>(...)`
///
/// # Arguments
///
/// - `TokenStream` - Local initializer expression or `global Type [= init_expr]` tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `LocalState` or `GlobalState` expression.
#[proc_macro]
pub fn state(input: TokenStream) -> TokenStream {
    let parsed: StateInput = parse_macro_input!(input as StateInput);
    expand_state(parsed).into()
}

/// Attribute macro splitting an `impl MyComponent { fn render(&self) -> NodeId { ... } ... }` block into clean `impl Lifecycle` and `impl Component` blocks.
///
/// # Arguments
///
/// - `TokenStream` - Optional `retention = Freeze | Destroy | Alive` options.
/// - `TokenStream` - Target `impl` block tokens.
///
/// # Returns
///
/// - `TokenStream` - Expanded `Lifecycle`, `Component`, and optional inherent helper `impl` blocks.
#[proc_macro_attribute]
pub fn component(attr: TokenStream, item: TokenStream) -> TokenStream {
    let args: ComponentAttrArgs = parse_macro_input!(attr as ComponentAttrArgs);
    let parsed: ItemImpl = parse_macro_input!(item as ItemImpl);
    expand_component_attr(args, parsed).into()
}

/// Derive macro implementing `ComponentProps` and span-preserving fluent named-prop setters for `view! { MyComp { prop: value } }`.
///
/// # Arguments
///
/// - `TokenStream` - Target struct definition tokens.
///
/// # Returns
///
/// - `TokenStream` - Generated `ComponentProps` and fluent setter implementations.
#[proc_macro_derive(Props, attributes(prop))]
pub fn derive_props(input: TokenStream) -> TokenStream {
    let parsed: DeriveInput = parse_macro_input!(input as DeriveInput);
    expand_props_derive(parsed).into()
}