Skip to content

Commit

Permalink
Extract check assertions command from the assertions module
Browse files Browse the repository at this point in the history
Leave only the assertions related logic (extraction and checking) in the
assertions module.
  • Loading branch information
ggiraldez committed Jun 18, 2024
1 parent 1d0a99e commit 96c91b1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 63 deletions.
3 changes: 0 additions & 3 deletions crates/codegen/runtime/cargo/src/runtime/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,4 @@ pub enum CommandError {
#[cfg(feature = "__experimental_bindings_api")]
#[error(transparent)]
BindingsError(#[from] crate::bindings::BindingsError),

#[error("Unknown error: {0}")]
Unknown(String),
}
51 changes: 16 additions & 35 deletions crates/solidity/outputs/cargo/slang_solidity/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@ use std::cmp::Ordering;
use std::collections::HashMap;

use regex::Regex;
use semver::Version;
use thiserror::Error;

use slang_solidity::bindings::Bindings;
use slang_solidity::cursor::Cursor;
use slang_solidity::kinds::TerminalKind;
use slang_solidity::query::Query;
use thiserror::Error;

use slang_solidity::cli::commands;
use slang_solidity::cli::commands::CommandError;

pub fn execute_check_assertions(
file_path_string: &str,
version: Version,
) -> Result<(), CommandError> {
let mut bindings = Bindings::create(version.clone());
let parse_output = commands::parse::parse_source_file(file_path_string, version, |_| ())?;
let tree_cursor = parse_output.create_tree_cursor();

bindings.add_file(file_path_string, tree_cursor.clone())?;
let assertions =
collect_assertions(tree_cursor).map_err(|e| CommandError::Unknown(e.to_string()))?;
#[derive(Debug, Error)]
pub enum AssertionError {
#[error("Invalid assertion at {0}:{1}")]
InvalidAssertion(usize, usize),

check_assertions(&bindings, &assertions)?;
#[error("Duplicate assertion definition {0}")]
DuplicateDefinition(String),

Ok(())
#[error("Failed {0} of {1} bindings assertions")]
FailedAssertions(usize, usize),
}

fn check_assertions(bindings: &Bindings, assertions: &Assertions) -> Result<(), CommandError> {
pub fn check_assertions(
bindings: &Bindings,
assertions: &Assertions,
) -> Result<(), AssertionError> {
let mut count = 0;
let mut success = 0;

Expand Down Expand Up @@ -116,26 +109,14 @@ fn check_assertions(bindings: &Bindings, assertions: &Assertions) -> Result<(),

if count > success {
eprintln!();
Err(CommandError::Unknown(format!(
"Failed {failed} of {count} bindings assertions",
failed = count - success
)))
Err(AssertionError::FailedAssertions(count - success, count))
} else {
println!("{count} binding assertions OK");
Ok(())
}
}

#[derive(Debug, Error)]
enum AssertionError {
#[error("Invalid assertion at {0}:{1}")]
InvalidAssertion(usize, usize),

#[error("Duplicate assertion definition {0}")]
DuplicateDefinition(String),
}

fn collect_assertions(cursor: Cursor) -> Result<Assertions, AssertionError> {
pub fn collect_assertions(cursor: Cursor) -> Result<Assertions, AssertionError> {
let mut assertions = Assertions::new();

let query = Query::parse("@comment [SingleLineComment]").unwrap();
Expand All @@ -153,7 +134,7 @@ fn collect_assertions(cursor: Cursor) -> Result<Assertions, AssertionError> {
Ok(assertions)
}

struct Assertions {
pub struct Assertions {
definitions: HashMap<String, Assertion>,
references: Vec<Assertion>,
}
Expand Down
50 changes: 43 additions & 7 deletions crates/solidity/outputs/cargo/slang_solidity/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#[allow(unused_imports)]
use slang_solidity::cli::commands::CommandError;
use clap::Subcommand;
use std::process::ExitCode;

use clap::Subcommand;
use semver::Version;
use slang_solidity::bindings::Bindings;
use slang_solidity::cli::commands;
use slang_solidity::cli::commands::CommandError;
use thiserror::Error;

#[derive(Subcommand, Debug)]
pub enum CustomCommands {
pub enum LocalCommands {
#[cfg(feature = "__experimental_bindings_api")]
CheckAssertions {
/// File path to the source file to parse
Expand All @@ -16,17 +20,30 @@ pub enum CustomCommands {
},
}

impl CustomCommands {
#[derive(Error, Debug)]
pub enum LocalCommandError {
#[error(transparent)]
Command(#[from] CommandError),

#[cfg(feature = "__experimental_bindings_api")]
#[error(transparent)]
Assertion(#[from] crate::assertions::AssertionError),

#[error(transparent)]
Bindings(#[from] slang_solidity::bindings::BindingsError),
}

impl LocalCommands {
#[cfg(not(feature = "__experimental_bindings_api"))]
pub fn execute(self) -> ExitCode {
unreachable!()
}

#[cfg(feature = "__experimental_bindings_api")]
pub fn execute(self) -> ExitCode {
let result: Result<(), CommandError> = match self {
let result: Result<(), LocalCommandError> = match self {
Self::CheckAssertions { file_path, version } => {
super::assertions::execute_check_assertions(&file_path, version)
check_assertions_command::execute(&file_path, version)
}
};
match result {
Expand All @@ -38,3 +55,22 @@ impl CustomCommands {
}
}
}

#[cfg(feature = "__experimental_bindings_api")]
mod check_assertions_command {
use super::{commands, Bindings, LocalCommandError, Version};
use crate::assertions;

pub fn execute(file_path_string: &str, version: Version) -> Result<(), LocalCommandError> {
let mut bindings = Bindings::create(version.clone());
let parse_output = commands::parse::parse_source_file(file_path_string, version, |_| ())?;
let tree_cursor = parse_output.create_tree_cursor();

bindings.add_file(file_path_string, tree_cursor.clone())?;
let assertions = assertions::collect_assertions(tree_cursor)?;

assertions::check_assertions(&bindings, &assertions)?;

Ok(())
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions crates/solidity/outputs/cargo/slang_solidity/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::process::ExitCode;

use clap::{
error::Error, error::ErrorKind, ArgMatches, Command, FromArgMatches, Parser as ClapParser,
Subcommand,
};
use clap::error::{Error, ErrorKind};
use clap::{ArgMatches, Command, FromArgMatches, Parser as ClapParser, Subcommand};
use slang_solidity::cli;

// Below are dependencies used by the API `lib.rs`, but not the CLI "main.rs".
Expand All @@ -23,7 +21,7 @@ mod supress_api_dependencies {
mod assertions;
mod commands;

use commands::CustomCommands;
use commands::LocalCommands;

#[derive(ClapParser, Debug)]
#[command(next_line_help = true)]
Expand All @@ -36,7 +34,7 @@ struct Cli {
#[derive(Debug)]
enum CliCommand {
Common(cli::Commands),
Custom(CustomCommands),
Custom(LocalCommands),
}

impl CliCommand {
Expand All @@ -52,7 +50,7 @@ impl FromArgMatches for CliCommand {
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
if let Ok(common) = cli::Commands::from_arg_matches(matches) {
Ok(Self::Common(common))
} else if let Ok(custom) = CustomCommands::from_arg_matches(matches) {
} else if let Ok(custom) = LocalCommands::from_arg_matches(matches) {
Ok(Self::Custom(custom))
} else {
Err(Error::new(ErrorKind::MissingSubcommand))
Expand All @@ -61,7 +59,7 @@ impl FromArgMatches for CliCommand {
fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
if let Ok(common) = cli::Commands::from_arg_matches(matches) {
*self = Self::Common(common);
} else if let Ok(custom) = CustomCommands::from_arg_matches(matches) {
} else if let Ok(custom) = LocalCommands::from_arg_matches(matches) {
*self = Self::Custom(custom);
} else {
return Err(Error::new(ErrorKind::MissingSubcommand));
Expand All @@ -73,14 +71,14 @@ impl FromArgMatches for CliCommand {
impl Subcommand for CliCommand {
fn augment_subcommands(cmd: Command) -> Command {
let cmd = cli::Commands::augment_subcommands(cmd);
CustomCommands::augment_subcommands(cmd)
LocalCommands::augment_subcommands(cmd)
}
fn augment_subcommands_for_update(cmd: Command) -> Command {
let cmd = cli::Commands::augment_subcommands(cmd);
CustomCommands::augment_subcommands(cmd)
LocalCommands::augment_subcommands(cmd)
}
fn has_subcommand(name: &str) -> bool {
cli::Commands::has_subcommand(name) || CustomCommands::has_subcommand(name)
cli::Commands::has_subcommand(name) || LocalCommands::has_subcommand(name)
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/solidity/outputs/cargo/tests/src/bindings/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ fn print_graph_as_mermaid(graph: &Graph) -> impl fmt::Display + '_ {
} else {
format!("{}", node.index())
};
let source = gn.attributes.get("__match").unwrap().as_syntax_node_ref().unwrap();
let source = gn
.attributes
.get("__match")
.unwrap()
.as_syntax_node_ref()
.unwrap();
let location = gn.attributes.get("__location").unwrap();

let node_label = format!(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96c91b1

Please sign in to comment.