Create basic library

This commit is contained in:
2024-04-14 20:42:24 -03:00
parent 9185b3a2a8
commit 40489dcd8f
9 changed files with 148 additions and 1 deletions

18
.editorconfig Normal file
View File

@ -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

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto eol=lf

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
Cargo.lock

22
Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
name = "unwrap-gui"
version = "0.1.0"
description = "Unwrap Gui"
repository = "https://github.com/Tribufu/UnwrapGui"
authors = ["Tribufu <contact@tribufu.com>"]
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"

21
LICENSE.txt Normal file
View File

@ -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.

View File

@ -1 +1,23 @@
# UnwrapGui
# 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

9
examples/option.rs Normal file
View File

@ -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();
}

13
examples/result.rs Normal file
View File

@ -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();
}

39
src/lib.rs Normal file
View File

@ -0,0 +1,39 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
use anyhow::Result;
use std::process;
pub trait UnwrapGui<T> {
fn unwrap_gui(self) -> T;
}
impl<T> UnwrapGui<T> for Option<T> {
fn unwrap_gui(self) -> T {
match self {
Some(value) => value,
None => show_dialog("Unwrapped an Option that was None"),
}
}
}
impl<T> UnwrapGui<T> for Result<T> {
fn unwrap_gui(self) -> T {
match self {
Ok(value) => value,
Err(e) => show_dialog(e.to_string()),
}
}
}
fn show_dialog(error: impl Into<String>) -> ! {
#[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)
}