Skip to main content

aro_macros/
lib.rs

1//! Procedural macros for the Aro web framework.
2//!
3//! Provides HTTP method attribute macros (`#[get("/path")]`, `#[post("/path")]`, etc.)
4//! and the `AroEntity` derive macro for reducing per-entity boilerplate.
5
6mod crate_path;
7mod entity;
8mod route;
9
10use proc_macro::TokenStream;
11
12/// Attach a `GET` route to a handler function.
13///
14/// # Example
15///
16/// ```ignore
17/// #[get("/users/{id}")]
18/// fn show_user(id: u64) { /* ... */ }
19/// ```
20#[proc_macro_attribute]
21pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
22    route::expand_route("get", args.into(), input.into()).into()
23}
24
25/// Attach a `POST` route to a handler function.
26#[proc_macro_attribute]
27pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
28    route::expand_route("post", args.into(), input.into()).into()
29}
30
31/// Attach a `PUT` route to a handler function.
32#[proc_macro_attribute]
33pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
34    route::expand_route("put", args.into(), input.into()).into()
35}
36
37/// Attach a `DELETE` route to a handler function.
38#[proc_macro_attribute]
39pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
40    route::expand_route("delete", args.into(), input.into()).into()
41}
42
43/// Attach a `PATCH` route to a handler function.
44#[proc_macro_attribute]
45pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
46    route::expand_route("patch", args.into(), input.into()).into()
47}
48
49/// Derive macro to generate Aro entity trait implementations.
50///
51/// Generates implementations of `aro_core::Entity`, `fletch_orm::Entity`, and
52/// `sqlx::FromRow` from a single annotated domain struct.
53///
54/// # Struct Attributes
55///
56/// - `#[aro(table = "table_name")]`: Override the table name
57///   (default: `snake_case` pluralized struct name).
58///
59/// # Field Attributes
60///
61/// - `#[aro(id)]` (required, exactly one): Marks the primary key field.
62/// - `#[aro(skip)]`: Exclude a field from persistence. Skipped fields are
63///   populated with `Default::default()` in the generated `FromRow` impl.
64/// - `#[aro(rename = "col_name")]`: Override the column name.
65///
66/// # Example
67///
68/// ```ignore
69/// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, AroEntity)]
70/// #[aro(table = "users")]
71/// pub struct User {
72///     #[aro(id)]
73///     pub id: i64,
74///     pub name: String,
75///     pub email: String,
76/// }
77/// ```
78#[proc_macro_derive(AroEntity, attributes(aro))]
79pub fn derive_aro_entity(input: TokenStream) -> TokenStream {
80    entity::expand_aro_entity(input.into()).into()
81}