impl input state file pairs

This commit is contained in:
collin 2020-08-15 20:40:56 -07:00
parent 779d926e69
commit fcd8de4a9d
4 changed files with 96 additions and 14 deletions

View File

@ -1,3 +1,5 @@
use crate::{InputFileError, StateFileError};
use std::{ffi::OsString, fs::FileType, io};
#[derive(Debug, Error)]
@ -11,15 +13,24 @@ pub enum InputsDirectoryError {
#[error("file {:?} extension getting", _0)]
GettingFileExtension(OsString),
#[error("file {:?} name getting", _0)]
GettingFileName(OsString),
#[error("file {:?} type getting: {}", _0, _1)]
GettingFileType(OsString, io::Error),
#[error("{}", _0)]
InputFileError(#[from] InputFileError),
#[error("invalid file {:?} extension: {:?}", _0, _1)]
InvalidFileExtension(OsString, OsString),
InvalidFileExtension(String, OsString),
#[error("invalid file {:?} type: {:?}", _0, _1)]
InvalidFileType(OsString, FileType),
#[error("reading: {}", _0)]
Reading(io::Error),
#[error("{}", _0)]
StateFileError(#[from] StateFileError),
}

View File

@ -1,4 +1,4 @@
use crate::{errors::InputsDirectoryError, inputs::INPUT_FILE_EXTENSION};
use crate::errors::InputsDirectoryError;
use std::{fs, path::PathBuf};
@ -17,7 +17,7 @@ impl InputsDirectory {
fs::create_dir_all(&path).map_err(InputsDirectoryError::Creating)
}
/// Returns a list of files in the source directory.
/// Returns a list of files in the input directory.
pub fn files(path: &PathBuf) -> Result<Vec<PathBuf>, InputsDirectoryError> {
let mut path = path.to_owned();
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
@ -39,17 +39,6 @@ impl InputsDirectory {
));
}
// Verify that the file has the default file extension
let file_extension = file_path
.extension()
.ok_or_else(|| InputsDirectoryError::GettingFileExtension(file_path.as_os_str().to_owned()))?;
if file_extension != INPUT_FILE_EXTENSION {
return Err(InputsDirectoryError::InvalidFileExtension(
file_path.as_os_str().to_owned(),
file_extension.to_owned(),
));
}
file_paths.push(file_path);
}

View File

@ -4,5 +4,8 @@ pub use directory::*;
pub mod input;
pub use input::*;
pub mod pairs;
pub use pairs::*;
pub mod state;
pub use state::*;

View File

@ -0,0 +1,79 @@
use crate::{
inputs::{InputFile, InputsDirectory, StateFile, INPUT_FILE_EXTENSION, STATE_FILE_EXTENSION},
InputsDirectoryError,
};
use std::{collections::HashMap, convert::TryFrom, path::PathBuf};
pub struct InputPairs {
/// Maps file names to input file pairs
pub pairs: HashMap<String, InputPair>,
}
pub struct InputPair {
pub input_file: String,
pub state_file: String,
}
impl InputPairs {
pub fn new() -> Self {
Self { pairs: HashMap::new() }
}
}
impl TryFrom<&PathBuf> for InputPairs {
type Error = InputsDirectoryError;
fn try_from(directory: &PathBuf) -> Result<Self, Self::Error> {
let files = InputsDirectory::files(directory)?;
let mut pairs = HashMap::<String, InputPair>::new();
for file in files {
let file_extension = file
.extension()
.ok_or_else(|| InputsDirectoryError::GettingFileExtension(file.as_os_str().to_owned()))?;
let file_name = file
.file_name()
.ok_or(InputsDirectoryError::GettingFileName(file.as_os_str().to_owned()))?
.to_str()
.ok_or(InputsDirectoryError::GettingFileName(file.as_os_str().to_owned()))?;
if file_extension == INPUT_FILE_EXTENSION {
let input_file = InputFile::new(file_name).read_from(&file)?;
if pairs.contains_key(file_name) {
let pair = pairs.get_mut(file_name).unwrap();
pair.input_file = input_file;
} else {
let pair = InputPair {
input_file,
state_file: "".to_owned(),
};
pairs.insert(file_name.to_owned(), pair);
}
} else if file_extension == STATE_FILE_EXTENSION {
let state_file = StateFile::new(file_name).read_from(&file)?;
if pairs.contains_key(file_name) {
let pair = pairs.get_mut(file_name).unwrap();
pair.state_file = state_file;
} else {
let pair = InputPair {
input_file: "".to_owned(),
state_file,
};
pairs.insert(file_name.to_owned(), pair);
}
} else {
return Err(InputsDirectoryError::InvalidFileExtension(
file_name.to_owned(),
file_extension.to_owned(),
));
}
}
Ok(InputPairs { pairs })
}
}