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
use std::any::type_name;
use std::error;
use std::process;

use crate::message::Messageable;

pub trait ErrorHandleable<T> {
    fn handle_error(self, msg: &str) -> T;
}

impl<T, E: error::Error> ErrorHandleable<T> for Result<T, E> {
    fn handle_error(self, msg: &str) -> T {
        self.unwrap_or_else(|e| ErrorHandler::handle(e, msg))
    }
}

struct ErrorHandler;

impl ErrorHandler {
    pub fn handle<E: error::Error>(err: E, msg: &str) -> ! {
        format!("{}: {}", type_name::<E>(), err)
            .trim()
            .warning_message()
            .show();
        msg.error_message().show();
        process::exit(1)
    }
}