aerro_macros/lib.rs
1//! Procedural macros for `aerro`. See [`aerro`](https://docs.rs/aerro) for
2//! the full usage guide.
3
4mod attrs;
5mod codegen;
6mod operation;
7
8/// Derive the [`aerro::Aerro`](https://docs.rs/aerro/latest/aerro/trait.Aerro.html)
9/// trait for an error enum.
10///
11/// # Attributes on each variant — `#[aerro(...)]`
12///
13/// | Key | Type | Required | Description |
14/// |-----|------|----------|-------------|
15/// | `category` | ident | ✓ | Error bucket: `Business`, `System`, `Auth`, `NotFound`, `RateLimit`, `Validation`, `Unavailable`, `Unknown` |
16/// | `code` | ident | ✓ | gRPC status code: `Ok`, `Cancelled`, `Unknown`, `InvalidArgument`, `NotFound`, `AlreadyExists`, `PermissionDenied`, `ResourceExhausted`, `FailedPrecondition`, `Aborted`, `OutOfRange`, `Unimplemented`, `Internal`, `Unavailable`, `DataLoss`, `Unauthenticated` |
17/// | `error` | string literal | ✓ | Display format; `{field_name}` interpolates variant fields |
18/// | `exposure` | ident | — | Override default exposure: `Internal`, `Trusted`, `Public` (default derived from `category`) |
19///
20/// # Field-level attributes
21///
22/// | Attribute | Description |
23/// |-----------|-------------|
24/// | `#[source]` | Marks this field as the `std::error::Error::source()` |
25/// | `#[from]` | Generates a `From<FieldType>` impl (field must be the only field) |
26///
27/// # Example
28///
29/// ```rust,ignore
30/// #[derive(Debug, aerro::Aerro)]
31/// pub enum CreateUserError {
32/// #[aerro(category = Business, code = AlreadyExists, error = "email already taken: {email}")]
33/// EmailTaken { email: String },
34///
35/// #[aerro(category = System, code = Internal, error = "db.unavailable")]
36/// DbUnavailable,
37/// }
38/// ```
39#[proc_macro_derive(Aerro, attributes(aerro, source, from))]
40pub fn aerro_derive(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
41 operation::expand(item.into()).into()
42}