Genericize LeoPackage interface

This commit is contained in:
raychu86 2020-08-28 23:14:22 -07:00
parent 75ebf0cef5
commit 920214e5cf
3 changed files with 85 additions and 2 deletions

View File

@ -48,6 +48,12 @@ impl From<crate::errors::OutputsDirectoryError> for PackageError {
}
}
impl From<crate::errors::READMEError> for PackageError {
fn from(error: crate::errors::READMEError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
}
}
impl From<crate::errors::SourceDirectoryError> for PackageError {
fn from(error: crate::errors::SourceDirectoryError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))

View File

@ -26,3 +26,19 @@ pub mod outputs;
pub mod package;
pub mod root;
pub mod source;
use std::path::PathBuf;
pub struct LeoPackage;
impl LeoPackage {
/// Creates a Leo package at the given path.
pub fn create(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
package::Package::create_package(package_name, is_lib, path)
}
/// Returns `true` if a Leo package exists at the given path.
pub fn exists_at(path: &PathBuf) -> bool {
package::Package::exists_at(path)
}
}

View File

@ -1,4 +1,9 @@
use crate::errors::PackageError;
use crate::{
errors::PackageError,
inputs::{InputFile, InputsDirectory, StateFile},
root::{Gitignore, Manifest, README},
source::{LibFile, MainFile, SourceDirectory},
};
use serde::Deserialize;
use std::path::PathBuf;
@ -21,7 +26,63 @@ impl Package {
}
}
pub fn remove_package(_package_name: &str) -> Result<(), PackageError> {
/// Returns `true` if a package exists at the given path
pub fn exists_at(path: &PathBuf) -> bool {
Manifest::exists_at(&path)
}
/// Creates a package at the given path
pub fn create_package(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
// Create the manifest file
Manifest::new(&package_name).write_to(&path)?;
// Create the .gitignore file
Gitignore::new().write_to(&path)?;
// Create the README.md file
README::new(&package_name).write_to(&path)?;
// Create the source directory
SourceDirectory::create(&path)?;
// Create a new library or binary file
if is_lib {
// Verify the library file does not exist
if !LibFile::exists_at(&path) {
// Create the library file in the source directory
LibFile::new(&package_name).write_to(&path)?;
}
} else {
// Create the input directory
InputsDirectory::create(&path)?;
// Verify the input file does not exist
let input_file = InputFile::new(&package_name);
if !input_file.exists_at(&path) {
// Create the input file in the inputs directory
input_file.write_to(&path)?;
}
// Verify the state file does not exist
let state_file = StateFile::new(&package_name);
if !state_file.exists_at(&path) {
// Create the state file in the inputs directory
state_file.write_to(&path)?;
}
// Verify the main file does not exist
if !MainFile::exists_at(&path) {
// Create the main file in the source directory
MainFile::new(&package_name).write_to(&path)?;
}
}
Ok(())
}
/// Removes the package at the given path
pub fn remove_package(_package_name: &str) -> Result<(), PackageError> {
unimplemented!()
}
}