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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/// Define a private namespace for all its items.
mod private
{
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use core::error::Error as ErrorTrait;
/// This trait allows adding extra context or information to an error, creating a tuple of the additional
/// context and the original error. This is particularly useful for error handling when you want to include
/// more details in the error without losing the original error value.
///
/// The `ErrWith` trait provides methods to wrap an error with additional context, either by using a closure
/// that generates the context or by directly providing the context.
///
pub trait ErrWith< ReportErr, ReportOk, E >
{
/// Takes a closure `f` that returns a value of type `ReportErr`, and uses it to wrap an error of type `(ReportErr, E)`
/// in the context of a `Result` of type `ReportOk`.
///
/// This method allows you to add additional context to an error by providing a closure that generates the context.
///
/// # Arguments
///
/// * `f` - A closure that returns the additional context of type `ReportErr`.
///
/// # Returns
///
/// A `Result` of type `ReportOk` if the original result is `Ok`, or a tuple `(ReportErr, E)` containing the additional
/// context and the original error if the original result is `Err`.
///
/// # Errors
///
/// qqq: errors
///
/// # Example
///
/// ```rust
/// use error_tools::ErrWith;
/// let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
/// let result_with_context : Result< (), ( &str, std::io::Error ) > = result.err_with( || "additional context" );
/// ```
fn err_with< F >( self, f : F ) -> core::result::Result< ReportOk, ( ReportErr, E ) >
where
F : FnOnce() -> ReportErr;
/// Takes a reference to a `ReportErr` value and uses it to wrap an error of type `(ReportErr, E)`
/// in the context of a `Result` of type `ReportOk`.
///
/// This method allows you to add additional context to an error by providing a reference to the context.
///
/// # Arguments
///
/// * `report` - A reference to the additional context of type `ReportErr`.
///
/// # Returns
///
/// A `Result` of type `ReportOk` if the original result is `Ok`, or a tuple `(ReportErr, E)` containing the additional
/// context and the original error if the original result is `Err`.
///
/// # Errors
///
/// qqq: Errors
///
/// # Example
///
/// ```rust
/// use error_tools::ErrWith;
/// let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
/// let report = "additional context";
/// let result_with_report : Result< (), ( &str, std::io::Error ) > = result.err_with_report( &report );
/// ```
fn err_with_report( self, report : &ReportErr ) -> core::result::Result< ReportOk, ( ReportErr, E ) >
where
ReportErr : Clone;
}
impl< ReportErr, ReportOk, E, IntoError > ErrWith< ReportErr, ReportOk, E >
for core::result::Result< ReportOk, IntoError >
where
IntoError : Into< E >,
{
#[ allow( clippy::implicit_return, clippy::min_ident_chars ) ]
#[ inline ]
fn err_with< F >( self, f : F ) -> core::result::Result< ReportOk, ( ReportErr, E ) >
where
F : FnOnce() -> ReportErr,
{
self.map_err( | error | ( f(), error.into() ) )
}
#[ inline( always ) ]
#[ allow( clippy::implicit_return ) ]
fn err_with_report( self, report : &ReportErr ) -> core::result::Result< ReportOk, ( ReportErr, E ) >
where
ReportErr : Clone,
Self : Sized,
{
self.map_err( | error | ( report.clone(), error.into() ) )
}
}
/// A type alias for a `Result` that contains an error which is a tuple of a report and an original error.
///
/// This is useful when you want to report additional information along with an error. The `ResultWithReport` type
/// helps in defining such results more concisely.
pub type ResultWithReport< Report, Error > = Result< Report, ( Report, Error ) >;
// ///
// /// Macro to generate an error descriptor.
// ///
// /// ### Basic use-case.
// /// ```rust
// /// # use error_tools::{ BasicError, err };
// /// fn f1() -> BasicError
// /// {
// /// return err!( "No attr" );
// /// }
// /// ```
// ///
//
// #[ macro_export ]
// macro_rules! err
// {
//
// ( $msg : expr ) =>
// {
// $crate::BasicError::new( $msg ).into()
// };
// ( $msg : expr, $( $arg : expr ),+ $(,)? ) =>
// {
// $crate::BasicError::new( format!( $msg, $( $arg ),+ ) ).into()
// };
//
// }
//
// ///
// /// Macro to return an Err( error ) generating error descriptor.
// ///
// /// ### Basic use-case.
// /// ```rust
// /// # use error_tools::{ BasicError, return_err };
// /// fn f1() -> Result< (), BasicError >
// /// {
// /// return_err!( "No attr" );
// /// }
// /// ```
// ///
//
// #[ macro_export ]
// macro_rules! return_err
// {
//
// ( $msg : expr ) =>
// {
// return Result::Err( $crate::err!( $msg ) )
// };
// ( $msg : expr, $( $arg : expr ),+ $(,)? ) =>
// {
// return Result::Err( $crate::err!( $msg, $( $arg ),+ ) )
// };
//
// }
//
// // zzz : review
// // xxx : rid of
//
// /// baic implementation of generic BasicError
//
// #[ derive( core::fmt::Debug, core::clone::Clone, core::cmp::PartialEq, core::cmp::Eq ) ]
// pub struct BasicError
// {
// msg : String,
// }
//
// impl BasicError
// {
// /// Constructor expecting message with description.
// pub fn new< Msg : Into< String > >( msg : Msg ) -> BasicError
// {
// BasicError { msg : msg.into() }
// }
// /// Message with description getter.
// pub fn msg( &self ) -> &String
// {
// &self.msg
// }
// }
//
// impl core::fmt::Display for BasicError
// {
// fn fmt(&self, f: &mut core::fmt::Formatter< '_ >) -> core::fmt::Result
// {
// write!( f, "{}", self.msg )
// }
// }
//
// impl ErrorTrait for BasicError
// {
// fn description( &self ) -> &str
// {
// &self.msg
// }
// }
//
// impl< T > From< BasicError > for Result< T, BasicError >
// {
// /// Returns the argument unchanged.
// #[ inline( always ) ]
// fn from( src : BasicError ) -> Self
// {
// Result::Err( src )
// }
// }
//
// pub use err;
// pub use return_err;
// qqq : write standard mod interface without using mod_interface /* aaa : Dmytro : added to each library file */
}
/// Assertions.
#[ cfg( feature = "enabled" ) ]
pub mod assert;
#[ cfg( feature = "enabled" ) ]
#[ cfg( feature = "error_typed" ) ]
/// Typed exceptions handling mechanism.
pub mod typed;
#[ cfg( feature = "enabled" ) ]
#[ cfg( feature = "error_untyped" ) ]
/// Untyped exceptions handling mechanism.
pub mod untyped;
#[ cfg( feature = "enabled" ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
#[ allow( clippy::pub_use ) ]
pub use own::*;
/// Own namespace of the module.
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod own
{
#[ allow( clippy::wildcard_imports ) ]
use super::*;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use orphan::*;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use assert::orphan::*;
#[ cfg( feature = "error_untyped" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use untyped::orphan::*;
#[ cfg( feature = "error_typed" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use typed::orphan::*;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use private::
{
// err,
// return_err,
ErrorTrait,
// BasicError,
};
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use super::assert;
#[ cfg( feature = "error_typed" ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use super::typed;
#[ cfg( feature = "error_untyped" ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use super::untyped;
}
/// Shared with parent namespace of the module
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod orphan
{
#[ allow( clippy::wildcard_imports ) ]
use super::*;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use exposed::*;
}
/// Exposed namespace of the module.
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod exposed
{
#[ allow( clippy::wildcard_imports ) ]
use super::*;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use prelude::*;
// Expose itself.
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use super::super::error;
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use private::
{
ErrWith,
ResultWithReport,
};
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use assert::exposed::*;
#[ cfg( feature = "error_untyped" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use untyped::exposed::*;
#[ cfg( feature = "error_typed" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use typed::exposed::*;
}
/// Prelude to use essentials: `use my_module::prelude::*`.
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod prelude
{
#[ allow( clippy::wildcard_imports ) ]
use super::*;
// #[ doc( inline ) ]
// pub use private::
// {
// // err,
// // return_err,
// ErrorTrait,
// // BasicError,
// };
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use assert::prelude::*;
#[ cfg( feature = "error_untyped" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use untyped::prelude::*;
#[ cfg( feature = "error_typed" ) ]
#[ doc( inline ) ]
#[ allow( clippy::useless_attribute, clippy::pub_use ) ]
pub use typed::prelude::*;
}