Reimplement StorageBackendError::{new,build} with Into<String>

This commit is contained in:
Matthias Beyer 2015-12-04 15:18:52 +01:00
parent 702f1b07ad
commit 03856cd3a1

View file

@ -42,7 +42,7 @@ impl StorageBackend {
}) })
}).or_else(|e| { }).or_else(|e| {
debug!("Creating failed, constructing error instance"); debug!("Creating failed, constructing error instance");
let mut serr = StorageBackendError::build( let mut serr = StorageBackendError::new(
"create_dir_all()", "create_dir_all()",
"Could not create store directories", "Could not create store directories",
Some(storepath) Some(storepath)
@ -83,7 +83,7 @@ impl StorageBackend {
Ok(v) Ok(v)
}) })
.map_err(|e| { .map_err(|e| {
let serr = StorageBackendError::build( let serr = StorageBackendError::new(
"iter_ids()", "iter_ids()",
"Cannot iter on file ids", "Cannot iter on file ids",
None); None);
@ -102,7 +102,7 @@ impl StorageBackend {
.into_iter()) .into_iter())
}) })
.map_err(|e| { .map_err(|e| {
let serr = StorageBackendError::build( let serr = StorageBackendError::new(
"iter_files()", "iter_files()",
"Cannot iter on files", "Cannot iter on files",
None); None);
@ -131,7 +131,7 @@ impl StorageBackend {
file.write_all(&string.clone().into_bytes()) file.write_all(&string.clone().into_bytes())
.map_err(|ioerr| { .map_err(|ioerr| {
debug!("Could not write file"); debug!("Could not write file");
let mut err = StorageBackendError::build( let mut err = StorageBackendError::new(
"File::write_all()", "File::write_all()",
"Could not write out File contents", "Could not write out File contents",
None None
@ -141,7 +141,7 @@ impl StorageBackend {
}) })
}).map_err(|writeerr| { }).map_err(|writeerr| {
debug!("Could not create file at '{}'", path); debug!("Could not create file at '{}'", path);
let mut err = StorageBackendError::build( let mut err = StorageBackendError::new(
"File::create()", "File::create()",
"Creating file on disk failed", "Creating file on disk failed",
None None
@ -171,7 +171,7 @@ impl StorageBackend {
file.write_all(&string.clone().into_bytes()) file.write_all(&string.clone().into_bytes())
.map_err(|ioerr| { .map_err(|ioerr| {
debug!("Could not write file"); debug!("Could not write file");
let mut err = StorageBackendError::build( let mut err = StorageBackendError::new(
"File::write()", "File::write()",
"Tried to write contents of this file, though operation did not succeed", "Tried to write contents of this file, though operation did not succeed",
Some(string) Some(string)
@ -181,7 +181,7 @@ impl StorageBackend {
}) })
}).map_err(|writeerr| { }).map_err(|writeerr| {
debug!("Could not write file at '{}'", path); debug!("Could not write file at '{}'", path);
let mut err = StorageBackendError::build( let mut err = StorageBackendError::new(
"File::open()", "File::open()",
"Tried to update contents of this file, though file doesn't exist", "Tried to update contents of this file, though file doesn't exist",
None None
@ -225,7 +225,7 @@ impl StorageBackend {
let fp = self.build_filepath(&file); let fp = self.build_filepath(&file);
remove_file(fp).map_err(|e| { remove_file(fp).map_err(|e| {
let mut serr = StorageBackendError::build( let mut serr = StorageBackendError::new(
"remove_file()", "remove_file()",
"File removal failed", "File removal failed",
Some(format!("{}", file)) Some(format!("{}", file))
@ -271,25 +271,13 @@ pub struct StorageBackendError {
} }
impl StorageBackendError { impl StorageBackendError {
fn new(action: String,
desc : String,
data : Option<String>) -> StorageBackendError
{
StorageBackendError {
action: action,
desc: desc,
data_dump: data,
caused_by: None,
}
}
fn build(action: &'static str, fn new<S>(action: S, desc: S, data: Option<String>) -> StorageBackendError
desc: &'static str, where S: Into<String>
data : Option<String>) -> StorageBackendError
{ {
StorageBackendError { StorageBackendError {
action: String::from(action), action: action.into(),
desc: String::from(desc), desc: desc.into(),
data_dump: data, data_dump: data,
caused_by: None, caused_by: None,
} }
@ -322,7 +310,7 @@ fn write_with_parser<'a, HP>(f: &File, p: &Parser<HP>) -> Result<String, Storage
{ {
p.write(f.contents()) p.write(f.contents())
.or_else(|err| { .or_else(|err| {
let mut serr = StorageBackendError::build( let mut serr = StorageBackendError::new(
"Parser::write()", "Parser::write()",
"Cannot translate internal representation of file contents into on-disk representation", "Cannot translate internal representation of file contents into on-disk representation",
None None