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
/// Internal namespace.
mod private
{
  pub use std::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`.
    ///
    /// # 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 ) -> std::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`.
    ///
    /// # 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 ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
    where
      ReportErr : Clone;

  }

  impl< ReportErr, ReportOk, E, IntoError > ErrWith< ReportErr, ReportOk, E >
  for std::result::Result< ReportOk, IntoError >
  where
    IntoError : Into< E >,
  {

    fn err_with< F >( self, f : F ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
    where
      F : FnOnce() -> ReportErr,
    {
      self.map_err( | e | ( f(), e.into() ) )
    }

    #[ inline( always ) ]
    fn err_with_report( self, report : &ReportErr ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
    where
      ReportErr : Clone,
      Self : Sized,
    {
      self.map_err( | e | ( report.clone(), e.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

  /// 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 */
}

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use own::*;

/// Own namespace of the module.
#[ allow( unused_imports ) ]
pub mod own
{
  use super::*;
  #[ doc( inline ) ]
  pub use orphan::*;
}

/// Shared with parent namespace of the module
#[ allow( unused_imports ) ]
pub mod orphan
{
  use super::*;
  #[ doc( inline ) ]
  pub use exposed::*;
}

/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed
{
  use super::*;

  #[ doc( inline ) ]
  pub use private::
  {
    ErrWith,
    ResultWithReport,
  };

  #[ doc( inline ) ]
  pub use prelude::*;
}

/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude
{
  use super::*;

  #[ doc( inline ) ]
  pub use private::
  {
    err,
    return_err,
    ErrorTrait,
    BasicError,
  };

}