lazyfile.rs: Replace error boxing and creation by call to new helper function

This commit is contained in:
Matthias Beyer 2016-06-27 18:00:12 +02:00
parent 0a05b9825c
commit 484520842a

View file

@ -1,6 +1,4 @@
use libimagerror::into::IntoError; use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
use error::{StoreError as SE, StoreErrorKind as SEK};
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::fs::{File, OpenOptions, create_dir_all}; use std::fs::{File, OpenOptions, create_dir_all};
@ -40,16 +38,10 @@ impl LazyFile {
// We seek to the beginning of the file since we expect each // We seek to the beginning of the file since we expect each
// access to the file to be in a different context // access to the file to be in a different context
f.seek(SeekFrom::Start(0)) f.seek(SeekFrom::Start(0))
.map_err(Box::new) .map_err_into(SEK::FileNotCreated)
.map_err(|e| SEK::FileNotCreated.into_error_with_cause(e))
.map(|_| f) .map(|_| f)
}, },
LazyFile::Absent(ref p) => { LazyFile::Absent(ref p) => try!(open_file(p).map_err_into(SEK::FileNotFound)),
try!(open_file(p)
.map_err(Box::new)
.map_err(|e| SEK::FileNotFound.into_error_with_cause(e))
)
}
}; };
*self = LazyFile::File(file); *self = LazyFile::File(file);
if let LazyFile::File(ref mut f) = *self { if let LazyFile::File(ref mut f) = *self {
@ -65,12 +57,7 @@ impl LazyFile {
debug!("Creating lazy file: {:?}", self); debug!("Creating lazy file: {:?}", self);
let file = match *self { let file = match *self {
LazyFile::File(ref mut f) => return Ok(f), LazyFile::File(ref mut f) => return Ok(f),
LazyFile::Absent(ref p) => { LazyFile::Absent(ref p) => try!(create_file(p).map_err_into(SEK::FileNotFound)),
try!(create_file(p)
.map_err(Box::new)
.map_err(|e| SEK::FileNotFound.into_error_with_cause(e))
)
}
}; };
*self = LazyFile::File(file); *self = LazyFile::File(file);
if let LazyFile::File(ref mut f) = *self { if let LazyFile::File(ref mut f) = *self {