Skip to content

Commit

Permalink
revise MsgBoxCreationError
Browse files Browse the repository at this point in the history
  • Loading branch information
bekker committed Oct 9, 2020
1 parent 68257bc commit d3e9b8d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ repository = "https://github.com/bekker/msgbox-rs"
keywords = ["msgbox", "gui", "gtk"]
license = "MIT"

[dependencies]
thiserror = "1.0"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "^0.3.8", features = ["winuser"] }

Expand Down
10 changes: 8 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate msgbox;

use msgbox::IconType;
use std::error::Error;

fn main() {
open_window("Hello Title", "Hello World!", IconType::Info);
Expand All @@ -10,7 +11,12 @@ fn main() {
fn open_window(title: &str, content: &str, icon_type: IconType) {
match msgbox::create(title, content, icon_type) {
Ok(()) => (),
Err(err) => eprintln!("Failed to open window (type: {}, title: \"{}\", content: \"{}\")",
err.icon_type, err.title, err.content),
Err(err) => {
eprintln!("{} (type: {}, title: \"{}\", content: \"{}\")",
err, icon_type, title, content);
if err.source().is_some() {
eprintln!("cause: {}", err.source().unwrap())
}
}
}
}
19 changes: 10 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#[derive(Debug)]
use std::{fmt, error};

#[derive(Debug, Copy, Clone)]
pub enum IconType {
Error,
Info,
None,
}

impl std::fmt::Display for IconType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
impl fmt::Display for IconType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

#[derive(Debug)]
pub struct MsgBoxCreationError<'a> {
pub title: &'a str,
pub content: &'a str,
pub icon_type: IconType,
#[derive(thiserror::Error, Debug)]
pub enum MsgBoxError {
#[error("failed to create a message box")]
Create{#[source] source: Option<Box<dyn error::Error>>},
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate thiserror;

mod common;
pub use common::{IconType, MsgBoxCreationError};
pub use common::{IconType, MsgBoxError};

/**
* GTK+3
Expand Down
16 changes: 9 additions & 7 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use ::gtk;
use ::gtk::prelude::*;
use ::gtk::{ButtonsType, DialogFlags, MessageType, MessageDialog};

use common::{IconType, MsgBoxCreationError};
use common::{IconType, MsgBoxError};

pub fn create<'a>(title:&'a str, content:&'a str, icon_type:IconType) -> std::result::Result<(), MsgBoxCreationError<'a>> {
#[derive(thiserror::Error, Debug)]
pub enum GtkError {
#[error("failed to initialize GTK")]
Init,
}

pub fn create(title:&str, content:&str, icon_type:IconType) -> std::result::Result<(), MsgBoxError> {
if gtk::init().is_err() {
return Err(MsgBoxCreationError {
title,
content,
icon_type
});
return Err(MsgBoxError::Create{source: Some(Box::new(GtkError::Init))})
}

let message_type = match icon_type {
Expand Down
4 changes: 2 additions & 2 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use cocoa::base::{id, nil};
use cocoa::foundation::NSString;

use common::{IconType, MsgBoxCreationError};
use common::{IconType, MsgBoxError};

/**
* cocoa-rs doesn't implement NSAlert yet (0.14.0)
Expand Down Expand Up @@ -86,7 +86,7 @@ impl NSAlert for id {
}
}

pub fn create<'a>(title: &'a str, content: &'a str, icon_type: IconType) -> std::result::Result<(), MsgBoxCreationError<'a>> {
pub fn create(title: &str, content: &str, icon_type: IconType) -> std::result::Result<(), MsgBoxError> {
let alert_style = match icon_type {
IconType::Error => NSAlertStyle::critical,
IconType::Info => NSAlertStyle::informational,
Expand Down
10 changes: 3 additions & 7 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use winapi::um::winuser::{
MessageBoxW, MB_ICONERROR, MB_ICONINFORMATION, MB_OK, MB_SYSTEMMODAL,
};

use common::{IconType, MsgBoxCreationError};
use common::{IconType, MsgBoxError};

pub fn create<'a>(title: &'a str, content: &'a str, icon_type: IconType) -> std::result::Result<(), MsgBoxCreationError<'a>> {
pub fn create(title: &str, content: &str, icon_type: IconType) -> std::result::Result<(), MsgBoxError> {

let lp_text: Vec<u16> = content.encode_utf16().chain(once(0)).collect();
let lp_caption: Vec<u16> = title.encode_utf16().chain(once(0)).collect();
Expand All @@ -28,11 +28,7 @@ pub fn create<'a>(title: &'a str, content: &'a str, icon_type: IconType) -> std:
lp_caption.as_ptr(),
window_type,
) {
0 => Err(MsgBoxCreationError {
title,
content,
icon_type
}),
0 => Err(MsgBoxError::Create{source: None}),
_ => Ok(())
}
}
Expand Down

0 comments on commit d3e9b8d

Please sign in to comment.