refactor: accept &Path instead of PathBuf where sufficient

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-10-16 17:05:41 +02:00
parent d662b9a236
commit 64774cdab6
26 changed files with 80 additions and 64 deletions

View File

@ -34,8 +34,16 @@ impl<'ast> fmt::Display for RangeOrExpression<'ast> {
RangeOrExpression::Range(ref range) => write!(
f,
"{}..{}",
range.from.as_ref().map(|v| v.to_string()).unwrap_or("".to_string()),
range.to.as_ref().map(|v| v.to_string()).unwrap_or("".to_string()),
range
.from
.as_ref()
.map(|v| v.to_string())
.unwrap_or_else(|| "".to_string()),
range
.to
.as_ref()
.map(|v| v.to_string())
.unwrap_or_else(|| "".to_string()),
),
}
}

View File

@ -17,7 +17,7 @@
use crate::{ast::Rule, errors::SyntaxError};
use pest::error::Error;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
#[derive(Debug, Error)]
pub enum ParserError {
@ -38,7 +38,7 @@ pub enum ParserError {
}
impl ParserError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
if let ParserError::SyntaxError(error) = self {
let new_error: Error<Rule> = match error {
SyntaxError::Error(error) => {

View File

@ -38,7 +38,11 @@ use snarkos_models::{
};
use sha2::{Digest, Sha256};
use std::{fs, marker::PhantomData, path::PathBuf};
use std::{
fs,
marker::PhantomData,
path::{Path, PathBuf},
};
#[derive(Clone)]
pub struct Compiler<F: Field + PrimeField, G: GroupType<F>> {
@ -71,17 +75,17 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
pub fn parse_input(
&mut self,
input_string: &str,
input_path: PathBuf,
input_path: &Path,
state_string: &str,
state_path: PathBuf,
state_path: &Path,
) -> Result<(), CompilerError> {
let input_syntax_tree = LeoInputParser::parse_file(&input_string).map_err(|mut e| {
e.set_path(input_path.clone());
e.set_path(input_path);
e
})?;
let state_syntax_tree = LeoInputParser::parse_file(&state_string).map_err(|mut e| {
e.set_path(state_path.clone());
e.set_path(state_path);
e
})?;
@ -121,9 +125,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
main_file_path: PathBuf,
output_directory: PathBuf,
input_string: &str,
input_path: PathBuf,
input_path: &Path,
state_string: &str,
state_path: PathBuf,
state_path: &Path,
) -> Result<Self, CompilerError> {
let mut compiler = Self::new(package_name, main_file_path, output_directory);
@ -149,7 +153,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), CompilerError> {
// Use the given bytes to construct the abstract syntax tree.
let ast = LeoAst::new(&self.main_file_path, &program_string).map_err(|mut e| {
e.set_path(self.main_file_path.clone());
e.set_path(&self.main_file_path);
e
})?;
@ -202,7 +206,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
generate_constraints::<F, G, CS>(cs, self.program, self.program_input, &self.imported_programs).map_err(
|mut error| {
error.set_path(path);
error.set_path(&path);
error
},
@ -228,7 +232,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
let path = self.main_file_path;
generate_constraints::<_, G, _>(cs, self.program, self.program_input, &self.imported_programs).map_err(
|mut error| {
error.set_path(path);
error.set_path(&path);
error
},
)

View File

@ -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.to_owned());
error.set_path(main_file_path);
tracing::error!("{} failed due to error\n\n{}\n", full_test_name, error);

View File

@ -20,7 +20,7 @@ use leo_input::InputParserError;
use leo_state::LocalDataVerificationError;
use bincode::Error as SerdeError;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
#[derive(Debug, Error)]
pub enum CompilerError {
@ -65,7 +65,7 @@ pub enum CompilerError {
}
impl CompilerError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
CompilerError::InputParserError(error) => error.set_path(path),
CompilerError::FunctionError(error) => error.set_path(path),

View File

@ -17,7 +17,7 @@
use crate::errors::ExpressionError;
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum ConsoleError {
@ -29,7 +29,7 @@ pub enum ConsoleError {
}
impl ConsoleError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
ConsoleError::Expression(error) => error.set_path(path),
ConsoleError::Error(error) => error.set_path(path),

View File

@ -19,7 +19,7 @@ use leo_core::LeoCoreError;
use leo_typed::{Error as FormattedError, Identifier, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum ExpressionError {
@ -52,7 +52,7 @@ pub enum ExpressionError {
}
impl ExpressionError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
ExpressionError::AddressError(error) => error.set_path(path),
ExpressionError::BooleanError(error) => error.set_path(path),

View File

@ -27,7 +27,7 @@ use crate::errors::{
};
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum FunctionError {
@ -63,7 +63,7 @@ pub enum FunctionError {
}
impl FunctionError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
FunctionError::AddressError(error) => error.set_path(path),
FunctionError::BooleanError(error) => error.set_path(path),

View File

@ -18,7 +18,7 @@ use leo_ast::ParserError;
use leo_typed::{Error as FormattedError, Identifier, ImportSymbol, Span};
use leo_core::LeoCoreError;
use std::{io, path::PathBuf};
use std::{io, path::Path};
#[derive(Debug, Error)]
pub enum ImportError {
@ -37,7 +37,7 @@ impl ImportError {
ImportError::Error(FormattedError::new_from_span(message, span))
}
fn new_from_span_with_path(message: String, span: Span, path: PathBuf) -> Self {
fn new_from_span_with_path(message: String, span: Span, path: &Path) -> Self {
ImportError::Error(FormattedError::new_from_span_with_path(message, span, path))
}
@ -65,13 +65,13 @@ impl ImportError {
Self::new_from_span(message, span)
}
pub fn directory_error(error: io::Error, span: Span, path: PathBuf) -> Self {
pub fn directory_error(error: io::Error, span: Span, path: &Path) -> Self {
let message = format!("compilation failed due to directory error - {:?}", error);
Self::new_from_span_with_path(message, span, path)
}
pub fn star(path: PathBuf, span: Span) -> Self {
pub fn star(path: &Path, span: Span) -> Self {
let message = format!("cannot import `*` from path `{:?}`", path);
Self::new_from_span(message, span)

View File

@ -16,7 +16,7 @@
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum OutputBytesError {
@ -25,7 +25,7 @@ pub enum OutputBytesError {
}
impl OutputBytesError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
OutputBytesError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use crate::errors::{AddressError, BooleanError, ConsoleError, ExpressionError, IntegerError, ValueError};
use leo_typed::{Error as FormattedError, Span, Type};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum StatementError {
@ -44,7 +44,7 @@ pub enum StatementError {
}
impl StatementError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
StatementError::AddressError(error) => error.set_path(path),
StatementError::BooleanError(error) => error.set_path(path),

View File

@ -17,7 +17,7 @@
use leo_typed::{Error as FormattedError, Span};
use snarkos_errors::{gadgets::SynthesisError, objects::account::AccountError};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum AddressError {
@ -26,7 +26,7 @@ pub enum AddressError {
}
impl AddressError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
AddressError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use leo_typed::{Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum BooleanError {
@ -26,7 +26,7 @@ pub enum BooleanError {
}
impl BooleanError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
BooleanError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use leo_typed::{Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum FieldError {
@ -26,7 +26,7 @@ pub enum FieldError {
}
impl FieldError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
FieldError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use leo_typed::{Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum GroupError {
@ -26,7 +26,7 @@ pub enum GroupError {
}
impl GroupError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
GroupError::Error(error) => error.set_path(path),
}

View File

@ -18,7 +18,7 @@ use leo_gadgets::errors::SignedIntegerError;
use leo_typed::{error::Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum IntegerError {
@ -27,7 +27,7 @@ pub enum IntegerError {
}
impl IntegerError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
IntegerError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use crate::errors::{AddressError, BooleanError, FieldError, GroupError, IntegerError};
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum ValueError {
@ -41,7 +41,7 @@ pub enum ValueError {
}
impl ValueError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
ValueError::AddressError(error) => error.set_path(path),
ValueError::BooleanError(error) => error.set_path(path),

View File

@ -69,9 +69,9 @@ impl ImportParser {
}
let entries = fs::read_dir(path)
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), &error_path))?
.collect::<Result<Vec<_>, std::io::Error>>()
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?;
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), &error_path))?;
let matched_source_entry = entries.into_iter().find(|entry| {
entry
@ -87,9 +87,9 @@ impl ImportParser {
self.parse_core_package(&package)
} else if imports_directory.exists() {
let entries = fs::read_dir(imports_directory)
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), &error_path))?
.collect::<Result<Vec<_>, std::io::Error>>()
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?;
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), &error_path))?;
let matched_import_entry = entries
.into_iter()

View File

@ -27,7 +27,7 @@ fn parse_import_file(entry: &DirEntry, span: &Span) -> Result<Program, ImportErr
// make sure the given entry is file
let file_type = entry
.file_type()
.map_err(|error| ImportError::directory_error(error, span.clone(), entry.path()))?;
.map_err(|error| ImportError::directory_error(error, span.clone(), &entry.path()))?;
let file_name = entry
.file_name()
.into_string()
@ -92,7 +92,7 @@ impl ImportParser {
Ok(())
} else {
// importing * from a directory or non-leo file in `package/src/` is illegal
Err(ImportError::star(entry.path(), span.clone()))
Err(ImportError::star(&entry.path(), span.clone()))
}
}

View File

@ -19,7 +19,7 @@ use leo_typed::{Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error, Eq, PartialEq)]
pub enum CoreCircuitError {
@ -28,7 +28,7 @@ pub enum CoreCircuitError {
}
impl CoreCircuitError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
CoreCircuitError::Error(error) => error.set_path(path),
}

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum CorePackageError {
@ -24,7 +24,7 @@ pub enum CorePackageError {
}
impl CorePackageError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
CorePackageError::Error(error) => error.set_path(path),
}

View File

@ -17,7 +17,7 @@
use leo_typed::{Error as FormattedError, ImportSymbol, Span};
use crate::CorePackageError;
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum CorePackageListError {
@ -29,7 +29,7 @@ pub enum CorePackageListError {
}
impl CorePackageListError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
CorePackageListError::CorePackageError(error) => error.set_path(path),
CorePackageListError::Error(error) => error.set_path(path),

View File

@ -17,7 +17,7 @@
use crate::{CoreCircuitError, CorePackageListError};
use leo_typed::{Error as FormattedError, Span};
use std::path::PathBuf;
use std::path::Path;
#[derive(Debug, Error)]
pub enum LeoCoreError {
@ -32,7 +32,7 @@ pub enum LeoCoreError {
}
impl LeoCoreError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
match self {
LeoCoreError::CoreCircuitError(error) => error.set_path(path),
LeoCoreError::CorePackageListError(error) => error.set_path(path),

View File

@ -28,7 +28,11 @@ use pest::{
error::{Error, ErrorVariant},
Span,
};
use std::{num::ParseIntError, path::PathBuf, str::ParseBoolError};
use std::{
num::ParseIntError,
path::{Path, PathBuf},
str::ParseBoolError,
};
#[derive(Debug, Error)]
pub enum InputParserError {
@ -52,7 +56,7 @@ pub enum InputParserError {
}
impl InputParserError {
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
if let InputParserError::SyntaxError(error) = self {
let new_error: Error<Rule> = match error {
InputSyntaxError::Error(error) => {

View File

@ -124,9 +124,9 @@ impl CLI for BuildCommand {
main_file_path,
output_directory,
&input_string,
input_path.into_owned(),
&input_path,
&state_string,
state_path.into_owned(),
&state_path,
)?;
// Compute the current program checksum

View File

@ -16,7 +16,7 @@
use crate::Span;
use std::{fmt, path::PathBuf};
use std::{fmt, path::Path};
pub const INDENT: &str = " ";
@ -55,7 +55,7 @@ impl Error {
}
}
pub fn new_from_span_with_path(message: String, span: Span, path: PathBuf) -> Self {
pub fn new_from_span_with_path(message: String, span: Span, path: &Path) -> Self {
Self {
path: Some(format!("{:?}", path)),
line: span.line,
@ -66,7 +66,7 @@ impl Error {
}
}
pub fn set_path(&mut self, path: PathBuf) {
pub fn set_path(&mut self, path: &Path) {
self.path = Some(format!("{:?}", path));
}