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,22 +1,79 @@
|
||||||
use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
|
pub use self::fs::LazyFile;
|
||||||
use std::io::{Seek, SeekFrom};
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::fs::{File, OpenOptions, create_dir_all};
|
|
||||||
|
|
||||||
/// `LazyFile` type
|
#[cfg(test)]
|
||||||
///
|
mod fs {
|
||||||
/// A lazy file is either absent, but a path to it is available, or it is present.
|
use error::StoreError as SE;
|
||||||
#[derive(Debug)]
|
use std::io::Cursor;
|
||||||
pub enum LazyFile {
|
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};
|
||||||
|
use std::fs::{File, OpenOptions, create_dir_all};
|
||||||
|
|
||||||
|
/// `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),
|
Absent(PathBuf),
|
||||||
File(File)
|
File(File)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
fn open_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
||||||
OpenOptions::new().write(true).read(true).open(p)
|
OpenOptions::new().write(true).read(true).open(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
fn create_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
||||||
if let Some(parent) = p.as_ref().parent() {
|
if let Some(parent) = p.as_ref().parent() {
|
||||||
debug!("Implicitely creating directory: {:?}", parent);
|
debug!("Implicitely creating directory: {:?}", parent);
|
||||||
if let Err(e) = create_dir_all(parent) {
|
if let Err(e) = create_dir_all(parent) {
|
||||||
|
@ -24,9 +81,9 @@ fn create_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpenOptions::new().write(true).read(true).create(true).open(p)
|
OpenOptions::new().write(true).read(true).create(true).open(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LazyFile {
|
impl LazyFile {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the mutable file behind a LazyFile object
|
* Get the mutable file behind a LazyFile object
|
||||||
|
@ -56,15 +113,20 @@ impl LazyFile {
|
||||||
pub fn create_file(&mut self) -> Result<&mut File, SE> {
|
pub fn create_file(&mut self) -> Result<&mut File, SE> {
|
||||||
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) => {
|
||||||
|
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)),
|
LazyFile::Absent(ref p) => try!(create_file(p).map_err_into(SEK::FileNotFound)),
|
||||||
};
|
};
|
||||||
*self = LazyFile::File(file);
|
*self = LazyFile::File(file);
|
||||||
if let LazyFile::File(ref mut f) = *self {
|
if let LazyFile::File(ref mut f) = *self {
|
||||||
|
try!(f.set_len(0).map_err_into(SEK::FileError));
|
||||||
return Ok(f);
|
return Ok(f);
|
||||||
}
|
}
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue