Skip to main content

rust_api_macros/
lib.rs

1//! Procedural macros for rust-api framework
2//!
3//! Provides route macros like #[get], #[post], etc. for defining HTTP endpoints
4//! in a FastAPI-style syntax.
5
6use proc_macro::TokenStream;
7use quote::quote;
8use syn::{parse_macro_input, ItemFn, LitStr};
9
10mod route;
11
12use route::{HttpMethod, RouteArgs};
13
14/// Define a GET route handler
15///
16/// # Example
17///
18/// ```ignore
19/// #[get("/users/:id")]
20/// async fn get_user(path: Path<String>) -> Json<User> {
21///     // handler code
22/// }
23/// ```
24#[proc_macro_attribute]
25pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
26    route::expand_route_macro(HttpMethod::Get, args, input)
27}
28
29/// Define a POST route handler
30///
31/// # Example
32///
33/// ```ignore
34/// #[post("/users")]
35/// async fn create_user(body: Json<CreateUser>) -> Json<User> {
36///     // handler code
37/// }
38/// ```
39#[proc_macro_attribute]
40pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
41    route::expand_route_macro(HttpMethod::Post, args, input)
42}
43
44/// Define a PUT route handler
45///
46/// # Example
47///
48/// ```ignore
49/// #[put("/users/:id")]
50/// async fn update_user(path: Path<String>, body: Json<User>) -> Json<User> {
51///     // handler code
52/// }
53/// ```
54#[proc_macro_attribute]
55pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
56    route::expand_route_macro(HttpMethod::Put, args, input)
57}
58
59/// Define a DELETE route handler
60///
61/// # Example
62///
63/// ```ignore
64/// #[delete("/users/:id")]
65/// async fn delete_user(path: Path<String>) -> StatusCode {
66///     // handler code
67/// }
68/// ```
69#[proc_macro_attribute]
70pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
71    route::expand_route_macro(HttpMethod::Delete, args, input)
72}
73
74/// Define a PATCH route handler
75///
76/// # Example
77///
78/// ```ignore
79/// #[patch("/users/:id")]
80/// async fn patch_user(path: Path<String>, body: Json<UserPatch>) -> Json<User> {
81///     // handler code
82/// }
83/// ```
84#[proc_macro_attribute]
85pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
86    route::expand_route_macro(HttpMethod::Patch, args, input)
87}