mirror of
https://github.com/AleoHQ/leo.git
synced 2025-01-03 15:28:05 +03:00
Adds input directory to
This commit is contained in:
parent
2f1e5acec4
commit
dcb5f542a8
@ -1,5 +1,5 @@
|
||||
use crate::{cli::*, cli_types::*};
|
||||
use crate::directories::SourceDirectory;
|
||||
use crate::directories::{InputsDirectory, SourceDirectory};
|
||||
use crate::errors::{CLIError, InitError};
|
||||
use crate::files::MainFile;
|
||||
use crate::manifest::Manifest;
|
||||
@ -53,6 +53,9 @@ impl CLI for InitCommand {
|
||||
// Create the source directory
|
||||
SourceDirectory::create(&path)?;
|
||||
|
||||
// Create the inputs directory
|
||||
InputsDirectory::create(&path)?;
|
||||
|
||||
// Create the main file in the source directory
|
||||
if !MainFile::exists_at(&path) {
|
||||
MainFile::new(&package_name).write_to(&path)?;
|
||||
|
61
leo/directories/inputs.rs
Normal file
61
leo/directories/inputs.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::errors::InputsDirectoryError;
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub(crate) static INPUTS_DIRECTORY_NAME: &str = "inputs/";
|
||||
|
||||
static INPUTS_FILE_EXTENSION: &str = "leo.in";
|
||||
|
||||
pub struct InputsDirectory;
|
||||
|
||||
impl InputsDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &PathBuf) -> Result<(), InputsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
||||
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(InputsDirectoryError::Creating)
|
||||
}
|
||||
|
||||
/// Returns a list of files in the source directory.
|
||||
pub fn files(path: &PathBuf) -> Result<Vec<PathBuf>, InputsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
|
||||
let directory = fs::read_dir(&path).map_err(InputsDirectoryError::Reading)?;
|
||||
|
||||
let mut file_paths = Vec::new();
|
||||
for file_entry in directory.into_iter() {
|
||||
let file_entry = file_entry.map_err(InputsDirectoryError::GettingFileEntry)?;
|
||||
let file_path = file_entry.path();
|
||||
|
||||
// Verify that the entry is structured as a valid file
|
||||
let file_type = file_entry
|
||||
.file_type()
|
||||
.map_err(|error| InputsDirectoryError::GettingFileType(file_path.as_os_str().to_owned(), error))?;
|
||||
if !file_type.is_file() {
|
||||
return Err(InputsDirectoryError::InvalidFileType(
|
||||
file_path.as_os_str().to_owned(),
|
||||
file_type,
|
||||
));
|
||||
}
|
||||
|
||||
// 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 != INPUTS_FILE_EXTENSION {
|
||||
return Err(InputsDirectoryError::InvalidFileExtension(
|
||||
file_path.as_os_str().to_owned(),
|
||||
file_extension.to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
file_paths.push(file_path);
|
||||
}
|
||||
|
||||
Ok(file_paths)
|
||||
}
|
||||
}
|
@ -1,2 +1,5 @@
|
||||
pub mod inputs;
|
||||
pub use self::inputs::*;
|
||||
|
||||
pub mod source;
|
||||
pub use self::source::*;
|
||||
|
@ -3,9 +3,9 @@ use crate::errors::SourceDirectoryError;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub(crate) static DIRECTORY_NAME_DEFAULT: &str = "src/";
|
||||
pub(crate) static SOURCE_DIRECTORY_NAME: &str = "src/";
|
||||
|
||||
static SOURCE_FILE_EXTENSION_DEFAULT: &str = "leo";
|
||||
static SOURCE_FILE_EXTENSION: &str = "leo";
|
||||
|
||||
pub struct SourceDirectory;
|
||||
|
||||
@ -13,8 +13,8 @@ impl SourceDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &PathBuf) -> Result<(), SourceDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
if path.is_dir() && !path.ends_with(DIRECTORY_NAME_DEFAULT) {
|
||||
path.push(PathBuf::from(DIRECTORY_NAME_DEFAULT));
|
||||
if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(PathBuf::from(SOURCE_DIRECTORY_NAME));
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(SourceDirectoryError::Creating)
|
||||
@ -23,7 +23,7 @@ impl SourceDirectory {
|
||||
/// Returns a list of files in the source directory.
|
||||
pub fn files(path: &PathBuf) -> Result<Vec<PathBuf>, SourceDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
path.push(PathBuf::from(DIRECTORY_NAME_DEFAULT));
|
||||
path.push(PathBuf::from(SOURCE_DIRECTORY_NAME));
|
||||
let directory = fs::read_dir(&path).map_err(SourceDirectoryError::Reading)?;
|
||||
|
||||
let mut file_paths = Vec::new();
|
||||
@ -46,7 +46,7 @@ impl SourceDirectory {
|
||||
let file_extension = file_path
|
||||
.extension()
|
||||
.ok_or_else(|| SourceDirectoryError::GettingFileExtension(file_path.as_os_str().to_owned()))?;
|
||||
if file_extension != SOURCE_FILE_EXTENSION_DEFAULT {
|
||||
if file_extension != SOURCE_FILE_EXTENSION {
|
||||
return Err(SourceDirectoryError::InvalidFileExtension(
|
||||
file_path.as_os_str().to_owned(),
|
||||
file_extension.to_owned(),
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::errors::{InitError, MainFileError, ManifestError, SourceDirectoryError};
|
||||
use crate::errors::{InitError, InputsDirectoryError, MainFileError, ManifestError, SourceDirectoryError};
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
pub enum CLIError {
|
||||
@ -9,6 +9,9 @@ pub enum CLIError {
|
||||
#[fail(display = "{}", _0)]
|
||||
InitError(InitError),
|
||||
|
||||
#[fail(display = "{}", _0)]
|
||||
InputsDirectoryError(InputsDirectoryError),
|
||||
|
||||
#[fail(display = "{}", _0)]
|
||||
MainFileError(MainFileError),
|
||||
|
||||
@ -26,6 +29,12 @@ impl From<InitError> for CLIError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<InputsDirectoryError> for CLIError {
|
||||
fn from(error: InputsDirectoryError) -> Self {
|
||||
CLIError::InputsDirectoryError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MainFileError> for CLIError {
|
||||
fn from(error: MainFileError) -> Self {
|
||||
CLIError::MainFileError(error)
|
||||
|
27
leo/errors/directory/inputs.rs
Normal file
27
leo/errors/directory/inputs.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use std::{ffi::OsString, fs::FileType, io};
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
pub enum InputsDirectoryError {
|
||||
|
||||
#[fail(display = "creating: {}", _0)]
|
||||
Creating(io::Error),
|
||||
|
||||
#[fail(display = "file entry getting: {}", _0)]
|
||||
GettingFileEntry(io::Error),
|
||||
|
||||
#[fail(display = "file {:?} extension getting", _0)]
|
||||
GettingFileExtension(OsString),
|
||||
|
||||
#[fail(display = "file {:?} type getting: {}", _0, _1)]
|
||||
GettingFileType(OsString, io::Error),
|
||||
|
||||
#[fail(display = "invalid file {:?} extension: {:?}", _0, _1)]
|
||||
InvalidFileExtension(OsString, OsString),
|
||||
|
||||
#[fail(display = "invalid file {:?} type: {:?}", _0, _1)]
|
||||
InvalidFileType(OsString, FileType),
|
||||
|
||||
#[fail(display = "reading: {}", _0)]
|
||||
Reading(io::Error),
|
||||
|
||||
}
|
@ -1,2 +1,5 @@
|
||||
pub mod inputs;
|
||||
pub use self::inputs::*;
|
||||
|
||||
pub mod source;
|
||||
pub use self::source::*;
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! The `main.leo` file.
|
||||
|
||||
use crate::directories::source::DIRECTORY_NAME_DEFAULT;
|
||||
use crate::directories::source::SOURCE_DIRECTORY_NAME;
|
||||
use crate::errors::MainFileError;
|
||||
|
||||
use serde::Deserialize;
|
||||
@ -23,8 +23,8 @@ impl MainFile {
|
||||
pub fn exists_at(path: &PathBuf) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(DIRECTORY_NAME_DEFAULT) {
|
||||
path.push(PathBuf::from(DIRECTORY_NAME_DEFAULT));
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(PathBuf::from(SOURCE_DIRECTORY_NAME));
|
||||
}
|
||||
path.push(PathBuf::from(FILE_NAME_DEFAULT));
|
||||
}
|
||||
@ -34,8 +34,8 @@ impl MainFile {
|
||||
pub fn write_to(self, path: &PathBuf) -> Result<(), MainFileError> {
|
||||
let mut path = path.to_owned();
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(DIRECTORY_NAME_DEFAULT) {
|
||||
path.push(PathBuf::from(DIRECTORY_NAME_DEFAULT));
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(PathBuf::from(SOURCE_DIRECTORY_NAME));
|
||||
}
|
||||
path.push(PathBuf::from(FILE_NAME_DEFAULT));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user