add import directory file and errors

This commit is contained in:
collin 2020-06-27 23:48:55 -07:00
parent 878310f793
commit a9338a9756
5 changed files with 71 additions and 2 deletions

View File

@ -0,0 +1,33 @@
use crate::errors::ImportsDirectoryError;
use std::{fs, path::PathBuf};
pub(crate) static IMPORTS_DIRECTORY_NAME: &str = "imports/";
pub struct ImportsDirectory;
impl ImportsDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &PathBuf) -> Result<(), ImportsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
path.push(PathBuf::from(IMPORTS_DIRECTORY_NAME));
}
fs::create_dir_all(&path).map_err(ImportsDirectoryError::Creating)
}
/// Removes the directory at the provided path.
pub fn remove(path: &PathBuf) -> Result<(), ImportsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
path.push(PathBuf::from(IMPORTS_DIRECTORY_NAME));
}
if path.exists() {
fs::remove_dir_all(&path).map_err(ImportsDirectoryError::Removing)?;
}
Ok(())
}
}

View File

@ -1,3 +1,6 @@
pub mod imports;
pub use self::imports::*;
pub mod inputs;
pub use self::inputs::*;

View File

@ -0,0 +1,28 @@
use std::{ffi::OsString, fs::FileType, io};
#[derive(Debug, Error)]
pub enum ImportsDirectoryError {
#[error("creating: {}", _0)]
Creating(io::Error),
#[error("file entry getting: {}", _0)]
GettingFileEntry(io::Error),
#[error("file {:?} extension getting", _0)]
GettingFileExtension(OsString),
#[error("file {:?} type getting: {}", _0, _1)]
GettingFileType(OsString, io::Error),
#[error("invalid file {:?} extension: {:?}", _0, _1)]
InvalidFileExtension(OsString, OsString),
#[error("invalid file {:?} type: {:?}", _0, _1)]
InvalidFileType(OsString, FileType),
#[error("reading: {}", _0)]
Reading(io::Error),
#[error("removing: {}", _0)]
Removing(io::Error),
}

View File

@ -1,3 +1,6 @@
pub mod imports;
pub use self::imports::*;
pub mod inputs;
pub use self::inputs::*;

View File

@ -1,7 +1,7 @@
//! The program package zip file.
use crate::{
directories::{INPUTS_DIRECTORY_NAME, OUTPUTS_DIRECTORY_NAME},
directories::{IMPORTS_DIRECTORY_NAME, INPUTS_DIRECTORY_NAME, OUTPUTS_DIRECTORY_NAME},
errors::ZipFileError,
files::{
CHECKSUM_FILE_EXTENSION,
@ -109,9 +109,11 @@ impl ZipFile {
}
fn is_excluded(path: &Path) -> bool {
// excluded directories: `/inputs`, `/outputs`
// excluded directories: `inputs`, `outputs`, `imports`
if path.ends_with(INPUTS_DIRECTORY_NAME.trim_end_matches("/"))
| path.ends_with(OUTPUTS_DIRECTORY_NAME.trim_end_matches("/"))
| path.ends_with(IMPORTS_DIRECTORY_NAME.trim_end_matches("/"))
| path
{
return true;
}