From 64774cdab670ef8d75e8f4c18633c07e4e10a51d Mon Sep 17 00:00:00 2001 From: ljedrz Date: Fri, 16 Oct 2020 17:05:41 +0200 Subject: [PATCH] refactor: accept &Path instead of PathBuf where sufficient Signed-off-by: ljedrz --- ast/src/common/range_or_expression.rs | 12 +++++++++-- ast/src/errors/parser.rs | 4 ++-- compiler/src/compiler.rs | 24 ++++++++++++--------- compiler/src/constraints/constraints.rs | 2 +- compiler/src/errors/compiler.rs | 4 ++-- compiler/src/errors/console.rs | 4 ++-- compiler/src/errors/expression.rs | 4 ++-- compiler/src/errors/function.rs | 4 ++-- compiler/src/errors/import.rs | 8 +++---- compiler/src/errors/output_bytes.rs | 4 ++-- compiler/src/errors/statement.rs | 4 ++-- compiler/src/errors/value/address.rs | 4 ++-- compiler/src/errors/value/boolean.rs | 4 ++-- compiler/src/errors/value/field.rs | 4 ++-- compiler/src/errors/value/group.rs | 4 ++-- compiler/src/errors/value/integer.rs | 4 ++-- compiler/src/errors/value/value.rs | 4 ++-- compiler/src/import/parser/parse_package.rs | 8 +++---- compiler/src/import/parser/parse_symbol.rs | 4 ++-- core/src/errors/core_circuit.rs | 4 ++-- core/src/errors/core_package.rs | 4 ++-- core/src/errors/core_package_list.rs | 4 ++-- core/src/errors/leo_core.rs | 4 ++-- input/src/errors/parser.rs | 8 +++++-- leo/commands/build.rs | 4 ++-- typed/src/errors/error.rs | 6 +++--- 26 files changed, 80 insertions(+), 64 deletions(-) diff --git a/ast/src/common/range_or_expression.rs b/ast/src/common/range_or_expression.rs index 58f79cabe4..5a13b3e4cc 100644 --- a/ast/src/common/range_or_expression.rs +++ b/ast/src/common/range_or_expression.rs @@ -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()), ), } } diff --git a/ast/src/errors/parser.rs b/ast/src/errors/parser.rs index bfeb7c7ada..3cbe2cb6b9 100644 --- a/ast/src/errors/parser.rs +++ b/ast/src/errors/parser.rs @@ -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 = match error { SyntaxError::Error(error) => { diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 946cfbed19..de552e62cd 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -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> { @@ -71,17 +75,17 @@ impl> Compiler { 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> Compiler { 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 { let mut compiler = Self::new(package_name, main_file_path, output_directory); @@ -149,7 +153,7 @@ impl> Compiler { 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> Compiler { generate_constraints::(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> Compiler { 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 }, ) diff --git a/compiler/src/constraints/constraints.rs b/compiler/src/constraints/constraints.rs index 963131eefc..803cfefcdf 100644 --- a/compiler/src/constraints/constraints.rs +++ b/compiler/src/constraints/constraints.rs @@ -147,7 +147,7 @@ pub fn generate_test_constraints>( (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); diff --git a/compiler/src/errors/compiler.rs b/compiler/src/errors/compiler.rs index 0f1bc30a7f..762b0f0e1c 100644 --- a/compiler/src/errors/compiler.rs +++ b/compiler/src/errors/compiler.rs @@ -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), diff --git a/compiler/src/errors/console.rs b/compiler/src/errors/console.rs index 4079aa872e..330dd15d52 100644 --- a/compiler/src/errors/console.rs +++ b/compiler/src/errors/console.rs @@ -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), diff --git a/compiler/src/errors/expression.rs b/compiler/src/errors/expression.rs index 605d6aa666..bb0ee8577f 100644 --- a/compiler/src/errors/expression.rs +++ b/compiler/src/errors/expression.rs @@ -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), diff --git a/compiler/src/errors/function.rs b/compiler/src/errors/function.rs index d41af4c915..f4ccd09231 100644 --- a/compiler/src/errors/function.rs +++ b/compiler/src/errors/function.rs @@ -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), diff --git a/compiler/src/errors/import.rs b/compiler/src/errors/import.rs index 956a329019..221effa76f 100644 --- a/compiler/src/errors/import.rs +++ b/compiler/src/errors/import.rs @@ -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) diff --git a/compiler/src/errors/output_bytes.rs b/compiler/src/errors/output_bytes.rs index 97183fff6f..d9ef930f35 100644 --- a/compiler/src/errors/output_bytes.rs +++ b/compiler/src/errors/output_bytes.rs @@ -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), } diff --git a/compiler/src/errors/statement.rs b/compiler/src/errors/statement.rs index b5f3002722..e39dd2e53d 100644 --- a/compiler/src/errors/statement.rs +++ b/compiler/src/errors/statement.rs @@ -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), diff --git a/compiler/src/errors/value/address.rs b/compiler/src/errors/value/address.rs index ea8b2424e6..52e84aaccd 100644 --- a/compiler/src/errors/value/address.rs +++ b/compiler/src/errors/value/address.rs @@ -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), } diff --git a/compiler/src/errors/value/boolean.rs b/compiler/src/errors/value/boolean.rs index dee94702aa..cafdc4e3f3 100644 --- a/compiler/src/errors/value/boolean.rs +++ b/compiler/src/errors/value/boolean.rs @@ -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), } diff --git a/compiler/src/errors/value/field.rs b/compiler/src/errors/value/field.rs index 47d06a0548..200749be95 100644 --- a/compiler/src/errors/value/field.rs +++ b/compiler/src/errors/value/field.rs @@ -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), } diff --git a/compiler/src/errors/value/group.rs b/compiler/src/errors/value/group.rs index 6082c251c3..63c20d5aad 100644 --- a/compiler/src/errors/value/group.rs +++ b/compiler/src/errors/value/group.rs @@ -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), } diff --git a/compiler/src/errors/value/integer.rs b/compiler/src/errors/value/integer.rs index 91c307bbec..1133a487f9 100644 --- a/compiler/src/errors/value/integer.rs +++ b/compiler/src/errors/value/integer.rs @@ -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), } diff --git a/compiler/src/errors/value/value.rs b/compiler/src/errors/value/value.rs index ee08a75293..8f4a3248fa 100644 --- a/compiler/src/errors/value/value.rs +++ b/compiler/src/errors/value/value.rs @@ -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), diff --git a/compiler/src/import/parser/parse_package.rs b/compiler/src/import/parser/parse_package.rs index 960bd24d65..bf2ed6cb7d 100644 --- a/compiler/src/import/parser/parse_package.rs +++ b/compiler/src/import/parser/parse_package.rs @@ -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::, 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::, 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() diff --git a/compiler/src/import/parser/parse_symbol.rs b/compiler/src/import/parser/parse_symbol.rs index cc68184dde..4e161ea849 100644 --- a/compiler/src/import/parser/parse_symbol.rs +++ b/compiler/src/import/parser/parse_symbol.rs @@ -27,7 +27,7 @@ fn parse_import_file(entry: &DirEntry, span: &Span) -> Result error.set_path(path), } diff --git a/core/src/errors/core_package.rs b/core/src/errors/core_package.rs index 30425d61d9..2a285abfb7 100644 --- a/core/src/errors/core_package.rs +++ b/core/src/errors/core_package.rs @@ -15,7 +15,7 @@ // along with the Leo library. If not, see . 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), } diff --git a/core/src/errors/core_package_list.rs b/core/src/errors/core_package_list.rs index 4cf97980cf..cc23101924 100644 --- a/core/src/errors/core_package_list.rs +++ b/core/src/errors/core_package_list.rs @@ -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), diff --git a/core/src/errors/leo_core.rs b/core/src/errors/leo_core.rs index 90aecef934..1c59ed9bc8 100644 --- a/core/src/errors/leo_core.rs +++ b/core/src/errors/leo_core.rs @@ -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), diff --git a/input/src/errors/parser.rs b/input/src/errors/parser.rs index 3f0fea85a7..24bfaa5e52 100644 --- a/input/src/errors/parser.rs +++ b/input/src/errors/parser.rs @@ -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 = match error { InputSyntaxError::Error(error) => { diff --git a/leo/commands/build.rs b/leo/commands/build.rs index 9d19bd2352..57c45dcca9 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -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 diff --git a/typed/src/errors/error.rs b/typed/src/errors/error.rs index ba787ef9c1..b6c2372050 100644 --- a/typed/src/errors/error.rs +++ b/typed/src/errors/error.rs @@ -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)); }