Mercurial > lbo > hg > leveldb-rs
changeset 142:06c8d65b6367
Implement Error + Display for Status and add conversion function
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 02 Jan 2017 17:37:09 +0100 |
parents | ad36c0aba2f2 |
children | eaccb6b7d5ed |
files | src/memtable.rs src/types.rs |
diffstat | 2 files changed, 54 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/src/memtable.rs Mon Jan 02 15:42:18 2017 +0100 +++ b/src/memtable.rs Mon Jan 02 17:37:09 2017 +0100 @@ -187,26 +187,26 @@ let mt = get_memtable(); // Smaller sequence number doesn't find entry - if let Result::Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 110)) { + if let Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 110)) { println!("{:?}", v); panic!("found"); } // Bigger sequence number falls back to next smaller - if let Result::Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 116)) { + if let Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 116)) { assert_eq!(v, "122".as_bytes()); } else { panic!("not found"); } // Exact match works - if let Result::Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 120)) { + if let Ok(v) = mt.get(&LookupKey::new("abc".as_bytes(), 120)) { assert_eq!(v, "123".as_bytes()); } else { panic!("not found"); } - if let Result::Ok(v) = mt.get(&LookupKey::new("abe".as_bytes(), 122)) { + if let Ok(v) = mt.get(&LookupKey::new("abe".as_bytes(), 122)) { assert_eq!(v, "125".as_bytes()); } else { panic!("not found");
--- a/src/types.rs Mon Jan 02 15:42:18 2017 +0100 +++ b/src/types.rs Mon Jan 02 17:37:09 2017 +0100 @@ -1,5 +1,10 @@ //! A collection of fundamental and/or simple types used by other modules +use std::error::Error; +use std::fmt::{self, Display, Formatter}; +use std::io; +use std::result; + #[derive(Debug, PartialOrd, PartialEq)] pub enum ValueType { TypeDeletion = 0, @@ -19,7 +24,52 @@ Corruption(String), NotSupported(String), InvalidArgument(String), + PermissionDenied(String), IOError(String), + Unknown(String), +} + +impl Display for Status { + fn fmt(&self, fmt: &mut Formatter) -> result::Result<(), fmt::Error> { + fmt.write_str(self.description()) + } +} + +impl Error for Status { + fn description(&self) -> &str { + match *self { + Status::OK => "ok", + Status::NotFound(ref s) => s, + Status::Corruption(ref s) => s, + Status::NotSupported(ref s) => s, + Status::InvalidArgument(ref s) => s, + Status::PermissionDenied(ref s) => s, + Status::IOError(ref s) => s, + Status::Unknown(ref s) => s, + } + } +} + +/// LevelDB's result type +pub type Result<T> = result::Result<T, Status>; + +pub fn from_io_result<T>(e: io::Result<T>) -> Result<T> { + match e { + Ok(r) => result::Result::Ok(r), + Err(e) => { + let err = e.description().to_string(); + + let r = match e.kind() { + io::ErrorKind::NotFound => Err(Status::NotFound(err)), + io::ErrorKind::InvalidData => Err(Status::Corruption(err)), + io::ErrorKind::InvalidInput => Err(Status::InvalidArgument(err)), + io::ErrorKind::PermissionDenied => Err(Status::PermissionDenied(err)), + _ => Err(Status::IOError(err)), + }; + + r + } + } } /// Denotes a key range