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
use std::error::Error;
#[derive(Debug, Default)]
pub(crate) struct DiagnosticCollector {
last_error: Option<Box<dyn Error + Send + Sync>>,
}
impl DiagnosticCollector {
#[allow(unused)]
pub(crate) fn report_error(&mut self, err: impl Into<Box<dyn Error + Send + Sync>>) {
self.last_error = Some(err.into());
}
#[allow(unused)]
pub(crate) fn capture<T, E: Into<Box<dyn Error + Send + Sync>>>(
&mut self,
err: Result<T, E>,
) -> Option<T> {
match err {
Ok(res) => Some(res),
Err(e) => {
self.report_error(e);
None
}
}
}
pub(crate) fn take_last_error(&mut self) -> Option<Box<dyn Error + Send + Sync>> {
self.last_error.take()
}
pub(crate) fn new() -> Self {
Self { last_error: None }
}
}