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
use super::{
super::{attachment::*, error::*, implementation::*, problem::*},
cause::*,
ref_iterator::*,
};
//
// CauseRef
//
/// Reference to a [Cause].
pub struct CauseRef<'problem, ErrorT> {
/// Containing problem.
pub problem: &'problem Problem,
/// Depth in causation chain.
pub depth: usize,
/// Error.
///
/// This error could be either on the causation chain or nested in
/// [source](std::error::Error::source).
pub error: &'problem ErrorT,
/// Attachments.
pub attachments: &'problem Vector<CapturedAttachment>,
}
impl<'problem, ErrorT> CauseRef<'problem, ErrorT> {
/// Constructor.
pub fn new(
problem: &'problem Problem,
depth: usize,
error: &'problem ErrorT,
attachments: &'problem Vector<CapturedAttachment>,
) -> Self {
CauseRef {
problem,
depth,
error,
attachments,
}
}
/// The cause.
pub fn cause(&self) -> &'problem Cause {
self.problem
.get(self.depth)
.expect("depth < causation chain length")
}
/// Iterate the causation chain starting from *under* this cause ending at the root.
///
/// Note that this will skip over [source](std::error::Error::source).
pub fn iter_under(&self) -> CauseRefIterator<'problem> {
CauseRefIterator::new(self.problem, self.depth + 1)
}
/// The cause under this one in the causation chain.
///
/// It will be [None] if we are the root cause.
///
/// Note that this will skip over [source](std::error::Error::source).
pub fn under(&self) -> Option<CauseRef<'problem, CapturedError>> {
self.iter_under().next()
}
/// Whether we are the top cause of the chain.
pub fn is_top(&self) -> bool {
self.depth == 0
}
/// Whether we are the root cause of the chain.
pub fn is_root(&self) -> bool {
let length = self.problem.depth();
assert!(self.depth < length, "depth < causation chain length");
self.depth == length - 1
}
}
impl<'problem, ErrorT> Attachments for CauseRef<'problem, ErrorT> {
fn attachments(&self) -> impl Iterator<Item = &CapturedAttachment> {
self.attachments.iter()
}
}