diff options
| author | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 14:15:02 +0100 |
|---|---|---|
| committer | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 14:21:01 +0100 |
| commit | 107624c5bb11e6ab8c72d85392f9645efd7c6545 (patch) | |
| tree | 3a7bd7c512ee1debbe42618f3a9c74690231fe5c | |
| parent | d3f5ca027545db5c0e54ceb758bcd4e27e2ce003 (diff) | |
| download | mail-107624c5bb11e6ab8c72d85392f9645efd7c6545.tar.gz | |
Add error module with crate's `Error` type.
Implement `Display`, `Error`, and various `FromError<T>` traits on
`Error`, allowing the use of `try!` on the various `Result` types in the
crate.
Note that currently the only error variants are those thrown from
a lower-level operation.
| -rw-r--r-- | src/error.rs | 47 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..57d636a --- /dev/null +++ b/src/error.rs @@ -0,0 +1,47 @@ +use std::{ + error, + fmt, + io, +}; + +use utils::NotmuchEnum; +use ffi; + +#[derive(Debug)] +pub enum Error { + IoError(io::Error), + NotmuchError(ffi::NotmuchStatus), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", error::Error::description(self)) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::IoError(ref e) => error::Error::description(e), + Error::NotmuchError(ref e) => e.description(), + } + } +} + +impl error::FromError<io::Error> for Error { + fn from_error(err: io::Error) -> Error { + Error::IoError(err) + } +} + +impl error::FromError<ffi::NotmuchStatus> for Error { + fn from_error(err: ffi::NotmuchStatus) -> Error { + Error::NotmuchError(err) + } +} + +impl error::FromError<ffi::notmuch_status_t> for Error { + fn from_error(err: ffi::notmuch_status_t) -> Error { + Error::NotmuchError(ffi::NotmuchStatus::from_notmuch_t(err)) + } +} @@ -6,3 +6,5 @@ mod macros; mod ffi; mod utils; + +pub mod error; |
