diff --git a/leo/directories/imports.rs b/leo/directories/imports.rs new file mode 100644 index 0000000000..2036deb835 --- /dev/null +++ b/leo/directories/imports.rs @@ -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(()) + } +} diff --git a/leo/directories/mod.rs b/leo/directories/mod.rs index 31f99c6104..6a37fa8b54 100644 --- a/leo/directories/mod.rs +++ b/leo/directories/mod.rs @@ -1,3 +1,6 @@ +pub mod imports; +pub use self::imports::*; + pub mod inputs; pub use self::inputs::*; diff --git a/leo/errors/directory/imports.rs b/leo/errors/directory/imports.rs new file mode 100644 index 0000000000..7dd2114fc0 --- /dev/null +++ b/leo/errors/directory/imports.rs @@ -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), +} diff --git a/leo/errors/directory/mod.rs b/leo/errors/directory/mod.rs index 31f99c6104..6a37fa8b54 100644 --- a/leo/errors/directory/mod.rs +++ b/leo/errors/directory/mod.rs @@ -1,3 +1,6 @@ +pub mod imports; +pub use self::imports::*; + pub mod inputs; pub use self::inputs::*; diff --git a/leo/files/zip.rs b/leo/files/zip.rs index d536c00838..c817f826f1 100644 --- a/leo/files/zip.rs +++ b/leo/files/zip.rs @@ -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; }