Implement From<ffi::NulError> for error types that have this variant
This commit is contained in:
parent
d282d2cd50
commit
8daef60a03
32
src/lib.rs
32
src/lib.rs
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user