Implement From<ffi::NulError> for error types that have this variant

This commit is contained in:
Mika Attila 2015-11-03 09:15:16 +01:00
parent d282d2cd50
commit 8daef60a03

View File

@ -1,17 +1,17 @@
//! Rustic bindings to [libnotify](https://developer.gnome.org/libnotify/) //! Rustic bindings to [libnotify](https://developer.gnome.org/libnotify/)
//! //!
//! ```rust //! ```rust
//!extern crate libnotify; //! extern crate libnotify;
//! //!
//!fn main() { //! fn main() {
//! let notify = libnotify::Context::new("hello").unwrap_or_else(|e| { //! let notify = libnotify::Context::new("hello").unwrap_or_else(|e| {
//! panic!("{}", e); //! panic!("{}", e);
//! }); //! });
//! let body_text = Some("This is the optional body text."); //! let body_text = Some("This is the optional body text.");
//! let n = notify.new_notification("This is the summary.", body_text, None) //! let n = notify.new_notification("This is the summary.", body_text, None)
//! .unwrap_or_else(|e| panic!("{}", e)); //! .unwrap_or_else(|e| panic!("{}", e));
//! n.show().unwrap_or_else(|e| panic!("{}", e)); //! n.show().unwrap_or_else(|e| panic!("{}", e));
//!} //! }
//! //!
//! ``` //! ```
@ -50,6 +50,12 @@ impl fmt::Display for ContextCreationError {
} }
} }
impl From<ffi::NulError> for ContextCreationError {
fn from(src: ffi::NulError) -> Self {
ContextCreationError::NulError(src)
}
}
#[derive(Debug)] #[derive(Debug)]
/// An error that can happen when attempting to create a notification. /// An error that can happen when attempting to create a notification.
pub enum NotificationCreationError { pub enum NotificationCreationError {
@ -69,6 +75,12 @@ impl fmt::Display for NotificationCreationError {
} }
} }
impl From<ffi::NulError> for NotificationCreationError {
fn from(src: ffi::NulError) -> Self {
NotificationCreationError::NulError(src)
}
}
/// 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.
@ -85,10 +97,7 @@ impl Context {
if sys::notify_is_initted() == TRUE { if sys::notify_is_initted() == TRUE {
return Err(ContextCreationError::AlreadyExists); return Err(ContextCreationError::AlreadyExists);
} }
let app_name = match CString::new(app_name) { let app_name = try!(CString::new(app_name));
Ok(name) => name,
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);
} }
@ -107,15 +116,9 @@ impl Context {
body: Option<&str>, body: Option<&str>,
icon: Option<&str>) icon: Option<&str>)
-> Result<Notification, NotificationCreationError> { -> Result<Notification, NotificationCreationError> {
let summary = match CString::new(summary) { let summary = try!(CString::new(summary));
Ok(cstr) => cstr,
Err(e) => return Err(NotificationCreationError::NulError(e)),
};
let body = match body { let body = match body {
Some(body) => match CString::new(body) { Some(body) => Some(try!(CString::new(body))),
Ok(cstr) => Some(cstr),
Err(e) => return Err(NotificationCreationError::NulError(e)),
},
None => None, None => None,
}; };
let body_ptr = match body { let body_ptr = match body {
@ -123,10 +126,7 @@ impl Context {
None => std::ptr::null(), None => std::ptr::null(),
}; };
let icon = match icon { let icon = match icon {
Some(icon) => match CString::new(icon) { Some(icon) => Some(try!(CString::new(icon))),
Ok(cstr) => Some(cstr),
Err(e) => return Err(NotificationCreationError::NulError(e)),
},
None => None, None => None,
}; };
let icon_ptr = match icon { let icon_ptr = match icon {