Skip to main content

lariv_rs_macros/
lib.rs

1//! Procedural macros for `lariv-rs`.
2//!
3//! # Macros
4//!
5//! - [`html_form`] — attribute macro: serde field wiring + `HtmlForm` trait impl
6//! - [`define_plugin_routes`] — proc macro: route tags, URL builders, response traits, `RouteRegistrar` hook
7//! - [`main`] — attribute macro: large-stack async `main` for deep HList install/mount
8
9mod html_form;
10mod main_attr;
11mod plugin_routes;
12
13use proc_macro::TokenStream;
14
15/// Attribute macro: owns serde field wiring and emits a `HtmlForm` trait implementation.
16///
17/// Applied to a struct or tagged enum. Field attributes use `#[form(...)]`:
18///
19/// - `label = "..."` — display label
20/// - `widget = Text | Email | Password | ...` — HTML widget type
21/// - `required` — non-empty validation
22/// - `name = "..."` — override HTML `name` attribute
23/// - `row = "..."` — group fields in the same form row
24///
25/// Macro args (on the attribute itself): `default`, `no_debug`, `tag = "..."`.
26///
27/// # Examples
28///
29/// ```ignore
30/// #[html_form]
31/// pub struct MyForm {
32///     #[form(label = "Name", widget = Text, required)]
33///     pub name: String,
34/// }
35/// ```
36#[proc_macro_attribute]
37pub fn html_form(attr: TokenStream, item: TokenStream) -> TokenStream {
38    html_form::html_form_attr(attr, item)
39}
40
41/// Generate route tags, proof type, and a `RouteRegistrar` hook.
42///
43/// # DSL
44///
45/// ```text
46/// define_plugin_routes! {
47///     plugin: PluginTag;           // required — plugin identity (for hook tagging)
48///     proof: ProofName;            // optional — reserved for future compile-time proofs
49///     slots: SlotCtxTy;            // optional — reserved for page/slot wiring
50///     pages: [ ... ];              // optional — reserved pane/page declarations
51///     routes: [
52///         get RouteTag, "/path", handler::fn;
53///         post RouteTag, "/path/{id}", handler::fn, modal;
54///         get RouteTag, "/path/{*tail}", bare handler::fn, redirect;
55///         post RouteTag, "/path", bare handler::fn, fragment(SwapKeyTy);
56///         post RouteTag, "/path", bare handler::fn, file;
57///         post RouteTag, "/path", bare handler::fn, generation;
58///         get RouteTag, "/path", bare handler::fn, raw;
59///         get RouteTag, "/users/u/{id}", handler::fn, param id: i64;
60///     ]
61/// }
62/// ```
63///
64/// ## Route line syntax
65///
66/// `{get|post} Tag, "path-literal", [bare] handler_path [, response] [, param name: Type]* ;`
67///
68/// - **`bare`** — handler is registered without the default view stack wrapper (required
69///   when specifying a non-default response kind).
70/// - **Response kinds** (default: `pane` for GET, `pane` for POST):
71///   - `modal` — HTMX modal overlay
72///   - `fragment(SwapKey)` — partial HTML swap keyed by `SwapKey`
73///   - `file` — file download response
74///   - `redirect` — redirect response (GET: pane redirect; POST: boost redirect)
75///   - `generation` — streaming/generation POST
76///   - `raw` — unwrapped handler response
77/// - **`param name: Type`** — override inferred path param type (default: `{id}` → `i64`,
78///   `{*name}` → `Vec<String>`, else `String`).
79///
80/// ## Generated items (per route)
81///
82/// - `RouteTag` struct — URL builder with `PATH`, `new(...)`, `path()`, `url()`, `with_query()`
83/// - `RouteTag: RouteTag, RouteUrl` — path metadata
84/// - Response marker traits — `AppPaneGet/Post`, `ModalGet`, `FragmentGet/Post`, etc.
85/// - `Hook: RouteRegistrar<HttpCapability<R>>` — prepends routes onto the HTTP capability
86///
87/// # Examples
88///
89/// ```ignore
90/// define_plugin_routes! {
91///     plugin: UsersTag;
92///     routes: [
93///         get UsersLoginGetRouteTag, "/users/login", handlers::auth::login_get;
94///         post UsersLoginPostRouteTag, "/users/login", handlers::auth::login_post;
95///         get UsersLogoutGetRouteTag, "/users/logout", bare handlers::auth::logout, redirect;
96///         get UsersListRouteTag, "/users", handlers::users::list, fragment(UserTableKey);
97///     ]
98/// }
99/// ```
100#[proc_macro]
101pub fn define_plugin_routes(input: TokenStream) -> TokenStream {
102    plugin_routes::define_plugin_routes(input)
103}
104
105/// Run `async fn main` on a thread with a raised stack size.
106///
107/// Re-exported from `lariv-rs` as [`lariv_rs::main`]. See that crate for attributes
108/// and examples.
109#[proc_macro_attribute]
110pub fn main(attr: TokenStream, item: TokenStream) -> TokenStream {
111    main_attr::main_attr(attr, item)
112}