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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/// Provides macros for grouping errors and generating automatic
/// conversions.
// `StdError` is referenced from generated code in the macro body.
use Error as StdError;
/// Macro for composing multi-error enums with automatic
/// `From<OtherError>` conversions and full [`ForgeError`] delegation.
///
/// Every variant must wrap exactly one source type that itself
/// implements [`ForgeError`]. The macro generates:
///
/// - the enum declaration,
/// - `Display` and `Error` implementations that forward to the
/// wrapped source,
/// - `From<T>` for each wrapped type,
/// - a [`ForgeError`] impl whose methods delegate directly to the
/// wrapped source's `ForgeError` methods (no type-erased
/// downcast, no fallback values).
///
/// # Example
///
/// ```
/// use error_forge::{group, AppError};
///
/// // `AppError` already implements `ForgeError`, so it can be
/// // wrapped directly. Other types you wrap with `group!` must
/// // also implement `ForgeError` (see the next section).
/// group! {
/// #[derive(Debug)]
/// pub enum ServiceError {
/// App(AppError),
/// }
/// }
///
/// // `From<AppError>` is generated, so `?` works against any
/// // function that returns an `AppError`.
/// let _err: ServiceError = AppError::config("missing").into();
/// ```
///
/// # `ForgeError` requirement
///
/// Each wrapped source type must implement [`ForgeError`].
/// `#[derive(ModError)]` (the `derive` feature) implements it for
/// you. [`define_errors!`] does **not**: it generates inherent
/// methods with the same names (`kind`, `caption`, `is_retryable`,
/// `is_fatal`, `status_code`, `exit_code`) but no trait impl, so a
/// `define_errors!` enum needs a short delegating impl before it can
/// be grouped. The same applies to a foreign type such as
/// `std::io::Error`: wrap it in a variant of an enum that implements
/// [`ForgeError`] and group that enum.
///
/// ```
/// use error_forge::{define_errors, group, AppError, ForgeError};
///
/// define_errors! {
/// pub enum FsError {
/// #[error(display = "Could not write {path}", path)]
/// #[kind(Filesystem, status = 500)]
/// WriteFailed { path: String },
/// }
/// }
///
/// // `FsError::kind(self)` resolves to the inherent method the
/// // macro generated, not back to this trait method.
/// impl ForgeError for FsError {
/// fn kind(&self) -> &'static str { FsError::kind(self) }
/// fn caption(&self) -> &'static str { FsError::caption(self) }
/// fn is_retryable(&self) -> bool { FsError::is_retryable(self) }
/// fn is_fatal(&self) -> bool { FsError::is_fatal(self) }
/// fn status_code(&self) -> u16 { FsError::status_code(self) }
/// fn exit_code(&self) -> i32 { FsError::exit_code(self) }
/// }
///
/// group! {
/// #[derive(Debug)]
/// pub enum ServiceError {
/// App(AppError),
/// Fs(FsError),
/// }
/// }
///
/// let err: ServiceError = FsError::writefailed("out.log".to_string()).into();
/// assert_eq!(err.to_string(), "Could not write out.log");
/// assert_eq!(err.kind(), "Filesystem");
/// assert_eq!(err.status_code(), 500);
/// ```
///
/// This is a **breaking change from `0.9.x`**, where `group!`
/// accepted any wrapped type but the resulting `ForgeError` impl
/// was silently incorrect — every method returned default
/// fallback values regardless of the wrapped type. The `1.0`
/// rewrite removes the type-erased downcast and trades it for a
/// trait bound the compiler can verify.
///
/// [`ForgeError`]: crate::error::ForgeError
/// [`define_errors!`]: crate::define_errors
) => ;
}