-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiagnostic.rs
46 lines (38 loc) · 917 Bytes
/
diagnostic.rs
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
use miette::{Diagnostic, Report};
pub trait UnwrapPretty {
type Output;
fn unwrap_pretty(self) -> Self::Output;
}
impl<T, E> UnwrapPretty for Result<T, E>
where
E: Diagnostic + Sync + Send + 'static,
{
type Output = T;
fn unwrap_pretty(self) -> Self::Output {
match self {
Ok(output) => output,
Err(diagnostic) => {
panic!("{:?}", Report::new(diagnostic));
}
}
}
}
pub trait OkPretty {
type Output;
fn ok_pretty(self) -> Option<Self::Output>;
}
impl<T, E> OkPretty for Result<T, E>
where
E: Diagnostic + Sync + Send + 'static,
{
type Output = T;
fn ok_pretty(self) -> Option<Self::Output> {
match self {
Ok(output) => Some(output),
Err(diagnostic) => {
println!("{:?}", Report::new(diagnostic));
None
}
}
}
}