unwrap_gui/
lib.rs

1// Copyright (c) Tribufu. All Rights Reserved.
2// SPDX-License-Identifier: MIT
3
4use anyhow::Result;
5use std::process;
6
7pub trait UnwrapGui<T> {
8    fn unwrap_gui(self) -> T;
9}
10
11impl<T> UnwrapGui<T> for Option<T> {
12    fn unwrap_gui(self) -> T {
13        match self {
14            Some(value) => value,
15            None => show_dialog("Unwrapped an Option that was None"),
16        }
17    }
18}
19
20impl<T> UnwrapGui<T> for Result<T> {
21    fn unwrap_gui(self) -> T {
22        match self {
23            Ok(value) => value,
24            Err(e) => show_dialog(e.to_string()),
25        }
26    }
27}
28
29fn show_dialog(error: impl Into<String>) -> ! {
30    #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
31    if let Err(_) = native_dialog::MessageDialog::new()
32        .set_title("Fatal Error")
33        .set_text(&error.into())
34        .set_type(native_dialog::MessageType::Error)
35        .show_alert()
36    {}
37
38    process::exit(0)
39}