From fdb5d1bb24377884fc8827d5895d1a0149017c3a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 26 May 2016 22:16:29 +0200 Subject: [PATCH 1/7] Replace old error construction code with new libimagerror functionality --- libimagstore/src/lazyfile.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libimagstore/src/lazyfile.rs b/libimagstore/src/lazyfile.rs index ec3e8f45..5079b686 100644 --- a/libimagstore/src/lazyfile.rs +++ b/libimagstore/src/lazyfile.rs @@ -1,3 +1,4 @@ +use libimagerror::into::IntoError; use error::{StoreError, StoreErrorKind}; use std::io::{Seek, SeekFrom}; @@ -39,14 +40,15 @@ impl LazyFile { // We seek to the beginning of the file since we expect each // access to the file to be in a different context f.seek(SeekFrom::Start(0)) - .map_err(|e| StoreError::new(StoreErrorKind::FileNotCreated, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| StoreErrorKind::FileNotCreated.into_error_with_cause(e)) .map(|_| f) }, LazyFile::Absent(ref p) => { - try!(open_file(p).map_err(|e| { - StoreError::new(StoreErrorKind::FileNotFound, - Some(Box::new(e))) - })) + try!(open_file(p) + .map_err(Box::new) + .map_err(|e| StoreErrorKind::FileNotFound.into_error_with_cause(e)) + ) } }; *self = LazyFile::File(file); @@ -64,10 +66,10 @@ impl LazyFile { let file = match *self { LazyFile::File(ref mut f) => return Ok(f), LazyFile::Absent(ref p) => { - try!(create_file(p).map_err(|e| { - StoreError::new(StoreErrorKind::FileNotFound, - Some(Box::new(e))) - })) + try!(create_file(p) + .map_err(Box::new) + .map_err(|e| StoreErrorKind::FileNotFound.into_error_with_cause(e)) + ) } }; *self = LazyFile::File(file); From 25c4bea8189c180802367f50f034a79f57071e50 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 26 May 2016 22:17:08 +0200 Subject: [PATCH 2/7] Shorten type names in import --- libimagstore/src/lazyfile.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libimagstore/src/lazyfile.rs b/libimagstore/src/lazyfile.rs index 5079b686..1e5cd66d 100644 --- a/libimagstore/src/lazyfile.rs +++ b/libimagstore/src/lazyfile.rs @@ -1,6 +1,6 @@ use libimagerror::into::IntoError; -use error::{StoreError, StoreErrorKind}; +use error::{StoreError as SE, StoreErrorKind as SEK}; use std::io::{Seek, SeekFrom}; use std::path::{Path, PathBuf}; use std::fs::{File, OpenOptions, create_dir_all}; @@ -33,7 +33,7 @@ impl LazyFile { /** * Get the mutable file behind a LazyFile object */ - pub fn get_file_mut(&mut self) -> Result<&mut File, StoreError> { + pub fn get_file_mut(&mut self) -> Result<&mut File, SE> { debug!("Getting lazy file: {:?}", self); let file = match *self { LazyFile::File(ref mut f) => return { @@ -41,13 +41,13 @@ impl LazyFile { // access to the file to be in a different context f.seek(SeekFrom::Start(0)) .map_err(Box::new) - .map_err(|e| StoreErrorKind::FileNotCreated.into_error_with_cause(e)) + .map_err(|e| SEK::FileNotCreated.into_error_with_cause(e)) .map(|_| f) }, LazyFile::Absent(ref p) => { try!(open_file(p) .map_err(Box::new) - .map_err(|e| StoreErrorKind::FileNotFound.into_error_with_cause(e)) + .map_err(|e| SEK::FileNotFound.into_error_with_cause(e)) ) } }; @@ -61,14 +61,14 @@ impl LazyFile { /** * Create a file out of this LazyFile object */ - pub fn create_file(&mut self) -> Result<&mut File, StoreError> { + pub fn create_file(&mut self) -> Result<&mut File, SE> { debug!("Creating lazy file: {:?}", self); let file = match *self { LazyFile::File(ref mut f) => return Ok(f), LazyFile::Absent(ref p) => { try!(create_file(p) .map_err(Box::new) - .map_err(|e| StoreErrorKind::FileNotFound.into_error_with_cause(e)) + .map_err(|e| SEK::FileNotFound.into_error_with_cause(e)) ) } }; From 5e98bd76b1e8b4ca1faba5832bdc5aeb5731a781 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 26 May 2016 22:17:53 +0200 Subject: [PATCH 3/7] Shorten type names in import --- libimagstore/src/storeid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index d6631333..140797f9 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -8,7 +8,7 @@ use std::fmt::{Debug, Formatter}; use std::fmt::Error as FmtError; use std::result::Result as RResult; -use error::{StoreError, StoreErrorKind}; +use error::{StoreError as SE, StoreErrorKind as SEK}; use store::Result; use store::Store; @@ -88,7 +88,7 @@ pub fn build_entry_path(store: &Store, path_elem: &str) -> Result { if path_elem.split('~').last().map_or(false, |v| Version::parse(v).is_err()) { debug!("Version cannot be parsed from {:?}", path_elem); debug!("Path does not contain version!"); - return Err(StoreError::new(StoreErrorKind::StorePathLacksVersion, None)); + return Err(SE::new(SEK::StorePathLacksVersion, None)); } debug!("Version checking succeeded"); From e91eb2f55d007ba28ef44f36941616901d7f050b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 26 May 2016 22:19:29 +0200 Subject: [PATCH 4/7] Use error.into() instead of building full type --- libimagstore/src/storeid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 140797f9..147b80cb 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -8,7 +8,7 @@ use std::fmt::{Debug, Formatter}; use std::fmt::Error as FmtError; use std::result::Result as RResult; -use error::{StoreError as SE, StoreErrorKind as SEK}; +use error::StoreErrorKind as SEK; use store::Result; use store::Store; @@ -88,7 +88,7 @@ pub fn build_entry_path(store: &Store, path_elem: &str) -> Result { if path_elem.split('~').last().map_or(false, |v| Version::parse(v).is_err()) { debug!("Version cannot be parsed from {:?}", path_elem); debug!("Path does not contain version!"); - return Err(SE::new(SEK::StorePathLacksVersion, None)); + return Err(SEK::StorePathLacksVersion.into()); } debug!("Version checking succeeded"); From 183c46a7ddacf88d9bd1f151176392e9e355eeae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 May 2016 10:11:51 +0200 Subject: [PATCH 5/7] Use Into implementations to convert error kinds into error types --- libimagstore/src/store.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index b7013b4d..c3962f11 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -33,6 +33,8 @@ use hook::accessor::{ MutableHookDataAccessor, use hook::position::HookPosition; use hook::Hook; +use libimagerror::into::IntoError; + use self::glob_store_iter::*; /// The Result Type returned by any interaction with the store that could fail @@ -739,7 +741,7 @@ impl EntryHeader { let mut parser = Parser::new(s); parser.parse() - .ok_or(ParserError::new(ParserErrorKind::TOMLParserErrors, None)) + .ok_or(ParserErrorKind::TOMLParserErrors.into()) .and_then(verify_header_consistency) .map(EntryHeader::from_table) } @@ -1151,12 +1153,12 @@ fn build_default_header() -> Value { // BTreeMap } fn verify_header(t: &Table) -> Result<()> { if !has_main_section(t) { - Err(SE::from(ParserError::new(ParserErrorKind::MissingMainSection, None))) + Err(SE::from(ParserErrorKind::MissingMainSection.into_error())) } else if !has_imag_version_in_main_section(t) { - Err(SE::from(ParserError::new(ParserErrorKind::MissingVersionInfo, None))) + Err(SE::from(ParserErrorKind::MissingVersionInfo.into_error())) } else if !has_only_tables(t) { debug!("Could not verify that it only has tables in its base table"); - Err(SE::from(ParserError::new(ParserErrorKind::NonTableInBaseTable, None))) + Err(SE::from(ParserErrorKind::NonTableInBaseTable.into_error())) } else { Ok(()) } @@ -1164,7 +1166,8 @@ fn verify_header(t: &Table) -> Result<()> { fn verify_header_consistency(t: Table) -> EntryResult { verify_header(&t) - .map_err(|e| ParserError::new(ParserErrorKind::HeaderInconsistency, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| ParserErrorKind::HeaderInconsistency.into_error_with_cause(e)) .map(|_| t) } From cb87b4b5d8893c3ce856ddabb885e4eb30c6e777 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 May 2016 10:13:03 +0200 Subject: [PATCH 6/7] Remove old error code which is unused --- libimagstore/src/hook/error.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs index 472dc142..451fb5a5 100644 --- a/libimagstore/src/hook/error.rs +++ b/libimagstore/src/hook/error.rs @@ -6,16 +6,3 @@ generate_error_types!(HookError, HookErrorKind, AccessTypeViolation => "Hook access type violation" ); -pub trait IntoHookError { - fn into_hookerror(self) -> HookError; - fn into_hookerror_with_cause(self, cause: Box) -> HookError; -} - -impl Into for (HookErrorKind, Box) { - - fn into(self) -> HookError { - HookError::new(self.0, Some(self.1)) - } - -} - From 3d9d5795e4948c427dd9936521b0f6c3ca676837 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 May 2016 10:15:29 +0200 Subject: [PATCH 7/7] Rewrite hook error module with error macros --- libimagstore/src/hook/error.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs index 451fb5a5..afe9ef07 100644 --- a/libimagstore/src/hook/error.rs +++ b/libimagstore/src/hook/error.rs @@ -1,8 +1,10 @@ -generate_error_imports!(); -use std::convert::Into; - -generate_error_types!(HookError, HookErrorKind, - HookExecutionError => "Hook exec error", - AccessTypeViolation => "Hook access type violation" +generate_error_module!( + generate_error_types!(HookError, HookErrorKind, + HookExecutionError => "Hook exec error", + AccessTypeViolation => "Hook access type violation" + ); ); +pub use self::error::HookError; +pub use self::error::HookErrorKind; +