Skip to content

Commit

Permalink
Use writeln! to generate the built-ins contents
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 3, 2024
1 parent fbec218 commit 7e05529
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
80 changes: 39 additions & 41 deletions crates/solidity/inputs/language/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String, Error> {
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::<Vec<_>>()
.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::<Vec<_>>()
.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)
}

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

0 comments on commit 7e05529

Please sign in to comment.