refactor: pass &Path instead of &PathBuf as arguments

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-10-16 16:18:46 +02:00
parent 790f1c472a
commit f59416be1b
36 changed files with 137 additions and 130 deletions

View File

@ -17,9 +17,9 @@
use leo_ast::LeoAst;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::path::{Path, PathBuf};
use std::path::Path;
fn leo_ast<'ast>(filepath: &'ast PathBuf, program_string: &'ast str) {
fn leo_ast<'ast>(filepath: &'ast Path, program_string: &'ast str) {
let result = LeoAst::<'ast>::new(filepath, program_string).unwrap();
black_box(result);
}

View File

@ -45,7 +45,7 @@ pub(crate) mod span;
pub(crate) use span::*;
use from_pest::FromPest;
use std::{fs, path::PathBuf};
use std::{fs, path::Path};
pub struct LeoAst<'ast> {
ast: files::File<'ast>,
@ -53,7 +53,7 @@ pub struct LeoAst<'ast> {
impl<'ast> LeoAst<'ast> {
/// Creates a new abstract syntax tree given the file path.
pub fn new(file_path: &'ast PathBuf, program_string: &'ast str) -> Result<Self, ParserError> {
pub fn new(file_path: &'ast Path, program_string: &'ast str) -> Result<Self, ParserError> {
// TODO (howardwu): Turn this check back on after fixing the testing module.
// assert_eq!(program_string, fs::read_to_string(file_path).map_err(|_| ParserError::FileReadError(file_path.clone()))?);
@ -71,8 +71,8 @@ impl<'ast> LeoAst<'ast> {
// TODO (howardwu): Remove this in favor of a dedicated file loader to verify checksums
// and maintain a global cache of program strings during the compilation process.
/// Loads the Leo code as a string from the given file path.
pub fn load_file(file_path: &'ast PathBuf) -> Result<String, ParserError> {
Ok(fs::read_to_string(file_path).map_err(|_| ParserError::FileReadError(file_path.clone()))?)
pub fn load_file(file_path: &'ast Path) -> Result<String, ParserError> {
Ok(fs::read_to_string(file_path).map_err(|_| ParserError::FileReadError(file_path.to_owned()))?)
}
/// Returns a reference to the inner abstract syntax tree representation.

View File

@ -34,7 +34,7 @@ use snarkos_models::{
curves::{Field, PrimeField},
gadgets::r1cs::{ConstraintSystem, TestConstraintSystem},
};
use std::path::PathBuf;
use std::path::Path;
pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
@ -65,8 +65,8 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
program: Program,
input: InputPairs,
imported_programs: &ImportParser,
main_file_path: &PathBuf,
output_directory: &PathBuf,
main_file_path: &Path,
output_directory: &Path,
) -> Result<(u32, u32), CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.get_name();
@ -147,7 +147,7 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
(false, _) => {
// Set file location of error
let mut error = result.unwrap_err();
error.set_path(main_file_path.clone());
error.set_path(main_file_path.to_owned());
tracing::error!("{} failed due to error\n\n{}\n", full_test_name, error);

View File

@ -21,7 +21,7 @@ use crate::errors::OutputFileError;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/";
@ -38,13 +38,13 @@ impl OutputFile {
}
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the output register variables from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<String, OutputFileError> {
pub fn read_from(&self, path: &Path) -> Result<String, OutputFileError> {
let path = self.setup_file_path(path);
let output = fs::read_to_string(&path).map_err(|_| OutputFileError::FileReadError(path.clone()))?;
@ -52,7 +52,7 @@ impl OutputFile {
}
/// Writes output to a file.
pub fn write(&self, path: &PathBuf, bytes: &[u8]) -> Result<(), OutputFileError> {
pub fn write(&self, path: &Path, bytes: &[u8]) -> Result<(), OutputFileError> {
// create output file
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -62,7 +62,7 @@ impl OutputFile {
/// Removes the output file at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, OutputFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, OutputFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -72,7 +72,7 @@ impl OutputFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -34,14 +34,14 @@ pub mod types;
pub mod values;
use from_pest::FromPest;
use std::{fs, path::PathBuf};
use std::{fs, path::Path};
pub struct LeoInputParser;
impl LeoInputParser {
/// Reads in the given file path into a string.
pub fn load_file(file_path: &PathBuf) -> Result<String, InputParserError> {
Ok(fs::read_to_string(file_path).map_err(|_| InputParserError::FileReadError(file_path.clone()))?)
pub fn load_file(file_path: &Path) -> Result<String, InputParserError> {
Ok(fs::read_to_string(file_path).map_err(|_| InputParserError::FileReadError(file_path.to_owned()))?)
}
/// Parses the input file and constructs a syntax tree.

View File

@ -114,7 +114,7 @@ impl CLI for AddCommand {
let path = current_dir()?;
// Enforce that the current directory is a leo package
Manifest::try_from(&path)?;
Manifest::try_from(path.as_path())?;
let (response, package_name) = match options {
(Some(author), Some(package_name), version) => {

View File

@ -62,7 +62,7 @@ impl CLI for BuildCommand {
let path = current_dir()?;
// Get the package name
let manifest = Manifest::try_from(&path)?;
let manifest = Manifest::try_from(path.as_path())?;
let package_name = manifest.get_package_name();
// Sanitize the package path to the root directory

View File

@ -52,7 +52,7 @@ impl CLI for CleanCommand {
// Get the package name
let path = current_dir()?;
let package_name = Manifest::try_from(&path)?.get_package_name();
let package_name = Manifest::try_from(path.as_path())?.get_package_name();
// Remove the checksum from the output directory
ChecksumFile::new(&package_name).remove(&path)?;

View File

@ -58,7 +58,7 @@ impl CLI for DeployCommand {
match BuildCommand::output(options)? {
Some((_program, _checksum_differs)) => {
// Get the package name
let _package_name = Manifest::try_from(&path)?.get_package_name();
let _package_name = Manifest::try_from(path.as_path())?.get_package_name();
tracing::error!("Unimplemented - `leo deploy`");

View File

@ -58,7 +58,7 @@ impl CLI for LintCommand {
match BuildCommand::output(options)? {
Some((_program, _checksum_differs)) => {
// Get the package name
let _package_name = Manifest::try_from(&path)?.get_package_name();
let _package_name = Manifest::try_from(path.as_path())?.get_package_name();
tracing::error!("Unimplemented - `leo lint`");

View File

@ -54,7 +54,7 @@ impl CLI for ProveCommand {
// Get the package name
let path = current_dir()?;
let package_name = Manifest::try_from(&path)?.get_package_name();
let package_name = Manifest::try_from(path.as_path())?.get_package_name();
tracing::info!("Starting...");

View File

@ -75,7 +75,7 @@ impl CLI for PublishCommand {
// Get the package manifest
let path = current_dir()?;
let package_manifest = Manifest::try_from(&path)?;
let package_manifest = Manifest::try_from(path.as_path())?;
let package_name = package_manifest.get_package_name();
let package_version = package_manifest.get_package_version();

View File

@ -62,7 +62,7 @@ impl CLI for SetupCommand {
fn output(options: Self::Options) -> Result<Self::Output, CLIError> {
// Get the package name
let path = current_dir()?;
let package_name = Manifest::try_from(&path)?.get_package_name();
let package_name = Manifest::try_from(path.as_path())?.get_package_name();
match BuildCommand::output(options)? {
Some((program, checksum_differs)) => {

View File

@ -56,7 +56,7 @@ impl CLI for TestCommand {
let path = current_dir()?;
// Get the package name
let manifest = Manifest::try_from(&path)?;
let manifest = Manifest::try_from(path.as_path())?;
let package_name = manifest.get_package_name();
// Sanitize the package path to the root directory
@ -96,7 +96,7 @@ impl CLI for TestCommand {
Compiler::<Fq, EdwardsGroupType>::parse_program_without_input(package_name, file_path, output_directory)?;
// Parse all inputs as input pairs
let pairs = InputPairs::try_from(&package_path)?;
let pairs = InputPairs::try_from(package_path.as_path())?;
// Run tests
let temporary_program = program;

View File

@ -16,7 +16,7 @@
use crate::errors::ImportsDirectoryError;
use std::{fs, path::PathBuf};
use std::{fs, path::Path};
pub static IMPORTS_DIRECTORY_NAME: &str = "imports/";
@ -24,7 +24,7 @@ pub struct ImportsDirectory;
impl ImportsDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &PathBuf) -> Result<(), ImportsDirectoryError> {
pub fn create(path: &Path) -> Result<(), ImportsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
path.push(IMPORTS_DIRECTORY_NAME);
@ -34,7 +34,7 @@ impl ImportsDirectory {
}
/// Removes the directory at the provided path.
pub fn remove(path: &PathBuf) -> Result<(), ImportsDirectoryError> {
pub fn remove(path: &Path) -> Result<(), ImportsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
path.push(IMPORTS_DIRECTORY_NAME);
@ -48,7 +48,7 @@ impl ImportsDirectory {
}
/// Removes an imported package in the imports directory at the provided path.
pub fn remove_import(path: &PathBuf, package_name: &str) -> Result<(), ImportsDirectoryError> {
pub fn remove_import(path: &Path, package_name: &str) -> Result<(), ImportsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
path.push(IMPORTS_DIRECTORY_NAME);

View File

@ -16,7 +16,11 @@
use crate::errors::InputsDirectoryError;
use std::{fs, fs::ReadDir, path::PathBuf};
use std::{
fs,
fs::ReadDir,
path::{Path, PathBuf},
};
pub static INPUTS_DIRECTORY_NAME: &str = "inputs/";
@ -24,7 +28,7 @@ pub struct InputsDirectory;
impl InputsDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &PathBuf) -> Result<(), InputsDirectoryError> {
pub fn create(path: &Path) -> Result<(), InputsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(INPUTS_DIRECTORY_NAME);
@ -34,7 +38,7 @@ impl InputsDirectory {
}
/// Returns a list of files in the input directory.
pub fn files(path: &PathBuf) -> Result<Vec<PathBuf>, InputsDirectoryError> {
pub fn files(path: &Path) -> Result<Vec<PathBuf>, InputsDirectoryError> {
let mut path = path.to_owned();
path.push(INPUTS_DIRECTORY_NAME);
let directory = fs::read_dir(&path).map_err(InputsDirectoryError::Reading)?;

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static INPUT_FILE_EXTENSION: &str = ".in";
@ -43,13 +43,13 @@ impl InputFile {
format!("{}{}{}", INPUTS_DIRECTORY_NAME, self.package_name, INPUT_FILE_EXTENSION)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the program input variables from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<(String, PathBuf), InputFileError> {
pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), InputFileError> {
let path = self.setup_file_path(path);
let input = fs::read_to_string(&path).map_err(|_| InputFileError::FileReadError(path.clone()))?;
@ -57,7 +57,7 @@ impl InputFile {
}
/// Writes the standard input format to a file.
pub fn write_to(self, path: &PathBuf) -> Result<(), InputFileError> {
pub fn write_to(self, path: &Path) -> Result<(), InputFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -78,7 +78,7 @@ r0: u32 = 0;
)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {

View File

@ -19,7 +19,7 @@ use crate::{
InputsDirectoryError,
};
use std::{collections::HashMap, convert::TryFrom, path::PathBuf};
use std::{collections::HashMap, convert::TryFrom, path::Path};
#[derive(Default)]
pub struct InputPairs {
@ -38,10 +38,10 @@ impl InputPairs {
}
}
impl TryFrom<&PathBuf> for InputPairs {
impl TryFrom<&Path> for InputPairs {
type Error = InputsDirectoryError;
fn try_from(directory: &PathBuf) -> Result<Self, Self::Error> {
fn try_from(directory: &Path) -> Result<Self, Self::Error> {
let files = InputsDirectory::files(directory)?;
let mut pairs = HashMap::<String, InputPair>::new();

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static STATE_FILE_EXTENSION: &str = ".state";
@ -43,13 +43,13 @@ impl StateFile {
format!("{}{}{}", INPUTS_DIRECTORY_NAME, self.package_name, STATE_FILE_EXTENSION)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the state input variables from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<(String, PathBuf), StateFileError> {
pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), StateFileError> {
let path = self.setup_file_path(path);
let input = fs::read_to_string(&path).map_err(|_| StateFileError::FileReadError(path.clone()))?;
@ -57,7 +57,7 @@ impl StateFile {
}
/// Writes the standard input format to a file.
pub fn write_to(self, path: &PathBuf) -> Result<(), StateFileError> {
pub fn write_to(self, path: &Path) -> Result<(), StateFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -97,7 +97,7 @@ leaf_randomness: [u8; 32] = [0; 32];
)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {

View File

@ -27,18 +27,18 @@ pub mod package;
pub mod root;
pub mod source;
use std::path::PathBuf;
use std::path::Path;
pub struct LeoPackage;
impl LeoPackage {
/// Initializes a Leo package at the given path.
pub fn initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
pub fn initialize(package_name: &str, is_lib: bool, path: &Path) -> Result<(), PackageError> {
package::Package::initialize(package_name, is_lib, path)
}
/// Removes an imported Leo package
pub fn remove_imported_package(package_name: &str, path: &PathBuf) -> Result<(), PackageError> {
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<(), PackageError> {
package::Package::remove_imported_package(package_name, path)
}
}

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static CHECKSUM_FILE_EXTENSION: &str = ".sum";
@ -39,20 +39,20 @@ impl ChecksumFile {
}
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the checksum from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<String, ChecksumFileError> {
pub fn read_from(&self, path: &Path) -> Result<String, ChecksumFileError> {
let path = self.setup_file_path(path);
Ok(fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.clone()))?)
}
/// Writes the given checksum to a file.
pub fn write_to(&self, path: &PathBuf, checksum: String) -> Result<(), ChecksumFileError> {
pub fn write_to(&self, path: &Path, checksum: String) -> Result<(), ChecksumFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -63,7 +63,7 @@ impl ChecksumFile {
/// Removes the checksum at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, ChecksumFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, ChecksumFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -73,7 +73,7 @@ impl ChecksumFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static CIRCUIT_FILE_EXTENSION: &str = ".json";
@ -39,20 +39,20 @@ impl CircuitFile {
}
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the serialized circuit from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<String, CircuitFileError> {
pub fn read_from(&self, path: &Path) -> Result<String, CircuitFileError> {
let path = self.setup_file_path(path);
Ok(fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.clone()))?)
}
/// Writes the given serialized circuit to a file.
pub fn write_to(&self, path: &PathBuf, circuit: String) -> Result<(), CircuitFileError> {
pub fn write_to(&self, path: &Path, circuit: String) -> Result<(), CircuitFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -63,7 +63,7 @@ impl CircuitFile {
/// Removes the serialized circuit at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, CircuitFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, CircuitFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -73,7 +73,7 @@ impl CircuitFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -16,7 +16,7 @@
use crate::errors::OutputsDirectoryError;
use std::{fs, path::PathBuf};
use std::{fs, path::Path};
pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/";
@ -24,7 +24,7 @@ pub struct OutputsDirectory;
impl OutputsDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &PathBuf) -> Result<(), OutputsDirectoryError> {
pub fn create(path: &Path) -> Result<(), OutputsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
path.push(OUTPUTS_DIRECTORY_NAME);
@ -34,7 +34,7 @@ impl OutputsDirectory {
}
/// Removes the directory at the provided path.
pub fn remove(path: &PathBuf) -> Result<(), OutputsDirectoryError> {
pub fn remove(path: &Path) -> Result<(), OutputsDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
path.push(OUTPUTS_DIRECTORY_NAME);

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static PROOF_FILE_EXTENSION: &str = ".proof";
@ -39,13 +39,13 @@ impl ProofFile {
}
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the proof from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<String, ProofFileError> {
pub fn read_from(&self, path: &Path) -> Result<String, ProofFileError> {
let path = self.setup_file_path(path);
let proof = fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.clone()))?;
@ -53,7 +53,7 @@ impl ProofFile {
}
/// Writes the given proof to a file.
pub fn write_to(&self, path: &PathBuf, proof: &[u8]) -> Result<(), ProofFileError> {
pub fn write_to(&self, path: &Path, proof: &[u8]) -> Result<(), ProofFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -66,7 +66,7 @@ impl ProofFile {
/// Removes the proof at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, ProofFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, ProofFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -76,7 +76,7 @@ impl ProofFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static PROVING_KEY_FILE_EXTENSION: &str = ".lpk";
@ -39,24 +39,24 @@ impl ProvingKeyFile {
}
}
pub fn full_path(&self, path: &PathBuf) -> PathBuf {
pub fn full_path(&self, path: &Path) -> PathBuf {
self.setup_file_path(path)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the proving key from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<Vec<u8>, ProvingKeyFileError> {
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, ProvingKeyFileError> {
let path = self.setup_file_path(path);
Ok(fs::read(&path).map_err(|_| ProvingKeyFileError::FileReadError(path.clone()))?)
}
/// Writes the given proving key to a file.
pub fn write_to(&self, path: &PathBuf, proving_key: &[u8]) -> Result<PathBuf, ProvingKeyFileError> {
pub fn write_to(&self, path: &Path, proving_key: &[u8]) -> Result<PathBuf, ProvingKeyFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -67,7 +67,7 @@ impl ProvingKeyFile {
/// Removes the proving key at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, ProvingKeyFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, ProvingKeyFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -77,7 +77,7 @@ impl ProvingKeyFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -22,7 +22,7 @@ use serde::Deserialize;
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
pub static VERIFICATION_KEY_FILE_EXTENSION: &str = ".lvk";
@ -39,24 +39,24 @@ impl VerificationKeyFile {
}
}
pub fn full_path(&self, path: &PathBuf) -> PathBuf {
pub fn full_path(&self, path: &Path) -> PathBuf {
self.setup_file_path(path)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the verification key from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<Vec<u8>, VerificationKeyFileError> {
pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, VerificationKeyFileError> {
let path = self.setup_file_path(path);
Ok(fs::read(&path).map_err(|_| VerificationKeyFileError::FileReadError(path.clone()))?)
}
/// Writes the given verification key to a file.
pub fn write_to(&self, path: &PathBuf, verification_key: &[u8]) -> Result<PathBuf, VerificationKeyFileError> {
pub fn write_to(&self, path: &Path, verification_key: &[u8]) -> Result<PathBuf, VerificationKeyFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
@ -67,7 +67,7 @@ impl VerificationKeyFile {
/// Removes the verification key at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, VerificationKeyFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, VerificationKeyFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -77,7 +77,7 @@ impl VerificationKeyFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -23,7 +23,7 @@ use crate::{
};
use serde::Deserialize;
use std::path::PathBuf;
use std::path::Path;
#[derive(Deserialize)]
pub struct Package {
@ -44,39 +44,39 @@ impl Package {
}
/// Returns `true` if a package is can be initialized at a given path.
pub fn can_initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> bool {
pub fn can_initialize(package_name: &str, is_lib: bool, path: &Path) -> bool {
let mut result = true;
let mut existing_files = vec![];
// Check if the manifest file already exists.
if Manifest::exists_at(&path) {
if Manifest::exists_at(path) {
existing_files.push(Manifest::filename());
result = false;
}
if is_lib {
// Check if the library file already exists.
if LibraryFile::exists_at(&path) {
if LibraryFile::exists_at(path) {
existing_files.push(LibraryFile::filename());
result = false;
}
} else {
// Check if the input file already exists.
let input_file = InputFile::new(&package_name);
if input_file.exists_at(&path) {
if input_file.exists_at(path) {
existing_files.push(input_file.filename());
result = false;
}
// Check if the state file already exists.
let state_file = StateFile::new(&package_name);
if state_file.exists_at(&path) {
if state_file.exists_at(path) {
existing_files.push(state_file.filename());
result = false;
}
// Check if the main file already exists.
if MainFile::exists_at(&path) {
if MainFile::exists_at(path) {
existing_files.push(MainFile::filename());
result = false;
}
@ -90,7 +90,7 @@ impl Package {
}
/// Returns `true` if a package is initialized at the given path
pub fn is_initialized(package_name: &str, is_lib: bool, path: &PathBuf) -> bool {
pub fn is_initialized(package_name: &str, is_lib: bool, path: &Path) -> bool {
// Check if the manifest file exists.
if !Manifest::exists_at(&path) {
return false;
@ -124,7 +124,7 @@ impl Package {
}
/// Creates a package at the given path
pub fn initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
pub fn initialize(package_name: &str, is_lib: bool, path: &Path) -> Result<(), PackageError> {
// First, verify that this directory is not already initialized as a Leo package.
{
if !Self::can_initialize(package_name, is_lib, path) {
@ -186,7 +186,7 @@ impl Package {
}
/// Removes the package at the given path
pub fn remove_imported_package(package_name: &str, path: &PathBuf) -> Result<(), PackageError> {
pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<(), PackageError> {
Ok(ImportsDirectory::remove_import(path, package_name)?)
}
}

View File

@ -19,7 +19,7 @@
use crate::errors::GitignoreError;
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::Path};
pub static GITIGNORE_FILENAME: &str = ".gitignore";
@ -31,7 +31,7 @@ impl Gitignore {
Self::default()
}
pub fn exists_at(path: &PathBuf) -> bool {
pub fn exists_at(path: &Path) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
path.push(GITIGNORE_FILENAME);
@ -39,7 +39,7 @@ impl Gitignore {
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), GitignoreError> {
pub fn write_to(self, path: &Path) -> Result<(), GitignoreError> {
let mut path = path.to_owned();
if path.is_dir() {
path.push(GITIGNORE_FILENAME);

View File

@ -21,7 +21,7 @@ use std::{
convert::TryFrom,
fs::File,
io::{Read, Write},
path::PathBuf,
path::Path,
};
pub const MANIFEST_FILENAME: &str = "Leo.toml";
@ -49,7 +49,7 @@ impl Manifest {
MANIFEST_FILENAME.to_string()
}
pub fn exists_at(path: &PathBuf) -> bool {
pub fn exists_at(path: &Path) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
path.push(MANIFEST_FILENAME);
@ -77,7 +77,7 @@ impl Manifest {
self.remote.clone()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), ManifestError> {
pub fn write_to(self, path: &Path) -> Result<(), ManifestError> {
let mut path = path.to_owned();
if path.is_dir() {
path.push(MANIFEST_FILENAME);
@ -104,10 +104,10 @@ author = "[AUTHOR]" # Add your Aleo Package Manager username, team's name, or or
}
}
impl TryFrom<&PathBuf> for Manifest {
impl TryFrom<&Path> for Manifest {
type Error = ManifestError;
fn try_from(path: &PathBuf) -> Result<Self, Self::Error> {
fn try_from(path: &Path) -> Result<Self, Self::Error> {
let mut path = path.to_owned();
if path.is_dir() {
path.push(MANIFEST_FILENAME);

View File

@ -19,7 +19,7 @@
use crate::errors::READMEError;
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::Path};
pub static README_FILENAME: &str = "README.md";
@ -39,7 +39,7 @@ impl README {
self.package_name.clone()
}
pub fn exists_at(path: &PathBuf) -> bool {
pub fn exists_at(path: &Path) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
path.push(README_FILENAME);
@ -47,7 +47,7 @@ impl README {
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), READMEError> {
pub fn write_to(self, path: &Path) -> Result<(), READMEError> {
let mut path = path.to_owned();
if path.is_dir() {
path.push(README_FILENAME);

View File

@ -55,24 +55,24 @@ impl ZipFile {
}
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
pub fn get_file_path(&self, current_dir: &PathBuf) -> PathBuf {
pub fn get_file_path(&self, current_dir: &Path) -> PathBuf {
self.setup_file_path(current_dir)
}
// /// Reads the program bytes from the given file path if it exists.
// pub fn read_from(&self, path: &PathBuf) -> Result<Vec<u8>, ZipFileError> {
// pub fn read_from(&self, path: &Path) -> Result<Vec<u8>, ZipFileError> {
// let path = self.setup_file_path(path);
//
// Ok(fs::read(&path).map_err(|_| ZipFileError::FileReadError(path.clone()))?)
// }
/// Writes the current package contents to a zip file.
pub fn write(&self, src_dir: &PathBuf) -> Result<(), ZipFileError> {
pub fn write(&self, src_dir: &Path) -> Result<(), ZipFileError> {
// Build walkdir iterator from current package
let walkdir = WalkDir::new(src_dir.clone());
@ -89,7 +89,7 @@ impl ZipFile {
let mut buffer = Vec::new();
for entry in walkdir.into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
let name = path.strip_prefix(src_dir.as_path()).unwrap();
let name = path.strip_prefix(src_dir).unwrap();
// Add file/directory exclusion
@ -125,7 +125,7 @@ impl ZipFile {
/// Removes the zip file at the given path if it exists. Returns `true` on success,
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
pub fn remove(&self, path: &PathBuf) -> Result<bool, ZipFileError> {
pub fn remove(&self, path: &Path) -> Result<bool, ZipFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
@ -135,7 +135,7 @@ impl ZipFile {
Ok(true)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
fn setup_file_path(&self, path: &Path) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {

View File

@ -16,7 +16,10 @@
use crate::errors::SourceDirectoryError;
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};
pub static SOURCE_DIRECTORY_NAME: &str = "src/";
@ -26,7 +29,7 @@ pub struct SourceDirectory;
impl SourceDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &PathBuf) -> Result<(), SourceDirectoryError> {
pub fn create(path: &Path) -> Result<(), SourceDirectoryError> {
let mut path = path.to_owned();
if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) {
path.push(SOURCE_DIRECTORY_NAME);
@ -36,7 +39,7 @@ impl SourceDirectory {
}
/// Returns a list of files in the source directory.
pub fn files(path: &PathBuf) -> Result<Vec<PathBuf>, SourceDirectoryError> {
pub fn files(path: &Path) -> Result<Vec<PathBuf>, SourceDirectoryError> {
let mut path = path.to_owned();
path.push(SOURCE_DIRECTORY_NAME);
let directory = fs::read_dir(&path).map_err(SourceDirectoryError::Reading)?;

View File

@ -19,7 +19,7 @@
use crate::{errors::LibraryFileError, source::directory::SOURCE_DIRECTORY_NAME};
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::Path};
pub static LIBRARY_FILENAME: &str = "lib.leo";
@ -39,7 +39,7 @@ impl LibraryFile {
format!("{}{}", SOURCE_DIRECTORY_NAME, LIBRARY_FILENAME)
}
pub fn exists_at(path: &PathBuf) -> bool {
pub fn exists_at(path: &Path) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
@ -50,7 +50,7 @@ impl LibraryFile {
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), LibraryFileError> {
pub fn write_to(self, path: &Path) -> Result<(), LibraryFileError> {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(SOURCE_DIRECTORY_NAME) {

View File

@ -19,7 +19,7 @@
use crate::{errors::MainFileError, source::directory::SOURCE_DIRECTORY_NAME};
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::Path};
pub static MAIN_FILENAME: &str = "main.leo";
@ -39,7 +39,7 @@ impl MainFile {
format!("{}{}", SOURCE_DIRECTORY_NAME, MAIN_FILENAME)
}
pub fn exists_at(path: &PathBuf) -> bool {
pub fn exists_at(path: &Path) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
@ -50,7 +50,7 @@ impl MainFile {
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), MainFileError> {
pub fn write_to(self, path: &Path) -> Result<(), MainFileError> {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(SOURCE_DIRECTORY_NAME) {

View File

@ -23,7 +23,7 @@ use std::{
convert::TryFrom,
fs::File,
io::{Read, Write},
path::PathBuf,
path::Path,
};
const OLD_MANIFEST_FORMAT: &str = r#"[package]
@ -56,7 +56,7 @@ fn create_outdated_manifest_file(path: PathBuf) -> PathBuf {
}
/// Read the manifest file into a string.
fn read_manifest_file(path: &PathBuf) -> String {
fn read_manifest_file(path: &Path) -> String {
let mut file = File::open(path.clone()).unwrap();
let size = file.metadata().unwrap().len() as usize;
@ -67,7 +67,7 @@ fn read_manifest_file(path: &PathBuf) -> String {
}
/// Read the manifest file and check that the remote format is updated.
fn remote_is_updated(path: &PathBuf) -> bool {
fn remote_is_updated(path: &Path) -> bool {
let manifest_string = read_manifest_file(&path);
for line in manifest_string.lines() {
if line.starts_with("remote") {
@ -79,7 +79,7 @@ fn remote_is_updated(path: &PathBuf) -> bool {
}
/// Read the manifest file and check that the project format is updated.
fn project_is_updated(path: &PathBuf) -> bool {
fn project_is_updated(path: &Path) -> bool {
let manifest_string = read_manifest_file(&path);
!manifest_string.contains(OLD_PROJECT_FORMAT) && manifest_string.contains(NEW_PROJECT_FORMAT)

View File

@ -17,9 +17,9 @@
use leo_ast::LeoAst;
use leo_typed::LeoTypedAst;
use std::path::PathBuf;
use std::path::Path;
fn to_typed_ast(program_filepath: &PathBuf) -> LeoTypedAst {
fn to_typed_ast(program_filepath: &Path) -> LeoTypedAst {
// Loads the Leo code as a string from the given file path.
let program_string = LeoAst::load_file(program_filepath).unwrap();