rpg_compiler/user_output/
compile_error.rs

1use std::io;
2use std::num::ParseIntError;
3#[macro_export]
4/// Prints out an compilation error message in red.
5macro_rules! compile_error {
6    ($( $arg: tt)*) => ({
7        let s = format!($($arg)*);
8        eprintln!("{}", simple_colors::red!(s));
9        std::process::exit(1)
10    })
11}
12
13pub static mut VERBOSE: bool = false;
14
15pub trait CompileError<T> {
16    fn expect_compile_error(self, msg: &str) -> T;
17}
18
19impl<T> CompileError<T> for Result<T, ParseIntError> {
20    fn expect_compile_error(self, msg: &str) -> T {
21        match self {
22            Ok(t) => t,
23            Err(e) => unsafe {
24                if VERBOSE {
25                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
26                } else {
27                    crate::compile_error!("{}", msg)
28                }
29            }
30        }
31    }
32}
33
34impl<T> CompileError<T> for Result<T, String> {
35    fn expect_compile_error(self, msg: &str) -> T {
36        match self {
37            Ok(t) => t,
38            Err(e) => unsafe {
39                if VERBOSE {
40                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
41                } else {
42                    crate::compile_error!("{}", msg)
43                }
44            }
45        }
46    }
47}
48
49impl<T> CompileError<T> for Option<T> {
50    fn expect_compile_error(self, msg: &str) -> T {
51        match self {
52            Some(t) => t,
53            None => crate::compile_error!("{}", msg)
54        }
55    }
56}
57
58impl<T> CompileError<T> for io::Result<T> {
59    fn expect_compile_error(self, msg: &str) -> T {
60        match self {
61            Ok(t) => t,
62            Err(e) => unsafe {
63                if VERBOSE {
64                    crate::compile_error!("{}\n== VERBOSE OUTPUT ==\n{}", msg, e)
65                } else {
66                    crate::compile_error!("{}", msg)
67                }
68            }
69        }
70    }
71}