Skip to content

Commit

Permalink
cover --overwrite with complex modes
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Dec 17, 2024
1 parent edb9033 commit 7eb411d
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 183 deletions.
79 changes: 34 additions & 45 deletions src/build/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::project::contract::vyper::ast::AST;
use crate::project::contract::vyper::expression::Expression as IR;
use crate::vyper::combined_json::contract::warning::Warning as CombinedJsonContractWarning;
use crate::vyper::combined_json::contract::Contract as CombinedJsonContract;
use crate::vyper::selection::Selection as VyperSelection;
use crate::vyper::selector::Selector as VyperSelector;

///
/// The Vyper contract build.
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Contract {
pub fn write_to_terminal(
self,
path: String,
selection: &[VyperSelection],
selection: &[VyperSelector],
) -> anyhow::Result<()> {
for warning in self.warnings.iter() {
writeln!(std::io::stderr(), "\n{warning}")?;
Expand All @@ -111,67 +111,67 @@ impl Contract {

for flag in selection.iter() {
match flag {
VyperSelection::IRJson => {
VyperSelector::IRJson => {
serde_json::to_writer(
std::io::stdout(),
self.ir_json.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::AST => {
VyperSelector::AST => {
serde_json::to_writer(
std::io::stdout(),
self.ast.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::ABI => {
VyperSelector::ABI => {
serde_json::to_writer(
std::io::stdout(),
self.abi.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::MethodIdentifiers => {
VyperSelector::MethodIdentifiers => {
serde_json::to_writer(
std::io::stdout(),
self.method_identifiers.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::Layout => {
VyperSelector::Layout => {
serde_json::to_writer(
std::io::stdout(),
self.layout.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::UserDocumentation => {
VyperSelector::UserDocumentation => {
serde_json::to_writer(
std::io::stdout(),
self.userdoc.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}
VyperSelection::DeveloperDocumentation => {
VyperSelector::DeveloperDocumentation => {
serde_json::to_writer(
std::io::stdout(),
self.devdoc.as_ref().expect("Always exists"),
)?;
writeln!(std::io::stdout())?;
}

VyperSelection::EraVMAssembly => {
VyperSelector::EraVMAssembly => {
writeln!(std::io::stderr(), "Contract `{path}` assembly:")?;
writeln!(
std::io::stdout(),
"{}",
self.build.assembly.as_ref().expect("Always exists")
)?;
}
VyperSelection::ProjectMetadata => {}
VyperSelector::ProjectMetadata => {}

VyperSelection::CombinedJson => {
VyperSelector::CombinedJson => {
panic!("Combined JSON is printed with another pipeline");
}
}
Expand All @@ -185,7 +185,7 @@ impl Contract {
///
pub fn write_to_directory(
self,
selection: &[VyperSelection],
selection: &[VyperSelector],
output_directory: &Path,
contract_path: &Path,
overwrite: bool,
Expand Down Expand Up @@ -234,57 +234,57 @@ impl Contract {
})?;
for flag in selection.iter() {
match flag {
VyperSelection::IRJson => {
VyperSelector::IRJson => {
serde_json::to_writer(
&extra_output_file,
self.ir_json.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::AST => {
VyperSelector::AST => {
serde_json::to_writer(
&extra_output_file,
self.ast.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::ABI => {
VyperSelector::ABI => {
serde_json::to_writer(
&extra_output_file,
self.abi.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::MethodIdentifiers => {
VyperSelector::MethodIdentifiers => {
serde_json::to_writer(
&extra_output_file,
self.method_identifiers.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::Layout => {
VyperSelector::Layout => {
serde_json::to_writer(
&extra_output_file,
self.layout.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::UserDocumentation => {
VyperSelector::UserDocumentation => {
serde_json::to_writer(
&extra_output_file,
self.userdoc.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}
VyperSelection::DeveloperDocumentation => {
VyperSelector::DeveloperDocumentation => {
serde_json::to_writer(
&extra_output_file,
self.devdoc.as_ref().expect("Always exists"),
)?;
writeln!(&extra_output_file)?;
}

VyperSelection::EraVMAssembly => {
VyperSelector::EraVMAssembly => {
let assembly_file_name = format!(
"{}.{}",
file_name,
Expand All @@ -297,32 +297,21 @@ impl Contract {
"Refusing to overwrite an existing file {assembly_file_path:?} (use --overwrite to force).",
);
}
File::create(&assembly_file_path)
.map_err(|error| {
anyhow::anyhow!(
"File {:?} creating error: {}",
assembly_file_path,
error
)
})?
.write_all(
self.build
.assembly
.as_ref()
.expect("Always exists")
.as_bytes(),
)
.map_err(|error| {
anyhow::anyhow!(
"File {:?} writing error: {}",
assembly_file_path,
error
)
})?;
std::fs::write(
&assembly_file_path,
self.build
.assembly
.as_ref()
.expect("Always exists")
.as_bytes(),
)
.map_err(|error| {
anyhow::anyhow!("File {assembly_file_path:?} writing error: {error}")
})?;
}
VyperSelection::ProjectMetadata => {}
VyperSelector::ProjectMetadata => {}

VyperSelection::CombinedJson => {
VyperSelector::CombinedJson => {
panic!("Combined JSON is printed with another pipeline");
}
}
Expand Down
32 changes: 14 additions & 18 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
pub mod contract;

use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
Expand All @@ -14,7 +13,7 @@ use normpath::PathExt;

use crate::vyper::combined_json::extra_data::ExtraData as CombinedJsonExtraData;
use crate::vyper::combined_json::CombinedJson;
use crate::vyper::selection::Selection as VyperSelection;
use crate::vyper::selector::Selector as VyperSelector;
use crate::vyper::Compiler as VyperCompiler;

use self::contract::Contract;
Expand Down Expand Up @@ -80,12 +79,12 @@ impl Build {
///
/// Writes all contracts to the terminal.
///
pub fn write_to_terminal(self, selection: &[VyperSelection]) -> anyhow::Result<()> {
pub fn write_to_terminal(self, selection: &[VyperSelector]) -> anyhow::Result<()> {
for (path, contract) in self.contracts.into_iter() {
contract.write_to_terminal(path, selection)?;
}

if selection.contains(&VyperSelection::ProjectMetadata) {
if selection.contains(&VyperSelector::ProjectMetadata) {
writeln!(std::io::stderr(), "Project metadata:")?;
writeln!(std::io::stdout(), "{}", self.project_metadata)?;
}
Expand All @@ -98,7 +97,7 @@ impl Build {
///
pub fn write_to_directory(
self,
selection: &[VyperSelection],
selection: &[VyperSelector],
output_directory: &Path,
overwrite: bool,
) -> anyhow::Result<()> {
Expand All @@ -113,7 +112,7 @@ impl Build {
)?;
}

if selection.contains(&VyperSelection::ProjectMetadata) {
if selection.contains(&VyperSelector::ProjectMetadata) {
let metadata_file_name = format!("meta.{}", era_compiler_common::EXTENSION_JSON);
let mut metadata_file_path = output_directory.to_owned();
metadata_file_path.push(metadata_file_name);
Expand All @@ -122,18 +121,15 @@ impl Build {
"Refusing to overwrite an existing file {metadata_file_path:?} (use --overwrite to force).",
);
}
File::create(&metadata_file_path)
.map_err(|error| {
anyhow::anyhow!("File {:?} creating error: {}", metadata_file_path, error)
})?
.write_all(
serde_json::to_string(&self.project_metadata)
.expect("Always valid")
.as_bytes(),
)
.map_err(|error| {
anyhow::anyhow!("File {:?} writing error: {}", metadata_file_path, error)
})?;
std::fs::write(
&metadata_file_path,
serde_json::to_string(&self.project_metadata)
.expect("Always valid")
.as_bytes(),
)
.map_err(|error| {
anyhow::anyhow!("File {metadata_file_path:?} writing error: {error}")
})?;
}

Ok(())
Expand Down
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use self::project::Project;
pub use self::r#const::*;
pub use self::vyper::combined_json::contract::Contract as VyperCompilerCombinedJsonContract;
pub use self::vyper::combined_json::CombinedJson as VyperCompilerCombinedJson;
pub use self::vyper::selection::Selection as VyperSelection;
pub use self::vyper::selector::Selector as VyperSelector;
pub use self::vyper::standard_json::input::language::Language as VyperCompilerStandardInputJsonLanguage;
pub use self::vyper::standard_json::input::settings::selection::Selection as VyperCompilerStandardInputJsonSettingsSelection;
pub use self::vyper::standard_json::input::settings::Settings as VyperCompilerStandardInputJsonSettings;
Expand Down Expand Up @@ -53,7 +53,7 @@ use rayon::iter::ParallelIterator;
///
pub fn llvm_ir(
input_paths: Vec<PathBuf>,
output_selection: &[VyperSelection],
output_selection: &[VyperSelector],
metadata_hash_type: era_compiler_common::HashType,
optimizer_settings: era_compiler_llvm_context::OptimizerSettings,
llvm_options: Vec<String>,
Expand All @@ -80,7 +80,7 @@ pub fn llvm_ir(
///
pub fn eravm_assembly(
input_paths: Vec<PathBuf>,
output_selection: &[VyperSelection],
output_selection: &[VyperSelector],
metadata_hash_type: era_compiler_common::HashType,
llvm_options: Vec<String>,
suppressed_warnings: Vec<WarningType>,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn eravm_assembly(
pub fn standard_output(
input_paths: Vec<PathBuf>,
vyper: &VyperCompiler,
output_selection: &[VyperSelection],
output_selection: &[VyperSelector],
evm_version: Option<era_compiler_common::EVMVersion>,
enable_decimals: bool,
search_paths: Option<Vec<String>>,
Expand Down Expand Up @@ -167,16 +167,16 @@ pub fn combined_json(
) -> anyhow::Result<VyperCompilerCombinedJson> {
let zkvyper_version = semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Always valid");

let output_selection: Vec<VyperSelection> = vec![
VyperSelection::IRJson,
VyperSelection::AST,
VyperSelection::ABI,
VyperSelection::MethodIdentifiers,
VyperSelection::Layout,
VyperSelection::UserDocumentation,
VyperSelection::DeveloperDocumentation,
VyperSelection::EraVMAssembly,
VyperSelection::ProjectMetadata,
let output_selection: Vec<VyperSelector> = vec![
VyperSelector::IRJson,
VyperSelector::AST,
VyperSelector::ABI,
VyperSelector::MethodIdentifiers,
VyperSelector::Layout,
VyperSelector::UserDocumentation,
VyperSelector::DeveloperDocumentation,
VyperSelector::EraVMAssembly,
VyperSelector::ProjectMetadata,
];

let project: Project = vyper.batch(
Expand Down
6 changes: 3 additions & 3 deletions src/process/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::borrow::Cow;

use crate::project::contract::Contract;
use crate::vyper::selection::Selection as VyperSelection;
use crate::vyper::selector::Selector as VyperSelector;
use crate::warning_type::WarningType;

///
Expand All @@ -22,7 +22,7 @@ pub struct Input<'a> {
/// The metadata hash.
pub metadata_hash: Option<era_compiler_common::Hash>,
/// The output selection flags.
pub output_selection: Vec<VyperSelection>,
pub output_selection: Vec<VyperSelector>,
/// The optimizer settings.
pub optimizer_settings: era_compiler_llvm_context::OptimizerSettings,
/// The extra LLVM arguments.
Expand All @@ -41,7 +41,7 @@ impl<'a> Input<'a> {
full_path: Cow<'a, String>,
contract: Cow<'a, Contract>,
metadata_hash: Option<era_compiler_common::Hash>,
output_selection: Vec<VyperSelection>,
output_selection: Vec<VyperSelector>,
optimizer_settings: era_compiler_llvm_context::OptimizerSettings,
llvm_options: Vec<String>,
suppressed_warnings: Vec<WarningType>,
Expand Down
Loading

0 comments on commit 7eb411d

Please sign in to comment.