From d3f5ca027545db5c0e54ceb758bcd4e27e2ce003 Mon Sep 17 00:00:00 2001 From: "C. Morgan Hamill" Date: Fri, 20 Mar 2015 08:46:32 -0400 Subject: Implement `Error` for `NotmuchStatus`. Entails implementation of `ToStr` and `Display`, also. Note that the `to_str()` method uses the `to_static_str()` method on the `ToStaticStr` trait internally --- this is how I expect to use the latter. This lets me provide a uniform `to_str()` method in the rest of the API, while transparently handling the differences between C string lifetimes. --- src/ffi.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ffi.rs b/src/ffi.rs index 2ee99e0..45a1f17 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -11,7 +11,17 @@ use libc::{ time_t, }; -use utils::NotmuchEnum; +use std::{ + error, + fmt, + str, +}; + +use utils::{ + NotmuchEnum, + ToStaticStr, + ToStr, +}; pub type notmuch_bool_t = c_int; @@ -56,6 +66,26 @@ impl notmuch_status_t { } } +impl ToStr for NotmuchStatus { + fn to_str(&self) -> Result<&str, str::Utf8Error> { + unsafe { + notmuch_status_to_string(self.to_notmuch_t()) + }.to_static_str() + } +} + +impl fmt::Display for NotmuchStatus { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_str().unwrap()) + } +} + +impl error::Error for NotmuchStatus { + fn description(&self) -> &str { + self.to_str().unwrap() + } +} + notmuch_enum! { #[repr(C)] #[derive(Copy, Debug)] -- cgit v1.2.1