rootcause 0.12.1

A flexible, ergonomic, and inspectable error reporting library for Rust
Documentation
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Extension traits for `Option` types to integrate with rootcause error
//! reporting.
//!
//! This module provides the [`OptionExt`] trait, which adds error handling
//! methods to `Option` types. When an `Option` is `None`, these methods
//! automatically create a [`Report`] containing a [`NoneError`] that captures
//! type information about the expected value.
//!
//! # Quick Start
//!
//! ```
//! use rootcause::{option_ext::OptionExt, prelude::*};
//!
//! fn get_config_value() -> Option<String> {
//!     None
//! }
//!
//! # fn example() -> Result<String, Report<&'static str>> {
//! // Convert None to a Report
//! let result = get_config_value().context("Failed to load configuration")?;
//! # Ok(result)
//! # }
//! ```
//!
//! # Available Methods
//!
//! The [`OptionExt`] trait provides methods similar to
//! [`ResultExt`](crate::prelude::ResultExt), including:
//!
//! - **[`ok_or_report()`](OptionExt::ok_or_report)** - Convert `None` to
//!   `Report<NoneError>`
//! - **[`context()`](OptionExt::context)** - Add context when `None`
//! - **Local variants** - `local_*` methods for non-`Send + Sync` types
//!
//! # Thread Safety
//!
//! All methods have both thread-safe (`Send + Sync`) and local
//! (non-thread-safe) variants. Use the `local_*` methods when working with
//! types that cannot be sent across threads, such as `Rc` or `Cell`.
//!
//! # Usage Considerations
//!
//! Some developers prefer to keep `Option` and `Result` handling visually
//! distinct in their code. Using [`OptionExt`] can make it less obvious when
//! you're working with an `Option` versus a `Result`, since both can use
//! similar error handling methods like `.context()`. If code clarity is a
//! concern, consider using explicit conversion with
//! [`.ok_or_report()`](OptionExt::ok_or_report) followed by standard `Result`
//! methods, or using [`Option::ok_or`] or [`Option::ok_or_else`] with your own
//! error types.

use crate::{
    Report, ReportConversion, handlers,
    markers::{Local, Mutable, SendSync},
};

/// Error type representing a missing `Option` value.
///
/// This error is automatically created when using [`OptionExt`] methods on a
/// `None` value. It captures the type name of the expected value for
/// better debugging.
///
/// # Examples
///
/// ```
/// use rootcause::option_ext::NoneError;
///
/// let error = NoneError::new::<String>();
/// assert!(format!("{error}").contains("String"));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoneError {
    /// The type name of the expected value.
    type_name: &'static str,
}

impl NoneError {
    /// Creates a new `NoneError` for the given type.
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::option_ext::NoneError;
    ///
    /// let error = NoneError::new::<String>();
    /// let message = format!("{error}");
    /// assert!(message.contains("String"));
    /// ```
    #[must_use]
    pub fn new<T: ?Sized>() -> Self {
        Self {
            type_name: core::any::type_name::<T>(),
        }
    }

    #[must_use]
    #[track_caller]
    fn new_sendsync_report<T: ?Sized>() -> Report<NoneError, Mutable, SendSync> {
        Report::new(Self::new::<T>())
    }

    #[must_use]
    #[track_caller]
    fn new_local_report<T: ?Sized>() -> Report<NoneError, Mutable, Local> {
        Report::new_local(Self::new::<T>())
    }
}

impl core::fmt::Display for NoneError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "Expected value of type {}, but found None",
            self.type_name
        )
    }
}

impl core::error::Error for NoneError {}

/// Extension trait for `Option` that provides error handling and reporting
/// functionality.
///
/// This trait adds methods to `Option` types that allow you to convert `None`
/// values into [`Report`]s with additional context and attachments. It provides
/// both thread-safe (`Send + Sync`) and local-only variants of each method.
///
/// When a `None` value is encountered, a [`NoneError`] is automatically
/// created to represent the missing value, capturing type information for
/// better debugging.
///
/// The methods in this trait fall into several categories:
///
/// - **Converting to reports**: [`ok_or_report`](OptionExt::ok_or_report)
///   converts `None` into a [`Report<NoneError>`]
/// - **Adding context**: [`context`](OptionExt::context),
///   [`context_with`](OptionExt::context_with), and variants add a new context
///   layer when the option is `None`
///
/// Each context method has a `local_*` variant for working with types that are
/// not `Send + Sync`.
///
/// # Examples
///
/// ```
/// use rootcause::option_ext::OptionExt;
///
/// let value: Option<String> = None;
/// let result = value.ok_or_report();
/// assert!(result.is_err());
/// ```
pub trait OptionExt<V> {
    /// Converts `None` into a [`Report<NoneError>`].
    ///
    /// If the option is `Some`, returns the value. If the option is `None`,
    /// creates a [`Report`] containing a [`NoneError`] that captures the
    /// type information of the expected value.
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::{
    ///     option_ext::{NoneError, OptionExt},
    ///     prelude::*,
    /// };
    ///
    /// let value: Option<String> = None;
    /// let result: Result<String, Report<NoneError>> = value.ok_or_report();
    /// assert!(result.is_err());
    /// ```
    #[track_caller]
    fn ok_or_report(self) -> Result<V, Report<NoneError, Mutable, SendSync>>;

    /// Converts `None` into a new [`Report`] using the provided context. The
    /// [`NoneError`] is set as a child of the new [`Report`].
    ///
    /// If the option is `Some`, returns the value unchanged. If the option is
    /// `None`, creates a [`NoneError`] and adds the provided context as
    /// the primary error message, with the [`NoneError`] becoming part of
    /// the error chain.
    ///
    /// See also [`local_context`](OptionExt::local_context) for a
    /// non-thread-safe version that works with types that are not
    /// `Send + Sync`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::{option_ext::OptionExt, prelude::*};
    ///
    /// fn get_user_id() -> Option<u64> {
    ///     None
    /// }
    ///
    /// let result: Result<u64, Report<&str>> = get_user_id().context("Failed to get user ID");
    /// ```
    #[track_caller]
    fn context<C>(self, context: C) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: Send + Sync + core::fmt::Display + core::fmt::Debug;

    /// Converts `None` into a new [`Report`] using context generated by the
    /// provided closure. The [`NoneError`] is set as a child of the new
    /// [`Report`].
    ///
    /// This is similar to [`context`](OptionExt::context), but the context is
    /// computed lazily using a closure. This can be useful when computing
    /// the context is expensive, as the closure will only be called if the
    /// option is `None`.
    ///
    /// See also [`local_context_with`](OptionExt::local_context_with) for a
    /// non-thread-safe version that works with types that are not
    /// `Send + Sync`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::{option_ext::OptionExt, prelude::*};
    ///
    /// fn expensive_context_computation() -> String {
    ///     format!("Failed at {}", "12:34:56")
    /// }
    ///
    /// let value: Option<u64> = None;
    /// let result: Result<u64, Report<String>> = value.context_with(expensive_context_computation);
    /// ```
    #[track_caller]
    fn context_with<C, F>(self, context: F) -> Result<V, Report<C, Mutable, SendSync>>
    where
        F: FnOnce() -> C,
        C: Send + Sync + core::fmt::Display + core::fmt::Debug;

    /// Converts `None` into a new [`Report`] using the provided context and a
    /// custom handler. The [`NoneError`] is set as a child of the new
    /// [`Report`].
    ///
    /// This is similar to [`context`](OptionExt::context), but uses a custom
    /// [`ContextHandler`] to control how the context is formatted and
    /// displayed.
    ///
    /// See also [`local_context_custom`](OptionExt::local_context_custom) for a
    /// non-thread-safe version that works with types that are not
    /// `Send + Sync`.
    ///
    /// [`ContextHandler`]: handlers::ContextHandler
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::{handlers, option_ext::OptionExt, prelude::*};
    ///
    /// #[derive(Debug)]
    /// struct ErrorContext {
    ///     code: u32,
    ///     message: String,
    /// }
    ///
    /// let value: Option<String> = None;
    /// let result: Result<String, Report<ErrorContext>> =
    ///     value.context_custom::<handlers::Debug, _>(ErrorContext {
    ///         code: 404,
    ///         message: "Not found".to_string(),
    ///     });
    /// ```
    #[track_caller]
    fn context_custom<H, C>(self, context: C) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: Send + Sync,
        H: handlers::ContextHandler<C>;

    /// Converts `None` into a new [`Report`] using context generated by the
    /// provided closure and a custom handler. The [`NoneError`] is set as
    /// a child of the new [`Report`].
    ///
    /// This is similar to [`context_with`](OptionExt::context_with), but uses a
    /// custom [`ContextHandler`] to control how the context is formatted and
    /// displayed.
    ///
    /// See also [`local_context_custom_with`](OptionExt::local_context_custom_with)
    /// for a non-thread-safe version that works with types that are not
    /// `Send + Sync`.
    ///
    /// [`ContextHandler`]: handlers::ContextHandler
    ///
    /// # Examples
    ///
    /// ```
    /// use rootcause::{handlers, option_ext::OptionExt, prelude::*};
    ///
    /// #[derive(Debug)]
    /// struct ErrorContext {
    ///     timestamp: String,
    ///     operation: String,
    /// }
    ///
    /// fn expensive_computation() -> ErrorContext {
    ///     ErrorContext {
    ///         timestamp: "12:34:56".to_string(),
    ///         operation: "lookup".to_string(),
    ///     }
    /// }
    ///
    /// let value: Option<u64> = None;
    /// let result: Result<u64, Report<ErrorContext>> =
    ///     value.context_custom_with::<handlers::Debug, _, _>(expensive_computation);
    /// ```
    #[track_caller]
    fn context_custom_with<H, C, F>(self, context: F) -> Result<V, Report<C, Mutable, SendSync>>
    where
        F: FnOnce() -> C,
        C: Send + Sync,
        H: handlers::ContextHandler<C>;

    /// Converts `None` to a different context type using [`ReportConversion`].
    ///
    /// If `None`, creates a [`Report<NoneError>`] and transforms it using
    /// the [`ReportConversion`] implementation. Implement
    /// [`ReportConversion`] once to define conversions, then use `context_to()`
    /// at call sites. The target type `C` is typically inferred from the return
    /// type.
    ///
    /// See also: [`local_context_to`](OptionExt::local_context_to) (non-`Send +
    /// Sync` version).
    ///
    /// [`ReportConversion`]: crate::ReportConversion
    ///
    /// # Examples
    ///
    /// ```
    /// # use rootcause::{ReportConversion, markers, option_ext::{OptionExt, NoneError}, prelude::*};
    /// #[derive(Debug)]
    /// enum AppError {
    ///     MissingValue
    /// }
    ///
    /// # impl std::fmt::Display for AppError {
    /// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "error") }
    /// # }
    /// impl<O, T> ReportConversion<NoneError, O, T> for AppError
    ///   where AppError: markers::ObjectMarkerFor<T>
    /// {
    ///     fn convert_report(report: Report<NoneError, O, T>) -> Report<Self, markers::Mutable, T>
    ///     {
    ///         report.context(AppError::MissingValue)
    ///     }
    /// }
    ///
    /// let value: Option<String> = None;
    /// let result: Result<String, Report<AppError>> = value.context_to();
    /// ```
    #[track_caller]
    fn context_to<C>(self) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: ReportConversion<NoneError, Mutable, SendSync>;

    // Local variants (non-Send + Sync)

    /// Converts `None` into a new local (non-thread-safe) [`Report`] using
    /// the provided context. The [`NoneError`] is set as a child of the new
    /// [`Report`].
    ///
    /// This is the non-`Send + Sync` version of
    /// [`context`](OptionExt::context). Use this when working with context
    /// types that cannot be sent across thread boundaries.
    ///
    /// See also [`context`](OptionExt::context) for a thread-safe version that
    /// returns a [`Report`] that can be sent across thread boundaries.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    ///
    /// use rootcause::{option_ext::OptionExt, prelude::*};
    ///
    /// let value: Option<String> = None;
    /// let result: Result<String, Report<Rc<&str>, _, markers::Local>> =
    ///     value.local_context(Rc::from("Failed to get value"));
    /// ```
    #[track_caller]
    fn local_context<C>(self, context: C) -> Result<V, Report<C, Mutable, Local>>
    where
        C: core::fmt::Display + core::fmt::Debug;

    /// Converts `None` into a new local (non-thread-safe) [`Report`] using
    /// context generated by the provided closure. The [`NoneError`] is set
    /// as a child of the new [`Report`].
    ///
    /// This is the non-`Send + Sync` version of
    /// [`context_with`](OptionExt::context_with). Use this when working with
    /// context types that cannot be sent across thread boundaries.
    ///
    /// See also [`context_with`](OptionExt::context_with) for a thread-safe
    /// version that returns a [`Report`] that can be sent across thread
    /// boundaries.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    ///
    /// use rootcause::{option_ext::OptionExt, prelude::*};
    ///
    /// fn expensive_computation() -> Rc<String> {
    ///     Rc::new(format!("Failed at {}", "12:34:56"))
    /// }
    ///
    /// let value: Option<u64> = None;
    /// let result: Result<u64, Report<Rc<String>, _, markers::Local>> =
    ///     value.local_context_with(expensive_computation);
    /// ```
    #[track_caller]
    fn local_context_with<C, F>(self, context: F) -> Result<V, Report<C, Mutable, Local>>
    where
        F: FnOnce() -> C,
        C: core::fmt::Display + core::fmt::Debug;

    /// Converts `None` into a new local (non-thread-safe) [`Report`] using the
    /// provided context and a custom handler. The [`NoneError`] is set as a
    /// child of the new [`Report`].
    ///
    /// This is the non-`Send + Sync` version of
    /// [`context_custom`](OptionExt::context_custom). Use this when working
    /// with context types that cannot be sent across thread boundaries.
    ///
    /// See also [`context_custom`](OptionExt::context_custom) for a thread-safe
    /// version that returns a [`Report`] that can be sent across thread
    /// boundaries.
    ///
    /// [`ContextHandler`]: handlers::ContextHandler
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    ///
    /// use rootcause::{handlers, option_ext::OptionExt, prelude::*};
    ///
    /// #[derive(Debug)]
    /// struct ErrorContext {
    ///     data: Rc<String>,
    /// }
    ///
    /// let value: Option<String> = None;
    /// let result: Result<String, Report<ErrorContext, _, markers::Local>> = value
    ///     .local_context_custom::<handlers::Debug, _>(ErrorContext {
    ///         data: Rc::new("context".to_string()),
    ///     });
    /// ```
    #[track_caller]
    fn local_context_custom<H, C>(self, context: C) -> Result<V, Report<C, Mutable, Local>>
    where
        C: 'static,
        H: handlers::ContextHandler<C>;

    /// Converts `None` into a new local (non-thread-safe) [`Report`] using
    /// context generated by the provided closure and a custom handler. The
    /// [`NoneError`] is set as a child of the new [`Report`].
    ///
    /// This is the non-`Send + Sync` version of
    /// [`context_custom_with`](OptionExt::context_custom_with). Use this when
    /// working with context types that cannot be sent across thread boundaries.
    ///
    /// See also [`context_custom_with`](OptionExt::context_custom_with) for a
    /// thread-safe version that returns a [`Report`] that can be sent across
    /// thread boundaries.
    ///
    /// [`ContextHandler`]: handlers::ContextHandler
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    ///
    /// use rootcause::{handlers, option_ext::OptionExt, prelude::*};
    ///
    /// #[derive(Debug)]
    /// struct ErrorContext {
    ///     data: Rc<String>,
    /// }
    ///
    /// fn expensive_computation() -> ErrorContext {
    ///     ErrorContext {
    ///         data: Rc::new("context".to_string()),
    ///     }
    /// }
    ///
    /// let value: Option<u64> = None;
    /// let result: Result<u64, Report<ErrorContext, _, markers::Local>> =
    ///     value.local_context_custom_with::<handlers::Debug, _, _>(expensive_computation);
    /// ```
    #[track_caller]
    fn local_context_custom_with<H, C, F>(self, context: F) -> Result<V, Report<C, Mutable, Local>>
    where
        F: FnOnce() -> C,
        C: 'static,
        H: handlers::ContextHandler<C>;

    /// Converts `None` to a different context type using [`ReportConversion`].
    ///
    /// This is the non-`Send + Sync` version of
    /// [`context_to`](OptionExt::context_to). Use this when working with types
    /// that cannot be sent across thread boundaries.
    ///
    /// See also [`context_to`](OptionExt::context_to) for a thread-safe version
    /// that returns a [`Report`] that can be sent across thread boundaries.
    ///
    /// [`ReportConversion`]: crate::ReportConversion
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::rc::Rc;
    /// # use rootcause::{ReportConversion, markers, option_ext::{OptionExt, NoneError}, prelude::*};
    /// #[derive(Debug)]
    /// enum AppError {
    ///     MissingValue
    /// }
    /// # impl std::fmt::Display for AppError {
    /// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "error") }
    /// # }
    ///
    /// impl<O> ReportConversion<NoneError, O, markers::Local> for AppError
    /// {
    ///     fn convert_report(report: Report<NoneError, O, markers::Local>) -> Report<Self, markers::Mutable, markers::Local>
    ///     {
    ///         report.context(AppError::MissingValue)
    ///     }
    /// }
    ///
    /// let value: Option<Rc<String>> = None;
    /// let result: Result<Rc<String>, Report<AppError, _, markers::Local>> = value.local_context_to();
    /// ```
    #[track_caller]
    fn local_context_to<C>(self) -> Result<V, Report<C, Mutable, Local>>
    where
        C: ReportConversion<NoneError, Mutable, Local>;
}

impl<V> OptionExt<V> for Option<V> {
    #[inline]
    fn ok_or_report(self) -> Result<V, Report<NoneError, Mutable, SendSync>> {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>()),
        }
    }

    #[inline]
    fn context<C>(self, context: C) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: Send + Sync + core::fmt::Display + core::fmt::Debug,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>().context(context)),
        }
    }

    #[inline]
    fn context_with<C, F>(self, context: F) -> Result<V, Report<C, Mutable, SendSync>>
    where
        F: FnOnce() -> C,
        C: Send + Sync + core::fmt::Display + core::fmt::Debug,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>().context(context())),
        }
    }

    #[inline]
    fn context_custom<H, C>(self, context: C) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: Send + Sync,
        H: handlers::ContextHandler<C>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>().context_custom::<H, _>(context)),
        }
    }

    #[inline]
    fn context_custom_with<H, C, F>(self, context: F) -> Result<V, Report<C, Mutable, SendSync>>
    where
        F: FnOnce() -> C,
        C: Send + Sync,
        H: handlers::ContextHandler<C>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>().context_custom::<H, _>(context())),
        }
    }

    #[inline]
    fn context_to<C>(self) -> Result<V, Report<C, Mutable, SendSync>>
    where
        C: ReportConversion<NoneError, Mutable, SendSync>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_sendsync_report::<V>().context_to()),
        }
    }

    #[inline]
    fn local_context<C>(self, context: C) -> Result<V, Report<C, Mutable, Local>>
    where
        C: core::fmt::Display + core::fmt::Debug,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_local_report::<V>().context(context)),
        }
    }

    #[inline]
    fn local_context_with<C, F>(self, context: F) -> Result<V, Report<C, Mutable, Local>>
    where
        F: FnOnce() -> C,
        C: core::fmt::Display + core::fmt::Debug,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_local_report::<V>().context(context())),
        }
    }

    #[inline]
    fn local_context_custom<H, C>(self, context: C) -> Result<V, Report<C, Mutable, Local>>
    where
        C: 'static,
        H: handlers::ContextHandler<C>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_local_report::<V>().context_custom::<H, _>(context)),
        }
    }

    #[inline]
    fn local_context_custom_with<H, C, F>(self, context: F) -> Result<V, Report<C, Mutable, Local>>
    where
        F: FnOnce() -> C,
        C: 'static,
        H: handlers::ContextHandler<C>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_local_report::<V>().context_custom::<H, _>(context())),
        }
    }

    #[inline]
    fn local_context_to<C>(self) -> Result<V, Report<C, Mutable, Local>>
    where
        C: ReportConversion<NoneError, Mutable, Local>,
    {
        match self {
            Some(v) => Ok(v),
            None => Err(NoneError::new_local_report::<V>().context_to()),
        }
    }
}