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
use std::io;
use std::num::ParseIntError;
#[macro_export]
/// Prints out an compilation error message in red.
macro_rules! compile_error {
    ($( $arg: tt)*) => ({
        let s = format!($($arg)*);
        eprintln!("{}", simple_colors::red!(s));
        std::process::exit(1)
    })
}

pub static mut VERBOSE: bool = false;

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

impl<T> CompileError<T> for Result<T, ParseIntError> {
    fn expect_compile_error(self, msg: &str) -> T {
        match self {
            Ok(t) => t,
            Err(e) => unsafe {
                if VERBOSE {
                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
                } else {
                    crate::compile_error!("{}", msg)
                }
            }
        }
    }
}

impl<T> CompileError<T> for Result<T, String> {
    fn expect_compile_error(self, msg: &str) -> T {
        match self {
            Ok(t) => t,
            Err(e) => unsafe {
                if VERBOSE {
                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
                } else {
                    crate::compile_error!("{}", msg)
                }
            }
        }
    }
}

impl<T> CompileError<T> for Option<T> {
    fn expect_compile_error(self, msg: &str) -> T {
        match self {
            Some(t) => t,
            None => crate::compile_error!("{}", msg)
        }
    }
}

impl<T> CompileError<T> for io::Result<T> {
    fn expect_compile_error(self, msg: &str) -> T {
        match self {
            Ok(t) => t,
            Err(e) => unsafe {
                if VERBOSE {
                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
                } else {
                    crate::compile_error!("{}", msg)
                }
            }
        }
    }
}