error_forge_derive/lib.rs
1//! Procedural macros for the `error-forge` crate.
2//!
3//! - `#[derive(ModError)]`, behind the default `derive` feature (pulls
4//! in `syn`, `quote` and `proc-macro2`).
5//! - An internal identifier-pasting macro used by `define_errors!`,
6//! always available and built on the compiler's `proc_macro` API
7//! alone. It is not part of the public API.
8//!
9//! Use these through `error-forge`; see its documentation.
10
11extern crate proc_macro;
12
13#[cfg(feature = "derive")]
14mod mod_error;
15mod paste;
16
17use proc_macro::TokenStream;
18
19/// Derive macro for ModError
20///
21/// This macro automatically implements the ForgeError trait and common
22/// error handling functionality for a struct or enum, allowing for
23/// "lazy mode" error creation with minimal boilerplate.
24///
25/// # Example
26///
27/// When the macro is used in your application where error-forge is a dependency:
28///
29/// ```ignore
30/// use error_forge::ModError;
31///
32/// #[derive(Debug, ModError)]
33/// #[error_prefix("Database")]
34/// pub enum DbError {
35/// #[error_display("Connection to {0} failed")]
36/// ConnectionFailed(String),
37///
38/// #[error_display("Query execution failed: {reason}")]
39/// QueryFailed { reason: String },
40///
41/// #[error_display("Transaction error")]
42/// #[error_http_status(400)]
43/// TransactionError,
44/// }
45/// ```
46///
47/// `#[error_display("...")]` is optional; without it a variant displays
48/// as its name. The string is a `format!` string and may reference any
49/// subset of the variant's fields: by name for struct-like variants
50/// (a raw identifier such as `r#type` is written `{type}`), by position
51/// (`{0}`, `{}`) for tuple variants. Fields it does not mention are
52/// ignored, `{{` and `}}` produce literal braces, and a width or
53/// precision given where the error is formatted (`{:>20}`) pads or
54/// truncates the whole message.
55///
56/// # Attributes
57///
58/// On the enum (or struct):
59///
60/// - `#[error_prefix("...")]`: caption prefix, giving `"<prefix>: Error"`.
61///
62/// On each enum variant:
63///
64/// - `#[error_display("...")]`, `#[error_kind("...")]`,
65/// `#[error_caption("...")]`: string literals.
66/// - `#[error_retryable]`, `#[error_fatal]`: flags. A bare flag means
67/// `true`; an explicit value (`#[error_retryable(false)]` or
68/// `#[error_retryable = false]`) is honoured.
69/// - `#[error_http_status(404)]`: an integer that fits in `u16`.
70/// - `#[error_exit_code(2)]`: an integer that fits in `i32`.
71///
72/// Each attribute accepts both the list form (`#[name(value)]`) and the
73/// name-value form (`#[name = value]`). A value of the wrong type or out
74/// of range is a compile error.
75///
76/// # Structs
77///
78/// On a struct only `#[error_prefix]` is read. The struct displays as
79/// `"<prefix>: Error"`, its `kind()` is the struct name, and the other
80/// metadata methods use the `ForgeError` defaults. The variant
81/// attributes listed above (`#[error_display]`, `#[error_retryable]`,
82/// and so on) are currently ignored when placed on a struct.
83///
84/// Note: This is a procedural macro that is re-exported by the `error-forge` crate.
85/// When using in your application, import it from the main crate with `use error_forge::ModError;`.
86#[cfg(feature = "derive")]
87#[proc_macro_derive(
88 ModError,
89 attributes(
90 error_prefix,
91 error_display,
92 error_kind,
93 error_caption,
94 error_retryable,
95 error_http_status,
96 error_exit_code,
97 error_fatal
98 )
99)]
100pub fn derive_mod_error(input: TokenStream) -> TokenStream {
101 let input = syn::parse_macro_input!(input as syn::DeriveInput);
102 TokenStream::from(mod_error::expand(&input))
103}
104
105/// Identifier pasting for `error-forge`'s `macro_rules!` macros.
106///
107/// Not public API: `error-forge` re-exports it as
108/// `error_forge::__private::paste!` for `define_errors!`, and it may
109/// change in any release. Each `[< ... >]` bracket in the input is
110/// replaced by one identifier built from the segments inside it:
111/// identifiers (a leading `r#` is dropped), each optionally followed by
112/// `:lower` or `:upper`. All other tokens pass through unchanged.
113///
114/// # Example
115///
116/// ```ignore
117/// error_forge::__private::paste! {
118/// fn [<NotFound:lower>]() {}
119/// }
120/// // expands to `fn notfound() {}`
121/// ```
122#[doc(hidden)]
123#[proc_macro]
124pub fn __paste(input: TokenStream) -> TokenStream {
125 paste::expand(input)
126}