From 445f0b6d13f13069a6895b33ce8f5cdc2506fa78 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Tue, 10 Mar 2015 19:14:18 +0100 Subject: [PATCH] Implement std::fmt::Display for the error types --- examples/show.rs | 11 +++++++---- src/lib.rs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/show.rs b/examples/show.rs index 603fa464..7bbea4c9 100644 --- a/examples/show.rs +++ b/examples/show.rs @@ -1,9 +1,12 @@ extern crate libnotify; fn main() { - let notify = libnotify::Context::new("hello").unwrap(); + let notify = libnotify::Context::new("hello").unwrap_or_else(|e| { + panic!("{}", e); + }); + let body_text = Some("This is the optional body text."); let n = notify.new_notification("This is the summary.", - Some("This is the optional body text."), - None).unwrap(); - n.show().unwrap(); + body_text, + None).unwrap_or_else(|e| panic!("{}", e)); + n.show().ok().expect("Failed to show notification"); } diff --git a/src/lib.rs b/src/lib.rs index 2263440b..95d8b11a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ extern crate "glib-2_0-sys" as glib; use std::ffi::CString; use std::marker::PhantomData; +use std::fmt; use glib::types::{ TRUE, @@ -32,12 +33,33 @@ pub enum ContextCreationError { NulError } +impl fmt::Display for ContextCreationError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use ContextCreationError::*; + match *self { + AlreadyExists => write!(f, "A Libnotify context already exists."), + InitFailure => write!(f, "Failed to initialize libnotify."), + NulError => write!(f, "Argument contains a nul character.") + } + } +} + #[derive(Debug)] pub enum NotificationCreationError { NulError, Unknown } +impl fmt::Display for NotificationCreationError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use NotificationCreationError::*; + match *self { + NulError => write!(f, "Argument contains a nul character."), + Unknown => write!(f, "Unknown error") + } + } +} + /// The context which within libnotify operates. /// /// Only one context can exist at a time.