rorpc_macros/lib.rs
1//! Thin proc-macro bridge for [`rorpc_parse`].
2//!
3//! This crate contains only proc-macro entry points. All parsing, validation,
4//! and code generation logic lives in `rorpc-parse` where it can be tested
5//! with normal `#[test]` functions.
6
7use proc_macro::TokenStream;
8use syn::parse_macro_input;
9
10/// Annotate an Axum handler with explicit HTTP method and path.
11///
12/// This is the canonical syntax for specifying both method and path explicitly.
13/// The function remains a valid Axum handler with metadata registered for contract
14/// generation and router discovery.
15///
16/// # Syntax
17///
18/// ```rust,ignore
19/// #[rorpc::route(method = "POST", path = "/planet/list")]
20/// async fn list_planets(State(db): State<Db>) -> Json<Vec<Planet>> {
21/// Json(db.list().await)
22/// }
23/// ```
24///
25/// # Arguments
26///
27/// - `method` — HTTP method string (`"GET"`, `"POST"`, etc.), normalized to uppercase. Required.
28/// - `path` — Route path string (e.g. `"/planet/list"`). Required.
29/// - `data` — String literal naming the SSE data payload type for streaming handlers (e.g. `"StreamEvent"`). Optional.
30///
31/// # For Shorthand Syntax
32///
33/// Consider using method-specific macros for brevity:
34/// - `#[rorpc::get("/path")]`
35/// - `#[rorpc::post("/path")]`
36/// - `#[rorpc::put("/path")]`
37/// - `#[rorpc::patch("/path")]`
38/// - `#[rorpc::delete("/path")]`
39#[proc_macro_attribute]
40pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
41 let args = parse_macro_input!(attr as rorpc_parse::codegen::OrpcArgs);
42 let func = parse_macro_input!(item as syn::ItemFn);
43 rorpc_parse::codegen::expand_orpc(args, func).into()
44}
45
46/// Shorthand for `#[orpc::route(method = "GET", path = "...")]`.
47///
48/// # Syntax
49///
50/// ```rust,ignore
51/// #[orpc::get("/planet/list")]
52/// async fn list_planets(State(db): State<Db>) -> Json<Vec<Planet>> {
53/// Json(db.list().await)
54/// }
55/// ```
56///
57/// # With streaming data type
58///
59/// ```rust,ignore
60/// #[orpc::get("/stream", data = "StreamEvent")]
61/// async fn stream_events() -> Sse<impl Stream<Item = Event>> {
62/// // data takes a string literal for IDE support
63/// }
64/// ```
65#[proc_macro_attribute]
66pub fn get(attr: TokenStream, item: TokenStream) -> TokenStream {
67 let shorthand_args = parse_macro_input!(attr as rorpc_parse::codegen::MethodShorthandArgs);
68 let func = parse_macro_input!(item as syn::ItemFn);
69 let args = shorthand_args.into_orpc_args("GET");
70 rorpc_parse::codegen::expand_orpc(args, func).into()
71}
72
73/// Shorthand for `#[orpc::route(method = "POST", path = "...")]`.
74///
75/// # Syntax
76///
77/// ```rust,ignore
78/// #[orpc::post("/planet/create")]
79/// async fn create_planet(
80/// State(db): State<Db>,
81/// Json(input): Json<CreateInput>,
82/// ) -> Result<Json<Planet>, AppError> {
83/// // ...
84/// }
85/// ```
86#[proc_macro_attribute]
87pub fn post(attr: TokenStream, item: TokenStream) -> TokenStream {
88 let shorthand_args = parse_macro_input!(attr as rorpc_parse::codegen::MethodShorthandArgs);
89 let func = parse_macro_input!(item as syn::ItemFn);
90 let args = shorthand_args.into_orpc_args("POST");
91 rorpc_parse::codegen::expand_orpc(args, func).into()
92}
93
94/// Shorthand for `#[orpc::route(method = "PUT", path = "...")]`.
95///
96/// # Syntax
97///
98/// ```rust,ignore
99/// #[orpc::put("/planet/{id}")]
100/// async fn update_planet(
101/// State(db): State<Db>,
102/// Json(input): Json<UpdateInput>,
103/// ) -> Result<Json<Planet>, AppError> {
104/// // ...
105/// }
106/// ```
107#[proc_macro_attribute]
108pub fn put(attr: TokenStream, item: TokenStream) -> TokenStream {
109 let shorthand_args = parse_macro_input!(attr as rorpc_parse::codegen::MethodShorthandArgs);
110 let func = parse_macro_input!(item as syn::ItemFn);
111 let args = shorthand_args.into_orpc_args("PUT");
112 rorpc_parse::codegen::expand_orpc(args, func).into()
113}
114
115/// Shorthand for `#[orpc::route(method = "PATCH", path = "...")]`.
116///
117/// # Syntax
118///
119/// ```rust,ignore
120/// #[orpc::patch("/planet/{id}")]
121/// async fn patch_planet(
122/// State(db): State<Db>,
123/// Json(input): Json<PatchInput>,
124/// ) -> Result<Json<Planet>, AppError> {
125/// // ...
126/// }
127/// ```
128#[proc_macro_attribute]
129pub fn patch(attr: TokenStream, item: TokenStream) -> TokenStream {
130 let shorthand_args = parse_macro_input!(attr as rorpc_parse::codegen::MethodShorthandArgs);
131 let func = parse_macro_input!(item as syn::ItemFn);
132 let args = shorthand_args.into_orpc_args("PATCH");
133 rorpc_parse::codegen::expand_orpc(args, func).into()
134}
135
136/// Shorthand for `#[orpc::route(method = "DELETE", path = "...")]`.
137///
138/// # Syntax
139///
140/// ```rust,ignore
141/// #[orpc::delete("/planet/{id}")]
142/// async fn delete_planet(
143/// State(db): State<Db>,
144/// Json(input): Json<DeleteInput>,
145/// ) -> Result<Json<()>, AppError> {
146/// // ...
147/// }
148/// ```
149#[proc_macro_attribute]
150pub fn delete(attr: TokenStream, item: TokenStream) -> TokenStream {
151 let shorthand_args = parse_macro_input!(attr as rorpc_parse::codegen::MethodShorthandArgs);
152 let func = parse_macro_input!(item as syn::ItemFn);
153 let args = shorthand_args.into_orpc_args("DELETE");
154 rorpc_parse::codegen::expand_orpc(args, func).into()
155}
156
157/// Auto-discovery router macro with optional module path filtering.
158///
159/// Discovers all `#[rorpc]`-annotated handlers via the `inventory` crate and
160/// builds an Axum `Router`. Accepts an optional state expression and/or a
161/// module path pattern to restrict which handlers are included.
162///
163/// # Syntax
164///
165/// ```text
166/// router!() // all handlers, no state
167/// router!(state) // all handlers, with state
168/// router!("pattern") // filtered, no state
169/// router!("pattern", state) // filtered + state (any order)
170/// router!(state, "pattern") // filtered + state (any order)
171/// router!(["pat1", "pat2"]) // multiple patterns
172/// router!("prefix::{a,b}") // brace expansion
173/// router!("prefix::*") // wildcard
174/// ```
175///
176/// # Pattern matching
177///
178/// Patterns match against the handler's `module_path!()` value:
179/// - `"handlers::planet"` — exact module or any child
180/// - `"handlers::*"` — all direct and nested children of `handlers::`
181/// - `"handlers::{planet,user}"` — brace expansion
182/// - `["handlers::planet", "api::v1"]` — explicit list
183#[proc_macro]
184pub fn router(input: TokenStream) -> TokenStream {
185 let args = parse_macro_input!(input as rorpc_parse::codegen::RouterArgs);
186 rorpc_parse::codegen::expand_router(args).into()
187}
188
189/// Derive macro that generates a `fn zod_ts() -> String` method on structs and enums.
190///
191/// The generated method returns a complete TypeScript block with a Zod schema
192/// and a `z.infer` type alias. An `inventory::submit!` call registers the real
193/// schema so contract generation prefers it over the `z.unknown()` fallback
194/// emitted by `#[rorpc]`.
195///
196/// # Example
197///
198/// ```rust,ignore
199/// #[derive(Serialize, Deserialize, ZodTs)]
200/// pub struct Planet {
201/// pub id: i32,
202/// #[zod(min_length(1), max_length(100))]
203/// pub name: String,
204/// pub description: Option<String>,
205/// }
206/// ```
207///
208/// # Supported `#[zod(...)]` field attributes
209///
210/// **Strings:** `min_length(n)`, `max_length(n)`, `length(n)`, `email`, `url`,
211/// `regex("pattern")`, `starts_with("s")`, `ends_with("s")`, `includes("s")`
212///
213/// **Numbers:** `min(n)`, `max(n)`, `int`, `positive`, `negative`,
214/// `nonnegative`, `nonpositive`, `finite`
215///
216/// **Arrays (`Vec<T>`):** `min_length(n)`, `max_length(n)`, `length(n)`
217#[proc_macro_derive(ZodTs, attributes(zod))]
218pub fn derive_zod_ts(input: TokenStream) -> TokenStream {
219 let input = parse_macro_input!(input as syn::DeriveInput);
220 match rorpc_parse::codegen::derive_zod_ts(input) {
221 Ok(tokens) => tokens.into(),
222 Err(err) => err.to_compile_error().into(),
223 }
224}
225
226/// Derive macro for registering error enum variants with rorpc.
227///
228/// Annotate an error enum so `generate_contract()` can emit TypeScript
229/// `.errors({...})` entries. Variant names are converted to `SCREAMING_SNAKE_CASE`.
230///
231/// # Example
232///
233/// ```rust,ignore
234/// #[derive(OrpcError)]
235/// pub enum AppError {
236/// NotFound,
237/// Conflict { reason: String },
238/// DatabaseError(String),
239/// }
240/// ```
241///
242/// # Variant mapping
243///
244/// - Unit variants: `NotFound` → `NOT_FOUND: {}`
245/// - Struct variants: `Conflict { reason: String }` → `CONFLICT: { data: z.object({...}) }`
246/// - Tuple variants: `DatabaseError(String)` → `DATABASE_ERROR: { data: z.string() }`
247#[proc_macro_derive(OrpcError)]
248pub fn derive_orpc_errors(input: TokenStream) -> TokenStream {
249 let input = parse_macro_input!(input as syn::DeriveInput);
250 match rorpc_parse::codegen::expand_orpc_errors(input) {
251 Ok(tokens) => tokens.into(),
252 Err(err) => err.to_compile_error().into(),
253 }
254}