forked from neovide/neovide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handling.rs
37 lines (32 loc) · 1005 Bytes
/
error_handling.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
use log::error;
fn show_error(explanation: &str) -> ! {
error!("{}", explanation);
panic!("{}", explanation.to_string());
}
pub trait ResultPanicExplanation<T, E: ToString> {
fn unwrap_or_explained_panic(self, explanation: &str) -> T;
}
impl<T, E: ToString> ResultPanicExplanation<T, E> for Result<T, E> {
fn unwrap_or_explained_panic(self, explanation: &str) -> T {
match self {
Err(error) => {
let explanation = format!("{}: {}", explanation, error.to_string());
show_error(&explanation);
}
Ok(content) => content,
}
}
}
pub trait OptionPanicExplanation<T> {
fn unwrap_or_explained_panic(self, explanation: &str) -> T;
}
impl<T> OptionPanicExplanation<T> for Option<T> {
fn unwrap_or_explained_panic(self, explanation: &str) -> T {
match self {
None => {
show_error(explanation);
}
Some(content) => content,
}
}
}