expect_dialog/
lib.rs

1///! It's just like `.expect()` except you get a dialog instead of only terminal output
2mod test;
3
4/// Expect dialog trait, implemented on Option and Result out of the box
5pub trait ExpectDialog<T> {
6    fn expect_dialog(self, msg: &str) -> T;
7}
8
9impl<T, E: std::fmt::Debug> ExpectDialog<T> for Result<T, E> {
10    fn expect_dialog(self, msg: &str) -> T {
11        match self {
12            Ok(value) => return value,
13            Err(e) => {
14                panic_dialog!("{msg}: {e:?}");
15            }
16        }
17    }
18}
19
20impl<T> ExpectDialog<T> for Option<T> {
21    fn expect_dialog(self, msg: &str) -> T {
22        match self {
23            Some(value) => return value,
24            None => {
25                panic_dialog!("{}", msg);
26            }
27        }
28    }
29}
30
31#[macro_export]
32#[cfg(not(test))]
33macro_rules! panic_dialog {
34    ($($arg:tt)*) => { 
35        let msg = format!($($arg)*);
36
37        native_dialog::MessageDialog::new()
38                    .set_type(native_dialog::MessageType::Error)
39                    .set_title("Fatal Error")
40                    .set_text(&msg)
41                    .show_alert()
42                    .expect("Could not display dialog box");
43        core::panic!($($arg)*);
44    }
45}
46
47#[macro_export]
48#[cfg(test)]
49macro_rules! panic_dialog {
50    ($($arg:tt)*) => { 
51        core::panic!($($arg)*);
52    }
53}