From 7e05529ccecf53a85cac168ac3f6a999d681293d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Thu, 3 Oct 2024 17:27:40 -0400 Subject: [PATCH] Use writeln! to generate the built-ins contents --- crates/solidity/inputs/language/src/lib.rs | 80 +++++++++---------- .../generated/bindings/generated/built_ins.rs | 12 +-- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/crates/solidity/inputs/language/src/lib.rs b/crates/solidity/inputs/language/src/lib.rs index dd2328a5c..86091fd83 100644 --- a/crates/solidity/inputs/language/src/lib.rs +++ b/crates/solidity/inputs/language/src/lib.rs @@ -2,60 +2,58 @@ mod definition; +use std::fmt::{Error, Write}; + use codegen_language_definition::model::BuiltIn; pub use definition::SolidityDefinition; pub fn render_built_ins(built_ins: &[BuiltIn]) -> String { - let mut lines: Vec = Vec::new(); - lines.push("contract $$ {".into()); + try_render_built_ins(built_ins).expect("Failed to render built-ins") +} + +fn try_render_built_ins(built_ins: &[BuiltIn]) -> Result { + let mut buffer = String::new(); + writeln!(buffer, "contract $$ {{")?; for item in built_ins { match item { BuiltIn::BuiltInFunction { item } => { - let return_type = item - .return_type - .as_ref() - .map(|return_type| format!(" returns ({return_type})")) - .unwrap_or_default(); - let parameters = item.parameters.join(", "); - lines.push(format!( + writeln!( + buffer, "function {name}({parameters}) public{return_type};", - name = item.name - )); + name = item.name, + parameters = item.parameters.join(", "), + return_type = item + .return_type + .as_ref() + .map(|return_type| format!(" returns ({return_type})")) + .unwrap_or_default(), + )?; } BuiltIn::BuiltInType { item } => { - let fields = item - .fields - .iter() - .map(|field| format!(" {field_def};", field_def = field.definition)) - .collect::>() - .join("\n"); - let functions = item - .functions - .iter() - .map(|function| { - format!( - " function({parameters}){return_type} {name};", - parameters = function.parameters.join(", "), - return_type = function - .return_type - .as_deref() - .map(|return_type| format!(" returns ({return_type})")) - .unwrap_or_default(), - name = function.name - ) - }) - .collect::>() - .join("\n"); - lines.push(format!( - "struct {name} {{\n{fields}\n{functions}\n}}", - name = item.name - )); + writeln!(buffer, "struct {name} {{", name = item.name)?; + for field in &item.fields { + writeln!(buffer, " {field_def};", field_def = field.definition)?; + } + for function in &item.functions { + writeln!( + buffer, + " function({parameters}){return_type} {name};", + name = function.name, + return_type = function + .return_type + .as_deref() + .map(|return_type| format!(" returns ({return_type})")) + .unwrap_or_default(), + parameters = function.parameters.join(", "), + )?; + } + writeln!(buffer, "}}")?; } BuiltIn::BuiltInVariable { item } => { - lines.push(format!("{var_def};", var_def = item.definition)); + writeln!(buffer, "{var_def};", var_def = item.definition)?; } } } - lines.push("}".into()); - lines.join("\n") + writeln!(buffer, "}}")?; + Ok(buffer) } diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/built_ins.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/built_ins.rs index 2ff6fbf40..8b688f1e4 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/built_ins.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/built_ins.rs @@ -16,11 +16,11 @@ struct $BuiltIn$Address { struct $BuiltIn$TxType { uint gasprice; address payable origin; - } uint now; $BuiltIn$TxType tx; -}"#### +} +"#### } else if *version < Version::new(0, 8, 0) { r####"contract $$ { function addmod(uint x, uint y, uint k) public returns (uint); @@ -35,11 +35,11 @@ struct $BuiltIn$Address { struct $BuiltIn$TxType { uint gasprice; address payable origin; - } uint now; $BuiltIn$TxType tx; -}"#### +} +"#### } else { r####"contract $$ { function addmod(uint x, uint y, uint k) public returns (uint); @@ -55,10 +55,10 @@ struct $BuiltIn$Address { struct $BuiltIn$TxType { uint gasprice; address payable origin; - } uint now; $BuiltIn$TxType tx; -}"#### +} +"#### } }