From 40489dcd8f6c1b18520a4d5596a5a6982127ec12 Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Sun, 14 Apr 2024 20:42:24 -0300 Subject: [PATCH] Create basic library --- .editorconfig | 18 ++++++++++++++++++ .gitattributes | 1 + .gitignore | 2 ++ Cargo.toml | 22 ++++++++++++++++++++++ LICENSE.txt | 21 +++++++++++++++++++++ README.md | 24 +++++++++++++++++++++++- examples/option.rs | 9 +++++++++ examples/result.rs | 13 +++++++++++++ src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE.txt create mode 100644 examples/option.rs create mode 100644 examples/result.rs create mode 100644 src/lib.rs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cf48504 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +end_of_line = lf +indent_style = space +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[*.env*] +insert_final_newline = false + +[.gitmodules] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..82eec0f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "unwrap-gui" +version = "0.1.0" +description = "Unwrap Gui" +repository = "https://github.com/Tribufu/UnwrapGui" +authors = ["Tribufu "] +license = "MIT" +edition = "2021" + +[lib] +name = "unwrap_gui" +crate-type = ["rlib"] +path = "src/lib.rs" + +[dependencies] +anyhow = "1.0.82" + +[target.'cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))'.dependencies] +native-dialog = "0.7.0" + +[dev-dependencies] +anyhow = "1.0.82" diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..292d951 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) Tribufu. All Rights Reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index ea73dc6..a0fd027 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# UnwrapGui \ No newline at end of file +# Unwrap Gui + +Show a native dialog message when unwrap. + +[![Crates.io][crates-badge]][crates-url] +[![MIT License][mit-badge]][mit-url] +[![Discord Chat][discord-badge]][discord-url] + +[crates-badge]: https://img.shields.io/crates/v/unwrap-gui.svg +[crates-url]: https://crates.io/crates/unwrap-gui +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: https://github.com/Tribufu/UnwrapGui/blob/main/LICENSE.txt +[discord-badge]: https://img.shields.io/discord/276504514616623104.svg?logo=discord&style=flat-square +[discord-url]: https://www.tribufu.com/discord + +[Website](https://www.tribufu.com) | +[Discord](https://www.tribufu.com/discord) + +## License + +This project is licensed under the [MIT License]. + +[MIT License]: https://github.com/Tribufu/UnwrapGui/blob/main/LICENSE.txt diff --git a/examples/option.rs b/examples/option.rs new file mode 100644 index 0000000..d71d1b2 --- /dev/null +++ b/examples/option.rs @@ -0,0 +1,9 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +use unwrap_gui::*; + +fn main() { + let option: Option<()> = None; + option.unwrap_gui(); +} diff --git a/examples/result.rs b/examples/result.rs new file mode 100644 index 0000000..c0f8c63 --- /dev/null +++ b/examples/result.rs @@ -0,0 +1,13 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +use anyhow::{Error, Result}; +use unwrap_gui::*; + +fn main() { + let result: Result<()> = Err(Error::msg( + "With this trait you can display a message and exit the app.", + )); + + result.unwrap_gui(); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3f5d5b8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,39 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +use anyhow::Result; +use std::process; + +pub trait UnwrapGui { + fn unwrap_gui(self) -> T; +} + +impl UnwrapGui for Option { + fn unwrap_gui(self) -> T { + match self { + Some(value) => value, + None => show_dialog("Unwrapped an Option that was None"), + } + } +} + +impl UnwrapGui for Result { + fn unwrap_gui(self) -> T { + match self { + Ok(value) => value, + Err(e) => show_dialog(e.to_string()), + } + } +} + +fn show_dialog(error: impl Into) -> ! { + #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] + if let Err(_) = native_dialog::MessageDialog::new() + .set_title("Fatal Error") + .set_text(&error.into()) + .set_type(native_dialog::MessageType::Error) + .show_alert() + {} + + process::exit(0) +}