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
use std::fmt;

pub mod argparse;

pub trait OrExit<T> {
    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T;
}

impl<T, E: fmt::Display> OrExit<T> for Result<T, E> {
    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T {
        match self {
            Err(why) => {
                match with_msg_prefix {
                    None => eprintln!("{}", why),
                    Some(msg) => eprintln!("{}: {}", msg, why),
                };
                std::process::exit(1);
            }
            Ok(value) => value,
        }
    }
}

impl<T> OrExit<T> for Option<T> {
    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T {
        match self {
            None => {
                match with_msg_prefix {
                    None => eprintln!("expected the Option to have some value"),
                    Some(msg) => eprintln!("{}", msg),
                };
                std::process::exit(1);
            }
            Some(value) => value,
        }
    }
}

#[macro_export]
macro_rules! print_named_vars {
    ($($id:ident), +) => {
        $(
            println!("{}", format_args!("{} {}", stringify!($id), $id));
        )+
    };
}

#[macro_export]
macro_rules! debug_print_named_vars {
    ($($id:ident), +) => {
        $(
            println!("{}", format_args!("{} {:?}", stringify!($id), $id));
        )+
    };
}

#[macro_export]
macro_rules! eprint_named_vars {
    ($($id:ident), +) => {
        $(
            eprintln!("{}", format_args!("{} {}", stringify!($id), $id));
        )+
    };
}

#[macro_export]
macro_rules! debug_eprint_named_vars {
    ($($id:ident), +) => {
        $(
            eprintln!("{}", format_args!("{} {:?}", stringify!($id), $id));
        )+
    };
}