kit_macros/
lib.rs

1//! Procedural macros for the Kit framework
2//!
3//! This crate provides compile-time validated macros for:
4//! - Inertia.js responses with component validation
5//! - Named route redirects with route validation
6
7use proc_macro::TokenStream;
8
9mod inertia;
10mod redirect;
11mod utils;
12
13/// Derive macro for generating `Serialize` implementation for Inertia props
14///
15/// # Example
16///
17/// ```rust,ignore
18/// #[derive(InertiaProps)]
19/// struct HomeProps {
20///     title: String,
21///     user: User,
22/// }
23/// ```
24#[proc_macro_derive(InertiaProps)]
25pub fn derive_inertia_props(input: TokenStream) -> TokenStream {
26    inertia::derive_inertia_props_impl(input)
27}
28
29/// Create an Inertia response with compile-time component validation
30///
31/// # Examples
32///
33/// ## With typed struct (recommended for type safety):
34/// ```rust,ignore
35/// #[derive(InertiaProps)]
36/// struct HomeProps {
37///     title: String,
38///     user: User,
39/// }
40///
41/// inertia_response!("Home", HomeProps { title: "Welcome".into(), user })
42/// ```
43///
44/// ## With JSON-like syntax (for quick prototyping):
45/// ```rust,ignore
46/// inertia_response!("Dashboard", { "user": { "name": "John" } })
47/// ```
48///
49/// This macro validates that the component file exists at compile time.
50/// If `frontend/src/pages/Dashboard.tsx` doesn't exist, you'll get a compile error.
51#[proc_macro]
52pub fn inertia_response(input: TokenStream) -> TokenStream {
53    inertia::inertia_response_impl(input)
54}
55
56/// Create a redirect to a named route with compile-time validation
57///
58/// # Examples
59///
60/// ```rust,ignore
61/// // Simple redirect
62/// redirect!("users.index").into()
63///
64/// // Redirect with route parameters
65/// redirect!("users.show").with("id", "42").into()
66///
67/// // Redirect with query parameters
68/// redirect!("users.index").query("page", "1").into()
69/// ```
70///
71/// This macro validates that the route name exists at compile time.
72/// If the route doesn't exist, you'll get a compile error with suggestions.
73#[proc_macro]
74pub fn redirect(input: TokenStream) -> TokenStream {
75    redirect::redirect_impl(input)
76}