Expose ffi::NulError in nul errors

This commit is contained in:
Mika Attila 2015-11-03 01:01:49 +01:00
parent db2578ea92
commit 6251e0ad5c

View File

@ -21,7 +21,7 @@ extern crate libnotify_sys as sys;
extern crate glib_2_0_sys as glib; extern crate glib_2_0_sys as glib;
extern crate gtypes; extern crate gtypes;
use std::ffi::{CStr, CString}; use std::ffi::{self, CStr, CString};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::fmt; use std::fmt;
use std::error::Error; use std::error::Error;
@ -35,7 +35,8 @@ pub enum ContextCreationError {
AlreadyExists, AlreadyExists,
/// Failed to initialize libnotify. /// Failed to initialize libnotify.
InitError, InitError,
NulError, /// A nul byte was found in the provided string.
NulError(ffi::NulError),
} }
impl fmt::Display for ContextCreationError { impl fmt::Display for ContextCreationError {
@ -44,14 +45,16 @@ impl fmt::Display for ContextCreationError {
match *self { match *self {
AlreadyExists => write!(f, "A Libnotify context already exists."), AlreadyExists => write!(f, "A Libnotify context already exists."),
InitError => write!(f, "Failed to initialize libnotify."), InitError => write!(f, "Failed to initialize libnotify."),
NulError => write!(f, "Argument contains a nul character."), NulError(ref e) => write!(f, "{}", e),
} }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum NotificationCreationError { pub enum NotificationCreationError {
NulError, /// A nul byte was found in the provided string.
NulError(ffi::NulError),
/// An unknown error happened.
Unknown, Unknown,
} }
@ -59,7 +62,7 @@ impl fmt::Display for NotificationCreationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use NotificationCreationError::*; use NotificationCreationError::*;
match *self { match *self {
NulError => write!(f, "Argument contains a nul character."), NulError(ref e) => write!(f, "{}", e),
Unknown => write!(f, "Unknown error"), Unknown => write!(f, "Unknown error"),
} }
} }
@ -83,7 +86,7 @@ impl Context {
} }
let app_name = match CString::new(app_name) { let app_name = match CString::new(app_name) {
Ok(name) => name, Ok(name) => name,
Err(_) => return Err(ContextCreationError::NulError), Err(e) => return Err(ContextCreationError::NulError(e)),
}; };
if sys::notify_init(app_name.as_ptr()) == FALSE { if sys::notify_init(app_name.as_ptr()) == FALSE {
return Err(ContextCreationError::InitError); return Err(ContextCreationError::InitError);
@ -105,12 +108,12 @@ impl Context {
-> Result<Notification, NotificationCreationError> { -> Result<Notification, NotificationCreationError> {
let summary = match CString::new(summary) { let summary = match CString::new(summary) {
Ok(cstr) => cstr, Ok(cstr) => cstr,
Err(_) => return Err(NotificationCreationError::NulError), Err(e) => return Err(NotificationCreationError::NulError(e)),
}; };
let body = match body { let body = match body {
Some(body) => match CString::new(body) { Some(body) => match CString::new(body) {
Ok(cstr) => Some(cstr), Ok(cstr) => Some(cstr),
Err(_) => return Err(NotificationCreationError::NulError), Err(e) => return Err(NotificationCreationError::NulError(e)),
}, },
None => None, None => None,
}; };
@ -121,7 +124,7 @@ impl Context {
let icon = match icon { let icon = match icon {
Some(icon) => match CString::new(icon) { Some(icon) => match CString::new(icon) {
Ok(cstr) => Some(cstr), Ok(cstr) => Some(cstr),
Err(_) => return Err(NotificationCreationError::NulError), Err(e) => return Err(NotificationCreationError::NulError(e)),
}, },
None => None, None => None,
}; };