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