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
use crate::{BoxError, ErrReport, Result, RootCauseFirst};
use eyre_impl::{ErrorContext, IntoErrorReporter};
use std::fmt;
impl RootCauseFirst {
pub fn note(&mut self, context: impl ContextObj) {
self.push(ContextObject::Note(Box::new(context)));
}
pub fn warn(&mut self, context: impl ContextObj) {
self.push(ContextObject::Warn(Box::new(context)));
}
}
impl ErrorContext<ContextObject> for RootCauseFirst {
fn push(&mut self, context: ContextObject) {
self.context.push(context);
}
}
impl Default for RootCauseFirst {
fn default() -> Self {
Self {
context: Vec::new(),
backtrace: std::backtrace::Backtrace::capture(),
}
}
}
pub trait ContextObj: fmt::Display + Send + Sync + 'static {}
impl<T> ContextObj for T where T: fmt::Display + Send + Sync + 'static {}
pub trait ContextExt<T, E>: private::Sealed {
fn note<C>(self, context: C) -> Result<T>
where
C: ContextObj;
fn with_note<C, F>(self, f: F) -> Result<T>
where
C: ContextObj,
F: FnOnce() -> C;
}
impl<T, E> ContextExt<T, E> for std::result::Result<T, E>
where
E: eyre_impl::IntoErrorReporter<BoxError, RootCauseFirst, ContextObject>
+ Send
+ Sync
+ 'static,
{
fn note<C>(self, context: C) -> Result<T>
where
C: fmt::Display + Send + Sync + 'static,
{
self.map_err(|error| {
ErrReport::new(error.ext_context(ContextObject::Note(Box::new(context))))
})
}
fn with_note<C, F>(self, context: F) -> Result<T>
where
C: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
self.map_err(|error| {
ErrReport::new(error.ext_context(ContextObject::Note(Box::new(context()))))
})
}
}
impl IntoErrorReporter<BoxError, RootCauseFirst, ContextObject> for crate::ErrReport {
fn ext_context(
mut self,
context: ContextObject,
) -> eyre_impl::ErrorReporter<BoxError, RootCauseFirst> {
self.0.context.push(context);
*self.0
}
}
pub enum ContextObject {
Note(Box<dyn ContextObj>),
Warn(Box<dyn ContextObj>),
}
impl fmt::Display for ContextObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Note(context) => write!(f, "Note: {}", context),
Self::Warn(context) => write!(f, "Warn: {}", context),
}
}
}
pub(crate) mod private {
use super::*;
pub trait Sealed {}
impl<T, E> Sealed for std::result::Result<T, E> where
E: eyre_impl::IntoErrorReporter<BoxError, RootCauseFirst, ContextObject>
{
}
}