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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Comprehensive error handling library with detailed backtrace tracking.
//!
//! `error2` provides enhanced error handling capabilities for Rust applications,
//! focusing on detailed error propagation tracking and ergonomic error conversion.
//!
//! # Features
//!
//! - **Backtrace Tracking** - Automatically capture error creation location; manually record propagation with `.attach()`
//! - **Error Chaining** - Chain errors from different libraries while preserving context
//! - **Derive Macro** - `#[derive(Error2)]` for easy error type creation
//! - **Type Conversion** - `Result<T, E1> -> Result<T, E2>`, `Option<T> -> Result<T, E>` with `.context()`
//! - **Type Erasure** - `BoxedError2` for anyhow-like ergonomics
//!
//! # Quick Start
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! error2 = "0.13.2"
//! ```
//!
//! Define your error types:
//!
//! ```
//! use std::io;
//!
//! use error2::prelude::*;
//!
//! #[derive(Debug, Error2)]
//! pub enum MyError {
//! #[error2(display("IO error: {source}"))]
//! Io {
//! source: io::Error,
//! backtrace: Backtrace,
//! },
//!
//! #[error2(display("not found: {key}"))]
//! NotFound { key: String, backtrace: Backtrace },
//! }
//! ```
//!
//! Use in your functions:
//!
//! ```
//! # use error2::prelude::*;
//! # use std::io;
//! # #[derive(Debug, Error2)]
//! # pub enum MyError {
//! # #[error2(display("IO error"))]
//! # Io { source: io::Error, backtrace: Backtrace },
//! # #[error2(display("not found: {key}"))]
//! # NotFound { key: String, backtrace: Backtrace },
//! # }
//! fn read_config(path: &str) -> Result<String, MyError> {
//! // Convert io::Error to MyError::Io
//! let content = std::fs::read_to_string(path).context(Io2)?;
//!
//! // Convert Option to Result
//! let value = content
//! .lines()
//! .next()
//! .context(NotFound2 { key: "first line" })?;
//!
//! Ok(value.to_string())
//! }
//! ```
//!
//! # Three Error Patterns
//!
//! Error2 supports three types of errors based on their field structure:
//!
//! ## 1. Root Error (New Error Origin)
//!
//! Use when creating a new error (not wrapping another):
//!
//! ```
//! # use error2::prelude::*;
//! #[derive(Debug, Error2)]
//! pub enum AppError {
//! #[error2(display("invalid ID: {id}"))]
//! InvalidId {
//! id: i64,
//! backtrace: Backtrace, // Only backtrace, no source
//! },
//! }
//! ```
//!
//! ## 2. Std Error (Wrapping std::error::Error)
//!
//! Use when wrapping standard library or third-party errors:
//!
//! ```
//! # use error2::prelude::*;
//! # use std::io;
//! #[derive(Debug, Error2)]
//! pub enum AppError {
//! #[error2(display("file error"))]
//! FileError {
//! source: io::Error, // Wrapped error
//! backtrace: Backtrace, // New backtrace
//! },
//! }
//! ```
//!
//! ## 3. Error2 Error (Chaining Error2 Types)
//!
//! Use when wrapping another Error2 type (reuses backtrace):
//!
//! ```
//! # use error2::prelude::*;
//! # #[derive(Debug, Error2)]
//! # #[error2(display("config error"))]
//! # pub struct ConfigError { backtrace: Backtrace }
//! #[derive(Debug, Error2)]
//! pub enum AppError {
//! #[error2(display("configuration failed"))]
//! Config {
//! source: ConfigError, // Only source, backtrace reused
//! },
//! }
//! ```
//!
//! # Core Traits
//!
//! - [`Error2`] - Extends `std::error::Error` with backtrace support
//! - [`Context`] - Type conversion: `Result<T, Source> -> Result<T, Target>`, `Option<T> -> Result<T, E>`
//! - [`Attach`] - Record error propagation locations
//! - [`RootError`] - Convenience methods for creating root errors
//!
//! # Type Erasure
//!
//! [`BoxedError2`] provides anyhow-like ergonomics:
//!
//! ```
//! use error2::prelude::*;
//!
//! fn do_something() -> Result<(), BoxedError2> {
//! std::fs::read_to_string("file.txt").context(ViaStd)?; // Convert to BoxedError2
//! Ok(())
//! }
//! ```
//!
//! # Location Tracking
//!
//! Use `.attach()` to record error propagation:
//!
//! ```
//! # use error2::prelude::*;
//! # use std::io;
//! # #[derive(Debug, Error2)]
//! # #[error2(display("error"))]
//! # struct MyError { source: io::Error, backtrace: Backtrace }
//! # fn inner() -> Result<(), MyError> { Ok(()) }
//! fn outer() -> Result<(), MyError> {
//! let result = inner().attach()?; // Records this location
//! Ok(result)
//! }
//! ```
//!
//! The backtrace shows multiple locations:
//!
//! ```
//! # use error2::prelude::*;
//! # use std::io;
//! # #[derive(Debug, Error2)]
//! # #[error2(display("error"))]
//! # struct MyError { source: io::Error, backtrace: Backtrace }
//! # fn inner() -> Result<(), MyError> {
//! # let err = io::Error::new(io::ErrorKind::NotFound, "not found");
//! # Err(err).context(MyError2)
//! # }
//! # fn outer() -> Result<(), MyError> { inner().attach() }
//! # fn main() {
//! use regex::Regex;
//!
//! if let Err(e) = outer() {
//! let msg = e.backtrace().error_message();
//!
//! // Full error format with multiple locations:
//! // MyError: error
//! // at /path/to/file.rs:496:14
//! // at /path/to/file.rs:498:45
//! // std::io::error::Error: not found
//!
//! let re = Regex::new(concat!(
//! r"(?s)^.+MyError: error",
//! r"\n at .+\.rs:\d+:\d+",
//! r"\n at .+\.rs:\d+:\d+",
//! r"\nstd::io::error::Error: not found$",
//! ))
//! .unwrap();
//! assert!(re.is_match(msg.as_ref()));
//! }
//! # }
//! ```
/// Attach adapters for iterators, futures, and streams.
/// Error kind enum for downcasting [`BoxedError2`].
///
/// See [`ErrorKind`](kind::ErrorKind) for details.
/// Internal transformation traits (not for direct use).
/// Re-exports of commonly used types and traits.
///
/// Import with `use error2::prelude::*;` to get:
/// - [`Error2`] trait
/// - [`Context`], [`Attach`], [`RootError`] traits
/// - [`Backtrace`], [`BoxedError2`] types
/// - [`ViaRoot`], [`ViaStd`], [`ViaErr2`] wrappers
/// - `#[derive(Error2)]` macro (if `derive` feature enabled)
pub use Error2;
pub use ;
pub use ;
pub
Sized>