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;
7
8mod route;
9
10use route::HttpMethod;
11
12/// Define a GET route handler
13///
14/// # Example
15///
16/// ```ignore
17/// #[get("/users/:id")]
18/// async fn get_user(path: Path<String>) -> Json<User> {
19/// // handler code
20/// }
21/// ```
22#[proc_macro_attribute]
23pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
24 route::expand_route_macro(HttpMethod::Get, args, input)
25}
26
27/// Define a POST route handler
28///
29/// # Example
30///
31/// ```ignore
32/// #[post("/users")]
33/// async fn create_user(body: Json<CreateUser>) -> Json<User> {
34/// // handler code
35/// }
36/// ```
37#[proc_macro_attribute]
38pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
39 route::expand_route_macro(HttpMethod::Post, args, input)
40}
41
42/// Define a PUT route handler
43///
44/// # Example
45///
46/// ```ignore
47/// #[put("/users/:id")]
48/// async fn update_user(path: Path<String>, body: Json<User>) -> Json<User> {
49/// // handler code
50/// }
51/// ```
52#[proc_macro_attribute]
53pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
54 route::expand_route_macro(HttpMethod::Put, args, input)
55}
56
57/// Define a DELETE route handler
58///
59/// # Example
60///
61/// ```ignore
62/// #[delete("/users/:id")]
63/// async fn delete_user(path: Path<String>) -> StatusCode {
64/// // handler code
65/// }
66/// ```
67#[proc_macro_attribute]
68pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
69 route::expand_route_macro(HttpMethod::Delete, args, input)
70}
71
72/// Define a PATCH route handler
73///
74/// # Example
75///
76/// ```ignore
77/// #[patch("/users/:id")]
78/// async fn patch_user(path: Path<String>, body: Json<UserPatch>) -> Json<User> {
79/// // handler code
80/// }
81/// ```
82#[proc_macro_attribute]
83pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
84 route::expand_route_macro(HttpMethod::Patch, args, input)
85}