leo_package/inputs/
directory.rs1use leo_errors::{PackageError, Result};
18
19use std::{
20 borrow::Cow,
21 fs,
22 fs::ReadDir,
23 path::{Path, PathBuf},
24};
25
26pub static INPUTS_DIRECTORY_NAME: &str = "inputs/";
27
28pub struct InputsDirectory;
29
30impl InputsDirectory {
31 pub fn create(path: &Path) -> Result<()> {
33 let mut path = Cow::from(path);
34 if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
35 path.to_mut().push(INPUTS_DIRECTORY_NAME);
36 }
37
38 fs::create_dir_all(&path).map_err(PackageError::failed_to_create_inputs_directory)?;
39 Ok(())
40 }
41
42 pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
44 let mut path = path.to_owned();
45 path.push(INPUTS_DIRECTORY_NAME);
46
47 let directory = fs::read_dir(&path).map_err(PackageError::failed_to_read_inputs_directory)?;
48 let mut file_paths = Vec::new();
49 parse_file_paths(directory, &mut file_paths)?;
50
51 Ok(file_paths)
52 }
53}
54
55fn parse_file_paths(directory: ReadDir, file_paths: &mut Vec<PathBuf>) -> Result<()> {
56 for file_entry in directory {
57 let file_entry = file_entry.map_err(PackageError::failed_to_get_input_file_entry)?;
58 let file_path = file_entry.path();
59
60 let file_type = file_entry
62 .file_type()
63 .map_err(|e| PackageError::failed_to_get_input_file_type(file_path.as_os_str().to_owned(), e))?;
64 if file_type.is_dir() {
65 let directory = fs::read_dir(&file_path).map_err(PackageError::failed_to_read_inputs_directory)?;
66
67 parse_file_paths(directory, file_paths)?;
68 continue;
69 } else if !file_type.is_file() {
70 return Err(PackageError::invalid_input_file_type(file_path.as_os_str().to_owned(), file_type).into());
71 }
72
73 file_paths.push(file_path);
74 }
75
76 Ok(())
77}