mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
perf: use Cow to avoid some PathBuf allocations
Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
parent
f59416be1b
commit
d662b9a236
@ -19,9 +19,10 @@
|
||||
use crate::errors::OutputFileError;
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/";
|
||||
@ -47,7 +48,7 @@ impl OutputFile {
|
||||
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()))?;
|
||||
let output = fs::read_to_string(&path).map_err(|_| OutputFileError::FileReadError(path.into_owned()))?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
@ -68,17 +69,18 @@ impl OutputFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| OutputFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| OutputFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, OUTPUT_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, OUTPUT_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -124,9 +124,9 @@ impl CLI for BuildCommand {
|
||||
main_file_path,
|
||||
output_directory,
|
||||
&input_string,
|
||||
input_path,
|
||||
input_path.into_owned(),
|
||||
&state_string,
|
||||
state_path,
|
||||
state_path.into_owned(),
|
||||
)?;
|
||||
|
||||
// Compute the current program checksum
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::errors::ImportsDirectoryError;
|
||||
|
||||
use std::{fs, path::Path};
|
||||
use std::{borrow::Cow, fs, path::Path};
|
||||
|
||||
pub static IMPORTS_DIRECTORY_NAME: &str = "imports/";
|
||||
|
||||
@ -25,9 +25,9 @@ pub struct ImportsDirectory;
|
||||
impl ImportsDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &Path) -> Result<(), ImportsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
||||
path.push(IMPORTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(ImportsDirectoryError::Creating)
|
||||
@ -35,9 +35,9 @@ impl ImportsDirectory {
|
||||
|
||||
/// Removes the directory at the provided path.
|
||||
pub fn remove(path: &Path) -> Result<(), ImportsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
||||
path.push(IMPORTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
if path.exists() {
|
||||
@ -49,12 +49,12 @@ impl ImportsDirectory {
|
||||
|
||||
/// Removes an imported package in the imports directory at the provided path.
|
||||
pub fn remove_import(path: &Path, package_name: &str) -> Result<(), ImportsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) {
|
||||
path.push(IMPORTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(IMPORTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
path.push(package_name);
|
||||
path.to_mut().push(package_name);
|
||||
|
||||
if !path.exists() || !path.is_dir() {
|
||||
return Err(ImportsDirectoryError::ImportDoesNotExist(package_name.into()));
|
||||
|
@ -17,6 +17,7 @@
|
||||
use crate::errors::InputsDirectoryError;
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs,
|
||||
fs::ReadDir,
|
||||
path::{Path, PathBuf},
|
||||
@ -29,9 +30,9 @@ pub struct InputsDirectory;
|
||||
impl InputsDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &Path) -> Result<(), InputsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
||||
path.push(INPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(INPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(InputsDirectoryError::Creating)
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::InputFileError, inputs::INPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static INPUT_FILE_EXTENSION: &str = ".in";
|
||||
@ -49,11 +50,13 @@ impl InputFile {
|
||||
}
|
||||
|
||||
/// Reads the program input variables from the given file path if it exists.
|
||||
pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), InputFileError> {
|
||||
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), InputFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
let input = fs::read_to_string(&path).map_err(|_| InputFileError::FileReadError(path.clone()))?;
|
||||
Ok((input, path))
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(input) => Ok((input, path)),
|
||||
Err(_) => Err(InputFileError::FileReadError(path.into_owned())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the standard input format to a file.
|
||||
@ -78,13 +81,14 @@ r0: u32 = 0;
|
||||
)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
||||
path.push(INPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(INPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, INPUT_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, INPUT_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::StateFileError, inputs::INPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static STATE_FILE_EXTENSION: &str = ".state";
|
||||
@ -49,11 +50,13 @@ impl StateFile {
|
||||
}
|
||||
|
||||
/// Reads the state input variables from the given file path if it exists.
|
||||
pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), StateFileError> {
|
||||
pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), StateFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
let input = fs::read_to_string(&path).map_err(|_| StateFileError::FileReadError(path.clone()))?;
|
||||
Ok((input, path))
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(input) => Ok((input, path)),
|
||||
Err(_) => Err(StateFileError::FileReadError(path.into_owned())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the standard input format to a file.
|
||||
@ -97,13 +100,14 @@ leaf_randomness: [u8; 32] = [0; 32];
|
||||
)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
|
||||
path.push(INPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(INPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, STATE_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, STATE_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::ChecksumFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static CHECKSUM_FILE_EXTENSION: &str = ".sum";
|
||||
@ -48,7 +49,7 @@ impl ChecksumFile {
|
||||
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()))?)
|
||||
Ok(fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.into_owned()))?)
|
||||
}
|
||||
|
||||
/// Writes the given checksum to a file.
|
||||
@ -69,17 +70,18 @@ impl ChecksumFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::CircuitFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static CIRCUIT_FILE_EXTENSION: &str = ".json";
|
||||
@ -48,7 +49,7 @@ impl CircuitFile {
|
||||
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()))?)
|
||||
Ok(fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.into_owned()))?)
|
||||
}
|
||||
|
||||
/// Writes the given serialized circuit to a file.
|
||||
@ -69,17 +70,18 @@ impl CircuitFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, CIRCUIT_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, CIRCUIT_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::errors::OutputsDirectoryError;
|
||||
|
||||
use std::{fs, path::Path};
|
||||
use std::{borrow::Cow, fs, path::Path};
|
||||
|
||||
pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/";
|
||||
|
||||
@ -25,9 +25,9 @@ pub struct OutputsDirectory;
|
||||
impl OutputsDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &Path) -> Result<(), OutputsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(OutputsDirectoryError::Creating)
|
||||
@ -35,9 +35,9 @@ impl OutputsDirectory {
|
||||
|
||||
/// Removes the directory at the provided path.
|
||||
pub fn remove(path: &Path) -> Result<(), OutputsDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
if path.exists() {
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::ProofFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static PROOF_FILE_EXTENSION: &str = ".proof";
|
||||
@ -48,7 +49,7 @@ impl ProofFile {
|
||||
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()))?;
|
||||
let proof = fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.into_owned()))?;
|
||||
Ok(proof)
|
||||
}
|
||||
|
||||
@ -72,17 +73,18 @@ impl ProofFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| ProofFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| ProofFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, PROOF_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, PROOF_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::ProvingKeyFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static PROVING_KEY_FILE_EXTENSION: &str = ".lpk";
|
||||
@ -39,7 +40,7 @@ impl ProvingKeyFile {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn full_path(&self, path: &Path) -> PathBuf {
|
||||
pub fn full_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
self.setup_file_path(path)
|
||||
}
|
||||
|
||||
@ -52,11 +53,11 @@ impl ProvingKeyFile {
|
||||
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()))?)
|
||||
Ok(fs::read(&path).map_err(|_| ProvingKeyFileError::FileReadError(path.into_owned()))?)
|
||||
}
|
||||
|
||||
/// Writes the given proving key to a file.
|
||||
pub fn write_to(&self, path: &Path, proving_key: &[u8]) -> Result<PathBuf, ProvingKeyFileError> {
|
||||
pub fn write_to<'a>(&self, path: &'a Path, proving_key: &[u8]) -> Result<Cow<'a, Path>, ProvingKeyFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
@ -73,17 +74,18 @@ impl ProvingKeyFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| ProvingKeyFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| ProvingKeyFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, PROVING_KEY_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, PROVING_KEY_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ use crate::{errors::VerificationKeyFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub static VERIFICATION_KEY_FILE_EXTENSION: &str = ".lvk";
|
||||
@ -39,7 +40,7 @@ impl VerificationKeyFile {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn full_path(&self, path: &Path) -> PathBuf {
|
||||
pub fn full_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
self.setup_file_path(path)
|
||||
}
|
||||
|
||||
@ -52,11 +53,15 @@ impl VerificationKeyFile {
|
||||
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()))?)
|
||||
Ok(fs::read(&path).map_err(|_| VerificationKeyFileError::FileReadError(path.into_owned()))?)
|
||||
}
|
||||
|
||||
/// Writes the given verification key to a file.
|
||||
pub fn write_to(&self, path: &Path, verification_key: &[u8]) -> Result<PathBuf, VerificationKeyFileError> {
|
||||
pub fn write_to<'a>(
|
||||
&self,
|
||||
path: &'a Path,
|
||||
verification_key: &[u8],
|
||||
) -> Result<Cow<'a, Path>, VerificationKeyFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
@ -73,17 +78,18 @@ impl VerificationKeyFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| VerificationKeyFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| VerificationKeyFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, VERIFICATION_KEY_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, VERIFICATION_KEY_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -19,7 +19,7 @@
|
||||
use crate::errors::GitignoreError;
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, io::Write, path::Path};
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::Path};
|
||||
|
||||
pub static GITIGNORE_FILENAME: &str = ".gitignore";
|
||||
|
||||
@ -32,17 +32,17 @@ impl Gitignore {
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(GITIGNORE_FILENAME);
|
||||
path.to_mut().push(GITIGNORE_FILENAME);
|
||||
}
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn write_to(self, path: &Path) -> Result<(), GitignoreError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(GITIGNORE_FILENAME);
|
||||
path.to_mut().push(GITIGNORE_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
|
@ -18,6 +18,7 @@ use crate::{errors::ManifestError, package::Package};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
convert::TryFrom,
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
@ -50,9 +51,9 @@ impl Manifest {
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(MANIFEST_FILENAME);
|
||||
path.to_mut().push(MANIFEST_FILENAME);
|
||||
}
|
||||
path.exists()
|
||||
}
|
||||
@ -78,9 +79,9 @@ impl Manifest {
|
||||
}
|
||||
|
||||
pub fn write_to(self, path: &Path) -> Result<(), ManifestError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(MANIFEST_FILENAME);
|
||||
path.to_mut().push(MANIFEST_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::create(&path).map_err(|error| ManifestError::Creating(MANIFEST_FILENAME, error))?;
|
||||
@ -108,9 +109,9 @@ impl TryFrom<&Path> for Manifest {
|
||||
type Error = ManifestError;
|
||||
|
||||
fn try_from(path: &Path) -> Result<Self, Self::Error> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(MANIFEST_FILENAME);
|
||||
path.to_mut().push(MANIFEST_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::open(path.clone()).map_err(|error| ManifestError::Opening(MANIFEST_FILENAME, error))?;
|
||||
|
@ -19,7 +19,7 @@
|
||||
use crate::errors::READMEError;
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, io::Write, path::Path};
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::Path};
|
||||
|
||||
pub static README_FILENAME: &str = "README.md";
|
||||
|
||||
@ -40,17 +40,17 @@ impl README {
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(README_FILENAME);
|
||||
path.to_mut().push(README_FILENAME);
|
||||
}
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn write_to(self, path: &Path) -> Result<(), READMEError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
path.push(README_FILENAME);
|
||||
path.to_mut().push(README_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
|
@ -34,9 +34,10 @@ use crate::{
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
path::Path,
|
||||
};
|
||||
use walkdir::WalkDir;
|
||||
use zip::write::{FileOptions, ZipWriter};
|
||||
@ -60,7 +61,7 @@ impl ZipFile {
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn get_file_path(&self, current_dir: &Path) -> PathBuf {
|
||||
pub fn get_file_path<'a>(&self, current_dir: &'a Path) -> Cow<'a, Path> {
|
||||
self.setup_file_path(current_dir)
|
||||
}
|
||||
|
||||
@ -74,7 +75,7 @@ impl ZipFile {
|
||||
/// Writes the current package contents to a zip file.
|
||||
pub fn write(&self, src_dir: &Path) -> Result<(), ZipFileError> {
|
||||
// Build walkdir iterator from current package
|
||||
let walkdir = WalkDir::new(src_dir.clone());
|
||||
let walkdir = WalkDir::new(src_dir);
|
||||
|
||||
// Create zip file
|
||||
let path = self.setup_file_path(src_dir);
|
||||
@ -131,17 +132,18 @@ impl ZipFile {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| ZipFileError::FileRemovalError(path.clone()))?;
|
||||
fs::remove_file(&path).map_err(|_| ZipFileError::FileRemovalError(path.into_owned()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &Path) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(OUTPUTS_DIRECTORY_NAME);
|
||||
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(format!("{}{}", self.package_name, ZIP_FILE_EXTENSION));
|
||||
path.to_mut()
|
||||
.push(format!("{}{}", self.package_name, ZIP_FILE_EXTENSION));
|
||||
}
|
||||
path
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
use crate::errors::SourceDirectoryError;
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
@ -30,9 +31,9 @@ pub struct SourceDirectory;
|
||||
impl SourceDirectory {
|
||||
/// Creates a directory at the provided path with the default directory name.
|
||||
pub fn create(path: &Path) -> Result<(), SourceDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
fs::create_dir_all(&path).map_err(SourceDirectoryError::Creating)
|
||||
@ -40,8 +41,8 @@ impl SourceDirectory {
|
||||
|
||||
/// Returns a list of files in the source directory.
|
||||
pub fn files(path: &Path) -> Result<Vec<PathBuf>, SourceDirectoryError> {
|
||||
let mut path = path.to_owned();
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
let mut path = Cow::from(path);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
let directory = fs::read_dir(&path).map_err(SourceDirectoryError::Reading)?;
|
||||
|
||||
let mut file_paths = Vec::new();
|
||||
|
@ -19,7 +19,7 @@
|
||||
use crate::{errors::LibraryFileError, source::directory::SOURCE_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, io::Write, path::Path};
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::Path};
|
||||
|
||||
pub static LIBRARY_FILENAME: &str = "lib.leo";
|
||||
|
||||
@ -40,23 +40,23 @@ impl LibraryFile {
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(LIBRARY_FILENAME);
|
||||
path.to_mut().push(LIBRARY_FILENAME);
|
||||
}
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn write_to(self, path: &Path) -> Result<(), LibraryFileError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(LIBRARY_FILENAME);
|
||||
path.to_mut().push(LIBRARY_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
|
@ -19,7 +19,7 @@
|
||||
use crate::{errors::MainFileError, source::directory::SOURCE_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, io::Write, path::Path};
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::Path};
|
||||
|
||||
pub static MAIN_FILENAME: &str = "main.leo";
|
||||
|
||||
@ -40,23 +40,23 @@ impl MainFile {
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(MAIN_FILENAME);
|
||||
path.to_mut().push(MAIN_FILENAME);
|
||||
}
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn write_to(self, path: &Path) -> Result<(), MainFileError> {
|
||||
let mut path = path.to_owned();
|
||||
let mut path = Cow::from(path);
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(SOURCE_DIRECTORY_NAME) {
|
||||
path.push(SOURCE_DIRECTORY_NAME);
|
||||
path.to_mut().push(SOURCE_DIRECTORY_NAME);
|
||||
}
|
||||
path.push(MAIN_FILENAME);
|
||||
path.to_mut().push(MAIN_FILENAME);
|
||||
}
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
|
Loading…
Reference in New Issue
Block a user