Skip to content

Commit

Permalink
feat(cli): pass solidity version as an arg (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankudoags authored Aug 26, 2023
1 parent c5615dd commit e7fc52f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
44 changes: 28 additions & 16 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ pub struct Emitter {
with_actions_as_comments: bool,
/// The indentation level of the emitted code.
indent: usize,
/// The solidity version to use in the emitted code.
version: String,
}

impl Emitter {
/// Create a new emitter with the given configuration.
pub fn new(with_actions_as_comments: bool, indent: usize) -> Self {
pub fn new(with_actions_as_comments: bool, indent: usize, version: String) -> Self {
Self {
with_actions_as_comments,
indent,
version,
}
}

Expand Down Expand Up @@ -93,7 +96,8 @@ impl<'a> EmitterI<'a> {
/// - The contract's name.
fn emit_contract_header(&self, root: &ast::Root) -> String {
let mut emitted = String::new();
emitted.push_str("pragma solidity [VERSION];\n\n");
// add version from self.emitter.version which is a String
emitted.push_str(&format!("pragma solidity {};\n\n", self.emitter.version));

// It's fine to unwrap here because we check that the filename always has an extension.
let contract_name = root.file_name.split_once('.').unwrap().0;
Expand Down Expand Up @@ -285,16 +289,24 @@ mod tests {
use crate::parser::Parser;
use crate::tokenizer::Tokenizer;

fn scaffold_with_flags(text: &str, with_comments: bool, indent: usize) -> Result<String> {
fn scaffold_with_flags(
text: &str,
with_comments: bool,
indent: usize,
version: &str,
) -> Result<String> {
let tokens = Tokenizer::new().tokenize(&text)?;
let ast = Parser::new().parse(&text, &tokens)?;
let mut discoverer = modifiers::ModifierDiscoverer::new();
let modifiers = discoverer.discover(&ast);
Ok(emitter::Emitter::new(with_comments, indent).emit(&ast, &modifiers))
Ok(
emitter::Emitter::new(with_comments, indent, version.to_string())
.emit(&ast, &modifiers),
)
}

fn scaffold(text: &str) -> Result<String> {
scaffold_with_flags(text, true, 2)
scaffold_with_flags(text, true, 2, "0.8.0")
}

#[test]
Expand All @@ -304,7 +316,7 @@ mod tests {

assert_eq!(
&scaffold(&file_contents)?,
r"pragma solidity [VERSION];
r"pragma solidity 0.8.0;
contract FileTest {
modifier whenSomethingBadHappens() {
Expand All @@ -326,7 +338,7 @@ contract FileTest {

assert_eq!(
&scaffold(&file_contents)?,
r"pragma solidity [VERSION];
r"pragma solidity 0.8.0;
contract FileTest {
modifier whenSomethingBadHappens() {
Expand All @@ -351,8 +363,8 @@ contract FileTest {
String::from("file.sol\n└── when something bad happens\n └── it should not revert");

assert_eq!(
&scaffold_with_flags(&file_contents, false, 2)?,
r"pragma solidity [VERSION];
&scaffold_with_flags(&file_contents, false, 2, "0.8.0")?,
r"pragma solidity 0.8.0;
contract FileTest {
modifier whenSomethingBadHappens() {
Expand All @@ -376,8 +388,8 @@ contract FileTest {
String::from("fi-e.sol\n└── when something bad happens\n └── it should not revert");

assert_eq!(
&scaffold_with_flags(&file_contents, false, 2)?,
r"pragma solidity [VERSION];
&scaffold_with_flags(&file_contents, false, 2, "0.8.0")?,
r"pragma solidity 0.8.0;
contract Fi_eTest {
modifier whenSomethingBadHappens() {
Expand All @@ -401,8 +413,8 @@ contract Fi_eTest {
String::from("file.sol\n└── when something bad happens\n └── it should not revert");

assert_eq!(
&scaffold_with_flags(&file_contents, false, 4)?,
r"pragma solidity [VERSION];
&scaffold_with_flags(&file_contents, false, 4, "0.8.0")?,
r"pragma solidity 0.8.0;
contract FileTest {
modifier whenSomethingBadHappens() {
Expand Down Expand Up @@ -432,7 +444,7 @@ contract FileTest {

assert_eq!(
&scaffold(&file_contents)?,
r"pragma solidity [VERSION];
r"pragma solidity 0.8.0;
contract Two_childrenTest {
modifier whenStuffCalled() {
Expand Down Expand Up @@ -475,7 +487,7 @@ contract Two_childrenTest {

assert_eq!(
&scaffold(&file_contents)?,
r"pragma solidity [VERSION];
r"pragma solidity 0.8.0;
contract ActionsTest {
modifier whenStuffCalled() {
Expand Down Expand Up @@ -522,7 +534,7 @@ contract ActionsTest {

assert_eq!(
&scaffold(&file_contents)?,
r"pragma solidity [VERSION];
r"pragma solidity 0.8.0;
contract DeepTest {
modifier whenStuffCalled() {
Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,20 @@ pub struct Compiled {
/// See the [crate-level documentation] for details.
///
/// [crate-level documentation]: ./index.html
pub fn scaffold(text: &str, with_comments: bool, indent: usize) -> error::Result<Compiled> {
pub fn scaffold(
text: &str,
with_comments: bool,
indent: usize,
solidity_version: &str,
) -> error::Result<Compiled> {
let tokens = tokenizer::Tokenizer::new().tokenize(text)?;
let ast = parser::Parser::new().parse(text, &tokens)?;
let mut analyzer = semantics::SemanticAnalyzer::new(text);
analyzer.analyze(&ast)?;
let mut discoverer = modifiers::ModifierDiscoverer::new();
let modifiers = discoverer.discover(&ast);
let emitted = emitter::Emitter::new(with_comments, indent).emit(&ast, modifiers);
let emitted = emitter::Emitter::new(with_comments, indent, solidity_version.to_string())
.emit(&ast, modifiers);

let output_file = match ast {
ast::Ast::Root(root) => root.file_name,
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct Config {
/// overwrite the output files.
#[arg(short = 'f', long, requires = "file-handling", default_value = "false")]
force_write: bool,

/// Arg to specify the output solidity version for the tests.
#[arg(short = 's', long, default_value = "0.8.0")]
solidity_version: String,
}

fn main() -> Result<()> {
Expand All @@ -53,7 +57,12 @@ fn run(config: &Config) -> Result<()> {
// to the filesystem.
for file in config.files.iter() {
let text = fs::read_to_string(file)?;
match scaffold(&text, config.with_actions_as_comments, config.indent) {
match scaffold(
&text,
config.with_actions_as_comments,
config.indent,
config.solidity_version.as_str(),
) {
Ok(compiled) => {
if config.write_files {
let mut output_path = file.clone();
Expand Down

0 comments on commit e7fc52f

Please sign in to comment.