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
//! **Context-rich error handling for Rust with zero-cost abstractions and zero allocations**
//!
//! This is the primary interface for ResExt. It re-exports the proc-macro as well as other helpers
//! provided by ResExt.
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext]
//! enum AppError {
//! Io(std::io::Error),
//! Parse(std::num::ParseIntError),
//! }
//!
//! fn read_config() -> Res<String> {
//! let content = std::fs::read_to_string("config.toml")
//! .context("Failed to read config file")?;
//!
//! let value: i32 = content.trim().parse()
//! .context("Failed to parse config value")?;
//!
//! Ok(content)
//! }
//! ```
//!
//! ---
//!
//! # Proc Macro
//!
//! The proc macro provides clean syntax with full customization:
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext(
//! prefix = "ERROR: ",
//! delimiter = " -> ",
//! include_variant = true,
//! alias = AppResult
//! )]
//! enum MyError {
//! Network(reqwest::Error),
//! Database { error: sqlx::Error },
//! }
//! ```
//!
//! ## Attribute Options
//!
//! - `prefix` - String prepended to entire error message
//! - `suffix` - String appended to entire error message
//! - `msg_prefix` - String prepended to each context message
//! - `msg_suffix` - String appended to each context message
//! - `delimiter` - Separator between context messages (default: " - ")
//! - `source_prefix` - String prepended to source error (default: "Error: ")
//! - `include_variant` - Include variant name in Display output (default: false)
//! - `alias` - Custom type alias name which is used for getting the names for other items generated by the proc-macro (default: `Res`)
//! - `buf_size` - Size for the context message byte buffer (default: 64)
//!
//! ---
//!
//! # Context Methods
//!
//! ## `.context(msg: &str)`
//!
//! Add static context to an error:
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext] enum E { Io(std::io::Error) }
//!
//! std::fs::read("file.txt")
//! .context("Failed to read file")?;
//! Ok::<(), ResErr>(())
//! ```
//!
//! ## `.with_context(args: core::fmt::Arguments<'_>)`
//!
//! Add dynamic context:
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext] enum E { Io(std::io::Error) }
//!
//! let path = "file.txt";
//! std::fs::read(path)
//! .with_context(|| format!("Failed to read {}", path))?;
//! Ok::<(), ResErr>(())
//! ```
//!
//! ## `.or_exit(code: i32)`
//!
//! Print error to Stderr and exit process with given code on error:
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext] enum E { Io(std::io::Error) }
//!
//! fn load_config() -> Res<()> { Ok(()) }
//!
//! let config = load_config().or_exit(1);
//! ```
//!
//! ## `.better_expect(msg: FnOnce() -> impl std::fmt::Display, code: i32)`
//!
//! Like `or_exit` but with custom message:
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext] enum E { Io(std::io::Error) }
//!
//! fn load_critical_data() -> Res<()> { Ok(()) }
//!
//! let data = load_critical_data()
//! .better_expect(|| "FATAL: Cannot start without data", 1);
//! ```
//!
//! ---
//!
//! # Error Display Format
//!
//! Errors are displayed with context chains:
//!
//! ```text
//! Failed to load application
//! - Failed to read config file
//! - Failed to open file
//! Error: No such file or directory
//! ```
//!
//! With `include_variant = true`:
//!
//! ```text
//! Failed to load application
//! - Failed to read config file
//! Error: Io: No such file or directory
//! ```
//!
//! ---
//!
//! # Examples
//!
//! ## Basic Error Handling
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext]
//! enum ConfigError {
//! Io(std::io::Error),
//! Parse(toml::de::Error),
//! }
//!
//! fn load_config(path: &str) -> Res<Config> {
//! let content = std::fs::read_to_string(path)
//! .context("Failed to read config")?;
//!
//! toml::from_str(&content)
//! .with_context(format_args!("Failed to parse {}", path))
//! }
//! ```
//!
//! ## Multiple Error Types
//!
//! ```rust,ignore
//! use resext::resext;
//!
//! #[resext(alias = ApiResult)]
//! enum ApiError {
//! Network(reqwest::Error),
//! Database(sqlx::Error),
//! Json(serde_json::Error),
//! }
//!
//! async fn fetch_user(id: u64) -> ApiResult<User> {
//! let response = reqwest::get(format!("/users/{}", id))
//! .await
//! .context("Failed to fetch user")?;
//!
//! let user = response.json()
//! .await
//! .context("Failed to parse user data")?;
//!
//! Ok(user)
//! }
//! ```
//!
pub use resext;
/// Panic with message if `condition` is true
///
/// Accepts a message as any type that implements `std::fmt::Display`
///
/// ## Examples
///
/// ```rust,ignore
/// use resext::panic_if;
///
/// let x = 5;
///
/// panic_if!(x > 10, "x is too big", 1);
/// panic_if!(x > 10, format!("x={} is too big", x), 1);
/// ```