Split up LazyFile for test/prod
This commit is contained in:
parent
363a1d246a
commit
1d1ad65705
1 changed files with 123 additions and 61 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue