program_flow/
lib.rs

1use std::fmt;
2
3pub mod argparse;
4
5pub trait OrExit<T> {
6    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T;
7}
8
9impl<T, E: fmt::Display> OrExit<T> for Result<T, E> {
10    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T {
11        match self {
12            Err(why) => {
13                match with_msg_prefix {
14                    None => eprintln!("{}", why),
15                    Some(msg) => eprintln!("{}: {}", msg, why),
16                };
17                std::process::exit(1);
18            }
19            Ok(value) => value,
20        }
21    }
22}
23
24impl<T> OrExit<T> for Option<T> {
25    fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T {
26        match self {
27            None => {
28                match with_msg_prefix {
29                    None => eprintln!("expected the Option to have some value"),
30                    Some(msg) => eprintln!("{}", msg),
31                };
32                std::process::exit(1);
33            }
34            Some(value) => value,
35        }
36    }
37}
38
39#[macro_export]
40macro_rules! print_named_vars {
41    ($($id:ident), +) => {
42        $(
43            println!("{}", format_args!("{} {}", stringify!($id), $id));
44        )+
45    };
46}
47
48#[macro_export]
49macro_rules! debug_print_named_vars {
50    ($($id:ident), +) => {
51        $(
52            println!("{}", format_args!("{} {:?}", stringify!($id), $id));
53        )+
54    };
55}
56
57#[macro_export]
58macro_rules! eprint_named_vars {
59    ($($id:ident), +) => {
60        $(
61            eprintln!("{}", format_args!("{} {}", stringify!($id), $id));
62        )+
63    };
64}
65
66#[macro_export]
67macro_rules! debug_eprint_named_vars {
68    ($($id:ident), +) => {
69        $(
70            eprintln!("{}", format_args!("{} {:?}", stringify!($id), $id));
71        )+
72    };
73}