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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use builder::*;
use context::*;
use core::fmt::{self, Debug, Display, Write};
use std::{
    any::TypeId,
    collections::HashSet,
    error::{self, Error},
};

#[cfg(all(feature = "debug_strategy_disabled", feature = "debug_strategy_full"))]
compile_error!(
    "features `debug_strategy_disabled` and `debug_strategy_full` are mutually exclusive"
);

pub use ext::OofExt;

/// Place above `fn` or `impl` to generate and inject context to `?` operators.
pub use oofs_derive::oofs;

/// Create a custom error `Oof` similar to `anyhow!`
///
/// You can format the error just like you do for `println!` and `anyhow!`.
///
/// Ex)
/// ```rust
/// # use oofs::{Oof, oofs, oof};
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// return oof!("custom error {}", "failure").into_res();
/// # }
/// ```
///
/// [Oof::into_res()](struct.Oof.html#method.into_res) wraps `Oof` in `Result::Err(_)`, so you can return it directly.
///
/// Since the macro returns `Oof`, you can chain methods like `tag` and `attach`.
///
/// Ex)
/// ```rust
/// # use oofs::{Oof, oofs, oof};
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// struct MyTag;
///
/// let x = 123usize;
///
/// return oof!("custom error {}", "failure").tag::<MyTag>().attach(x).into_res();
/// # }
/// ```
#[macro_export]
macro_rules! oof {
    ($($arg:tt)*) => {
        $crate::Oof::custom(format!($($arg)*))
    };
}

/// Check that a given expression evaluates to `true`, else return an error.
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// ensure!(false);
/// # Ok(())
/// # }
/// ```
///
/// First parameter is an expression that evaluates to `bool`.
/// If the expression evaluates to `false`, the macro will return `Err(Oof)`.
///
/// Second parameter is `context(...)`, and is an optional second parameter.
/// You can use this if you want to display your own context message, instead of the default `assertion failed: EXPRESSION at LOCATION`.
/// Inside `context(...)`, you can write as you do for `println!`.
///
/// Other optional parameters are for tagging, attach, and attach_lazy.
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// struct MyTag;
/// struct OtherTag;
///
/// let x = 123usize;
/// let y = "some value";
/// let z = "lazy attachment";
///
/// ensure!(
///   false,
///   context("custom context with value {:?}", x),
///   tag: [MyTag, OtherTag],
///   attach: [&y, "attachment", Instant::now()],
///   attach_lazy: [|| format!("context {}", &z)]
/// );
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! ensure {
    ($cond:expr, context($($arg:tt)*) $(, $($t:tt)*)?) => {
        $crate::ensure!(@internal $cond, $crate::oof!($($arg)*), $($($t)*)?);
    };
    ($cond:expr $(, $($t:tt)*)?) => {
        $crate::ensure!($cond, context("assertion failed: `{}`", stringify!($cond)), $($($t)*)?);
    };
    (@internal $cond:expr, $ret:expr, tag: [$($tag:ty),* $(,)?] $(, $($t:tt)*)?) => {
        $crate::ensure!(@internal $cond, $ret $(.tag::<$tag>())*, $($($t)*)?);
    };
    (@internal $cond:expr, $ret:expr, attach: [$($a:expr),* $(,)?] $(, $($t:tt)*)?) => {
        $crate::ensure!(@internal $cond, $ret $(.attach($a))*, $($($t)*)?);
    };
    (@internal $cond:expr, $ret:expr, attach_lazy: [$($l:expr),* $(,)?] $(, $($t:tt)*)?) => {
        $crate::ensure!(@internal $cond, $ret $(.attach_lazy($l))*, $($($t)*)?);
    };
    (@internal $cond:expr, $ret:expr, ) => {
        if !$cond {
            return $ret.into_res();
        }
    };
}

/// Check that two given expressions are same, else return an error.
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// ensure_eq!(1u8, 2u8);
/// # Ok(())
/// # }
/// ```
///
/// First two parameters are parameters to be compared.
/// If the parameters are not same, the macro will return `Err(Oof)`.
///
/// Third parameter is `context(...)`, and is an optional third parameter.
/// You can use this if you want to display your own context message, instead of the default `assertion failed: (left == right) at LOCATION`.
/// Inside `context(...)`, you can write as you do for `println!`.
///
/// Other optional parameters are for tagging, attach, and attach_lazy.
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// struct MyTag;
/// struct OtherTag;
///
/// let x = 123usize;
/// let y = "some value";
/// let z = "lazy attachment";
///
/// ensure_eq!(
///   1u8, 2u8,
///   context("custom context with value {:?}", x),
///   tag: [MyTag, OtherTag],
///   attach: [&y, "attachment", Instant::now()],
///   attach_lazy: [|| format!("context {}", &z)]
/// );
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! ensure_eq {
    ($l:expr, $r:expr, context($($c:tt)*) $(, $($t:tt)*)?) => {
        match (&$l, &$r) {
            (left, right) => {
                $crate::ensure!(*left == *right, context($($c)*), $($($t)*)?);
            }
        }
    };
    ($l:expr, $r:expr $(, $($t:tt)*)?) => {
        match (&$l, &$r) {
            (left, right) => {
                $crate::ensure!(
                    *left == *right,
                    context("assertion failed: `(left == right)`"),
                    attach_lazy: [
                        || format!(" left: {:?}", &*left),
                        || format!("right: {:?}", &*right)
                    ],
                    $($($t)*)?
                );
            }
        }
    };
}

/// Wraps a custom error with `Oof`
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// return wrap_err(std::io::Error::new(std::io::ErrorKind::Other, "Some Error")).into_res();
/// # Ok(())
/// # }
/// ```
///
/// Since `wrap_err(_)` returns `Oof`, you can chain methods like `tag` and `attach`.
///
/// Ex)
/// ```rust
/// # use oofs::*;
/// # use std::time::Instant;
/// # #[oofs]
/// # fn _ex() -> Result<(), Oof> {
/// struct MyTag;
/// let x = 123u8;
///
/// return wrap_err(std::io::Error::new(std::io::ErrorKind::Other, "Some Error"))
///     .tag::<MyTag>()
///     .attach(x)
///     .into_res();
/// # Ok(())
/// # }
/// ```
#[cfg_attr(feature = "location", track_caller)]
pub fn wrap_err(e: impl 'static + Send + Sync + Error) -> Oof {
    Oof::builder().with_source(e).build()
}

/// Error type for oofs.
///
/// `Oof` implements `std::error::Error`.
pub struct Oof {
    source: Option<Box<dyn 'static + Send + Sync + Error>>,
    context: Box<Context>,
    tags: HashSet<TypeId>,
    attachments: Vec<String>,
    #[cfg(feature = "location")]
    location: Location,
}

impl Display for Oof {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let context = self.context.as_ref();

        write!(f, "{context}")?;

        #[cfg(feature = "location")]
        write!(f, " at `{}`", self.location)?;

        if matches!(context, Context::Generated(_)) || !self.attachments.is_empty() {
            writeln!(f)?;
        }

        if let Context::Generated(c) = context {
            c.fmt_args(f)?;
        }

        if !self.attachments.is_empty() {
            writeln!(f, "\nAttachments:")?;
            for (i, a) in self.attachments.iter().enumerate() {
                let mut indented = Indented {
                    inner: f,
                    number: Some(i),
                    started: false,
                };

                write!(indented, "{}", a)?;
                writeln!(f)?;
            }
        }

        Ok(())
    }
}

impl Debug for Oof {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            #[cfg(not(feature = "location"))]
            let debug = f
                .debug_struct("Oof")
                .field("context", &self.context)
                .field("source", &self.source)
                .field("tags", &self.tags)
                .field("attachments", &self.attachments)
                .finish();

            #[cfg(feature = "location")]
            let debug = f
                .debug_struct("Oof")
                .field("context", &self.context)
                .field("source", &self.source)
                .field("location", &self.location)
                .field("tags", &self.tags)
                .field("attachments", &self.attachments)
                .finish();

            return debug;
        }

        write!(f, "{self}")?;

        if let Some(cause) = self.source() {
            write!(f, "\nCaused by:")?;

            let multiple = cause.source().is_some();
            for (n, error) in chain::Chain::new(cause).enumerate() {
                writeln!(f)?;

                let mut indented = Indented {
                    inner: f,
                    number: if multiple { Some(n) } else { None },
                    started: false,
                };

                write!(indented, "{error}")?;
            }
        }

        Ok(())
    }
}

impl error::Error for Oof {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        if let Some(e) = &self.source {
            Some(e.as_ref())
        } else {
            None
        }
    }
}

impl Oof {
    /// Create a new `Oof` with custom context message.
    ///
    /// You can also use [oof!(...)](oof).
    #[cfg_attr(feature = "location", track_caller)]
    pub fn custom(message: String) -> Oof {
        Self::builder().with_custom(message).build()
    }

    #[cfg_attr(feature = "location", track_caller)]
    fn builder() -> OofBuilder {
        OofBuilder::new()
    }

    /// Lists all tags as [TypeId](https://doc.rust-lang.org/1.64.0/core/any/struct.TypeId.html).
    ///
    /// You can also use [oof!(...)](oof).
    pub fn tags(&self) -> impl Iterator<Item = &TypeId> {
        self.tags.iter()
    }

    /// Check if this `Oof` is tagged as given type.
    ///
    /// This method only checks one level deep.
    /// To check all nested errors, use [Oof::tagged_nested](struct.Oof.html#method.tagged_nested).
    pub fn tagged<T: 'static>(&self) -> bool {
        self.tags.contains(&TypeId::of::<T>())
    }

    /// Check if this `Oof` is tagged in all nested errors.
    ///
    /// This method checks all levels.
    pub fn tagged_nested<T: 'static>(&self) -> bool {
        if self.tagged::<T>() {
            return true;
        }

        for cause in chain::Chain::new(self).skip(1) {
            if let Some(e) = cause.downcast_ref::<Oof>() {
                if e.tagged::<T>() {
                    return true;
                }
            }
        }

        false
    }

    /// Check if this `Oof` is tagged in all nested errors in reverse order.
    ///
    /// This method checks all levels.
    pub fn tagged_nested_rev<T: 'static>(&self) -> bool {
        for cause in chain::Chain::new(self).skip(1).rev() {
            if let Some(e) = cause.downcast_ref::<Oof>() {
                if e.tagged::<T>() {
                    return true;
                }
            }
        }

        if self.tagged::<T>() {
            return true;
        }

        false
    }

    /// Tag `Oof` with type and return Self.
    pub fn tag<T: 'static>(mut self) -> Self {
        self.tags.insert(TypeId::of::<T>());
        self
    }

    /// Tag `Oof` if given closure returns `true` and return Self.
    pub fn tag_if<Tag, F>(self, f: F) -> Self
    where
        Tag: 'static,
        F: FnOnce(&Box<dyn 'static + Send + Sync + Error>) -> bool,
    {
        if let Some(source) = self.source.as_ref() {
            if f(&source) {
                return self.tag::<Tag>();
            }
        }

        self
    }

    /// Attach any value that implements `std::fmt::Debug`.
    ///
    /// This attached value will be listed as attachments in the displayed error.
    ///
    /// Ex)
    /// ```rust
    /// use oofs::{oof, oofs};
    /// # use oofs::Oof;
    ///
    /// # #[oofs]
    /// # fn _ex() -> Result<(), Oof> {
    /// let x = 123u8;
    ///
    /// return oof!("custom error")
    ///     .attach(x)
    ///     .attach("some attachment")
    ///     .into_res();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Above example will output:
    ///
    /// ```text
    /// custom error at `oofs/tests/basic.rs:9:13`
    ///
    /// Attachments:
    ///    0: 123
    ///    1: "some attachment"
    /// ```
    pub fn attach<D: fmt::Debug>(mut self, debuggable: D) -> Self {
        self.attachments.push(format!("{debuggable:?}"));
        self
    }

    /// Lazily load and attach any value that implements `ToString`.
    ///
    /// This attached value will be listed as attachments in the displayed error.
    ///
    /// Ex)
    /// ```rust
    /// use oofs::{oof, oofs};
    /// # use oofs::Oof;
    ///
    /// # #[oofs]
    /// # fn _ex() -> Result<(), Oof> {
    ///
    /// return oof!("custom error")
    ///     .attach_lazy(|| "some attachment")
    ///     .into_res();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Above example will output:
    ///
    /// ```text
    /// custom error at `oofs/tests/basic.rs:9:13`
    ///
    /// Attachments:
    ///    0: "some attachment"
    /// ```
    pub fn attach_lazy<D: ToString, F: FnOnce() -> D>(mut self, f: F) -> Self {
        self.attachments.push(f().to_string());
        self
    }

    /// Wraps `Oof` in `Result::Err(_)`.
    ///
    /// Use it to easily return an `Oof` instead of manually wrapping it in `Err(_)`.
    pub fn into_res<T, E>(self) -> Result<T, E>
    where
        E: From<Self>,
    {
        Err(self.into())
    }
}

mod builder;
mod chain;
mod context;
mod ext;
mod tsa;

/// Module by attribute `#[oofs]`
pub mod __used_by_attribute {
    pub use crate::{builder::*, context::*, tsa::*};

    pub const DEBUG_OWNED: bool = cfg!(all(
        not(feature = "debug_strategy_disabled"),
        any(debug_assertions, feature = "debug_strategy_full")
    ));
}