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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Macros and other utility code.

/// This should be used for every write to stdout.
#[macro_export]
macro_rules! puts {
    ($($arg:tt)*) => ({
        use std::io::Write;
        match write!(std::io::stdout(), $($arg)*) {
            Ok(()) => (),
            // Don't panic on SIGPIPE.
            Err(ref err) if err.kind() == std::io::ErrorKind::BrokenPipe => std::process::exit(141),
            Err(ref err) =>  panic!("{}", err),
        };
    })
}

/// Print to stdout, prefixed by "c ".
#[macro_export]
macro_rules! comment {
    ($($arg:tt)*) => ({
        $crate::puts!("c ");
        $crate::puts!($($arg)*);
        $crate::puts!("\n");
    })
}

/// Print to stdout with yellow font color.
#[macro_export]
macro_rules! as_warning {
    ($what:expr) => {{
        if $crate::output::is_a_tty() {
            $crate::puts!("\x1b[33;1m");
        }
        $what;
        if $crate::output::is_a_tty() {
            $crate::puts!("\x1b[0m");
        }
    }};
}

/// Print to stdout with red font color.
#[macro_export]
macro_rules! as_error {
    ($what:expr) => {{
        if $crate::output::is_a_tty() {
            $crate::puts!("\x1b[31;1m");
        }
        $what;
        if $crate::output::is_a_tty() {
            $crate::puts!("\x1b[0m");
        }
    }};
}

/// Report a fatal error and exit.
#[macro_export]
macro_rules! die {
    ($($arg:tt)*) => ({
        $crate::as_error!({
            $crate::puts!($($arg)*);
            $crate::puts!("\n");
        });
        std::process::exit(2);
    })
}

/// Native assertions cannot be disabled, that's why why prefer to use this
/// macro.
#[macro_export]
macro_rules! invariant {
    ($($arg:tt)*) => ({
        if $crate::config::CHECK_INVARIANTS {
            assert!($($arg)*);
        }
    })
}

/// Like invariant, but for preconditions.
#[macro_export]
macro_rules! requires {
    ($($arg:tt)*) => ({
        if $crate::config::CHECK_PRECONDITIONS {
            assert!($($arg)*);
        }
    })
}