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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use TokenStream;
use ;
/// Derive the `Error` trait for a struct.
///
/// The `#[derive(Error)]` macro provides a declarative way to implement
/// both the [`Error`] and [`Display`] traits for your custom error types.
/// It is controlled by `#[error(...)]` *type-level attributes* that specify
/// how the error should behave.
///
/// # General form
///
/// ```ignore
/// #[derive(Error, Debug)]
/// #[error("a formatted message {arg}", arg)]
/// // #[error(source(...))]
/// // #[error(transparent(...))]
/// // #[error(from)]
/// struct MyError { /* fields */ }
/// ```
///
/// The attribute syntax follows the form:
///
/// ```text
/// #[error(<parameter>)]
/// ```
///
/// Multiple `#[error(...)]` attributes can be applied to the same type,
/// and are classified according to their *parameter kind*.
///
///
/// # Attribute kinds
///
/// The following attribute kinds are supported:
///
/// ## `#[error("...")]` - format string
///
/// Provides a [`Display`] implementation for the error type, using
/// Rust formatting syntax. Additional expressions may be listed as
/// comma-separated arguments.
///
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error("failed to read file: {path}")]
/// struct ReadError {
/// path: String,
/// }
/// ```
///
/// Expands to:
///
/// ```ignore
/// impl std::fmt::Display for ReadError {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "failed to read file: {}", self.path)
/// }
/// }
/// ```
///
/// ## `#[error(source(<field>))]` - source field
///
/// Declares which field represents the underlying cause of the error.
/// This field will be returned from `Error::source`.
///
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error("network request failed")]
/// #[error(source(io))]
/// struct NetworkError {
/// io: std::io::Error,
/// }
/// ```
///
/// Produces:
/// ```ignore
/// impl std::error::Error for NetworkError {
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
/// Some(&self.io)
/// }
/// }
/// ```
///
/// ## `#[error(transparent(<field>))]` - transparent wrapper
///
/// Makes the error type a transparent wrapper around one of its fields.
/// Both [`Display`] and [`Error`] are forwarded directly to that field.
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error(transparent(0))]
/// struct Wrapper(std::io::Error);
/// ```
///
/// This makes `Wrapper` behave exactly like its inner error for
/// formatting and chaining purposes.
///
///
/// ## `#[error(from)]` conversion
///
/// Requests that a `From<T>` implementation be generated for a specified
/// field type, allowing automatic conversion into the error type.
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error("parse failed")]
/// #[error(from)]
/// struct ParseError {
/// inner: std::num::ParseIntError,
/// }
/// ```
///
/// Allows:
/// ```ignore
/// let err: ParseError = "abc".parse::<u32>().unwrap_err().into();
/// ```
///
///
/// # Enums
///
/// Enums may use the same attribute kinds at the *variant* level.
/// Each variant can provide its own formatting, source, or transparency.
/// ```
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// enum MyError {
/// #[error("invalid input: {_0}")]
/// InvalidInput(String),
///
/// #[error(transparent(inner))]
/// Io {
/// inner: std::io::Error,
/// },
///
/// #[error("other error")]
/// Other,
/// }
/// ```
///
///
/// # Inline options
///
/// Inline behavior for generated methods can be controlled using:
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error(inline(always))]
/// #[error("{msg}")]
/// struct FastError {
/// msg: &'static str,
/// }
/// ```
///
/// Valid values are:
/// - `neutral` (default) → `#[inline]`
/// - `always` → `#[inline(always)]`
/// - `never` → `#[inline(never)]`
///
/// When omitted, no explicit `#[inline(...)]` attribute is emitted.
///
///
/// # Root import
///
/// All generated code defaults to using `::core`. This can be overridden:
/// ```rust
/// # use fack_macro::Error;
/// #[derive(Error, Debug)]
/// #[error("{msg}")]
/// #[error(import(::std))]
/// struct StdError {
/// msg: &'static str,
/// }
/// ```
///
/// This allows compatibility with `std::error::Error` where desired.
///
/// [`Error`]: error
/// [`Display`]: core::fmt::Display