Implement std::fmt::Display for the error types

This commit is contained in:
Mika Attila 2015-03-10 19:14:18 +01:00
parent a0955acabd
commit 445f0b6d13
2 changed files with 29 additions and 4 deletions

View File

@ -1,9 +1,12 @@
extern crate libnotify; extern crate libnotify;
fn main() { 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.", let n = notify.new_notification("This is the summary.",
Some("This is the optional body text."), body_text,
None).unwrap(); None).unwrap_or_else(|e| panic!("{}", e));
n.show().unwrap(); n.show().ok().expect("Failed to show notification");
} }

View File

@ -17,6 +17,7 @@ extern crate "glib-2_0-sys" as glib;
use std::ffi::CString; use std::ffi::CString;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::fmt;
use glib::types::{ use glib::types::{
TRUE, TRUE,
@ -32,12 +33,33 @@ pub enum ContextCreationError {
NulError 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)] #[derive(Debug)]
pub enum NotificationCreationError { pub enum NotificationCreationError {
NulError, NulError,
Unknown 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. /// The context which within libnotify operates.
/// ///
/// Only one context can exist at a time. /// Only one context can exist at a time.