Split up LazyFile for test/prod

This commit is contained in:
Marcel Müller 2016-07-21 16:00:59 +02:00
parent 363a1d246a
commit 1d1ad65705
No known key found for this signature in database
GPG key ID: 84BD3634786D56CF

View file

@ -1,3 +1,60 @@
pub use self::fs::LazyFile;
#[cfg(test)]
mod fs {
use error::StoreError as SE;
use std::io::Cursor;
use std::path::PathBuf;
/// `LazyFile` type
///
/// A lazy file is either absent, but a path to it is available, or it is present.
#[derive(Debug)]
pub enum LazyFile {
Absent(PathBuf),
File(Cursor<Vec<u8>>)
}
impl LazyFile {
/**
* Get the mutable file behind a LazyFile object
*/
pub fn get_file_mut(&mut self) -> Result<&mut Cursor<Vec<u8>>, SE> {
debug!("Getting lazy file: {:?}", self);
let file = match *self {
LazyFile::File(ref mut f) => return {
Ok(f)
},
LazyFile::Absent(ref p) => unreachable!(),
};
*self = LazyFile::File(file);
if let LazyFile::File(ref mut f) = *self {
return Ok(f);
}
unreachable!()
}
/**
* Create a file out of this LazyFile object
*/
pub fn create_file(&mut self) -> Result<&mut Cursor<Vec<u8>>, SE> {
debug!("Creating lazy file: {:?}", self);
let file = match *self {
LazyFile::File(ref mut f) => return Ok(f),
LazyFile::Absent(ref p) => unreachable!(),
};
*self = LazyFile::File(file);
if let LazyFile::File(ref mut f) = *self {
return Ok(f);
}
unreachable!()
}
}
}
#[cfg(not(test))]
mod fs {
use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
use std::io::{Seek, SeekFrom};
use std::path::{Path, PathBuf};
@ -56,16 +113,21 @@ impl LazyFile {
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::File(ref mut f) => {
try!(f.set_len(0).map_err_into(SEK::FileError));
return Ok(f)
},
LazyFile::Absent(ref p) => try!(create_file(p).map_err_into(SEK::FileNotFound)),
};
*self = LazyFile::File(file);
if let LazyFile::File(ref mut f) = *self {
try!(f.set_len(0).map_err_into(SEK::FileError));
return Ok(f);
}
unreachable!()
}
}
}
#[cfg(test)]
mod test {