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
//! # Ergonomic result / error handling helpers built on anyhow and thiserror.
//!
//! Related docs:
//! - [Github: okerr](https://github.com/nicolab/okerr)
//! - [Docs.rs: anyhow](https://docs.rs/anyhow/latest/anyhow/)
//! - [Docs.rs: thiserror](https://docs.rs/thiserror/latest/thiserror/)
//!
//! This crate is a re-export of `anyhow` and aliase of`thiserror`.
//!
//! - It provides a `Result` type that is `anyhow::Result`
//! and `okerr::derive::Error` is an alias of `thiserror::Error`.
//! - NOTE: `okerr::derive::Error` requieres the `thiserror` dependency to be added to your `Cargo.toml` (`cargo add thiserror`).
//! - It also provides a `err!` macro that is a shorthand for `Err(anyhow::anyhow!(...))` or `Err(okerr::anyerr!(...))`.
//! - It also provides a `fail!` macro that is `anyhow::bail!`.
//! - It also provides a `anyerr!` macro that is `anyhow::anyhow!`.
//! - All "anyhow" is also available as "okerr" aliases (Context, ensure!, format_err!, etc.).
//!
//! # Example
//!
//! ```
//! use okerr::{Result, err, fail, anyerr};
//!
//! fn divide(a: i32, b: i32) -> Result<i32> {
//! if b == 0 {
//! err!("Cannot divide by zero")
//! } else {
//! Ok(a / b)
//! }
//! }
//!
//! fn maybe_fail(should_fail: bool) -> Result<String> {
//! if should_fail {
//! fail!("Oops!");
//! }
//!
//! Ok("No error".to_string())
//! }
//!
//! fn main() {
//! let result = divide(10, 2);
//! assert!(result.is_ok());
//! assert_eq!(result.unwrap(), 5);
//!
//! let result = divide(10, 0);
//! assert!(result.is_err());
//! assert_eq!(result.unwrap_err().to_string(), "Cannot divide by zero");
//!
//! // fail! does early return
//! let result = maybe_fail(true);
//! assert!(result.is_err());
//! assert_eq!(result.unwrap_err().to_string(), "Oops!");
//!
//! // No error
//! let result = maybe_fail(false);
//! assert!(result.is_ok());
//! assert_eq!(result.unwrap(), "No error");
//!
//! // Same as anyhow!(...).
//! // Creates an Error directly,
//! // from a string or any std::error::Error
//! let error = anyerr!("Oops!");
//! assert_eq!(error.to_string(), "Oops!");
//! }
//! ```
//!
//! # Example with `okerr::derive::Error`
//!
//! ```
//! use okerr::{Result, err, derive::Error};
//!
//! #[derive(Error, Debug)]
//! enum MyError {
//! #[error("Cannot divide by zero")]
//! DivideByZero,
//! #[error("Cannot divide by {0}")]
//! DivideBy(i32),
//! }
//!
//! fn divide(a: i32, b: i32) -> Result<i32> {
//! if b == 0 {
//! err!(MyError::DivideByZero)
//! } else if b < 0 {
//! err!(MyError::DivideBy(b))
//! } else {
//! Ok(a / b)
//! }
//! }
//!
//! fn main() {
//! let result = divide(10, 2);
//! assert!(result.is_ok());
//! assert_eq!(result.unwrap(), 5);
//!
//! let result = divide(10, 0);
//! assert!(result.is_err());
//! assert_eq!(result.unwrap_err().to_string(), "Cannot divide by zero");
//!
//! let result = divide(10, -2);
//! assert!(result.is_err());
//! assert_eq!(result.unwrap_err().to_string(), "Cannot divide by -2");
//! }
//! ```
//!
//! It's very simple, very lightweight
//! (just a few lines of code in the `okerr` crate), no overhead, no abstraction cost.
//! `okerr` provides consistency and a excellent DX. 100% compatible with `anyhow` and `thiserror`, convert easily error from a boxed error (like eyre::Report and others).
pub use ;
/// Sugar for thiserror::Error.
/// `okerr::derive::Error` is an alias of `thiserror::Error`.
/// - https://docs.rs/thiserror/latest/thiserror/
/// NOTE: requieres the `thiserror` dependency.
/// Same as `anyhow!` (and its alias: `format_err!`).
/// - [Docs.rs: macro anyhow!](https://docs.rs/anyhow/latest/anyhow/macro.anyhow.html)
/// Shorthand for `Err(anyerr!(...))` or `Err(anyhow!(...))`.
/// - [Docs.rs: macro anyhow!](https://docs.rs/anyhow/latest/anyhow/macro.anyhow.html)
/// Same as `anyhow::bail!`.
/// - [Docs.rs: macro bail!](https://docs.rs/anyhow/latest/anyhow/macro.bail.html)
/// Convert a boxed error into an okerr/anyhow Error.
///
/// # Example:
/// ```
/// use okerr::{Result, from_boxed_error};
///
/// fn returns_eyre_error() -> eyre::Result<i32> {
/// Err(eyre::eyre!("eyre error"))
/// }
///
/// fn convert_eyre() -> Result<i32> {
/// returns_eyre_error().map_err(|e| from_boxed_error(e.into()))
/// }
///
/// let result = convert_eyre();
/// assert!(result.is_err());
/// assert!(result.unwrap_err().to_string().contains("eyre error"));
/// ```
/// Wrap a Result into an okerr/anyhow Error.
///
/// Equivalent to `result.map_err(okerr::Error::new)`