Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Improve the Display impl for ProgramCore #2532

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions synthesizer/program/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,56 +143,55 @@ impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Deb
}
}

#[allow(clippy::format_push_string)]
impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Display
for ProgramCore<N, Instruction, Command>
{
/// Prints the program as a string.
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// Initialize a string for the program.
let mut program = String::new();

if !self.imports.is_empty() {
// Print the imports.
for import in self.imports.values() {
program.push_str(&format!("{import}\n"));
writeln!(f, "{import}")?;
}

// Print a newline.
program.push('\n');
writeln!(f)?;
}

// Print the program name.
program += &format!("{} {};\n\n", Self::type_name(), self.id);
write!(f, "{} {};\n\n", Self::type_name(), self.id)?;

for (identifier, definition) in self.identifiers.iter() {
let mut identifier_iter = self.identifiers.iter().peekable();
while let Some((identifier, definition)) = identifier_iter.next() {
match definition {
ProgramDefinition::Mapping => match self.mappings.get(identifier) {
Some(mapping) => program.push_str(&format!("{mapping}\n\n")),
Some(mapping) => writeln!(f, "{mapping}")?,
None => return Err(fmt::Error),
},
ProgramDefinition::Struct => match self.structs.get(identifier) {
Some(struct_) => program.push_str(&format!("{struct_}\n\n")),
Some(struct_) => writeln!(f, "{struct_}")?,
None => return Err(fmt::Error),
},
ProgramDefinition::Record => match self.records.get(identifier) {
Some(record) => program.push_str(&format!("{record}\n\n")),
Some(record) => writeln!(f, "{record}")?,
None => return Err(fmt::Error),
},
ProgramDefinition::Closure => match self.closures.get(identifier) {
Some(closure) => program.push_str(&format!("{closure}\n\n")),
Some(closure) => writeln!(f, "{closure}")?,
None => return Err(fmt::Error),
},
ProgramDefinition::Function => match self.functions.get(identifier) {
Some(function) => program.push_str(&format!("{function}\n\n")),
Some(function) => writeln!(f, "{function}")?,
None => return Err(fmt::Error),
},
}
// Omit the last newline.
if identifier_iter.peek().is_some() {
writeln!(f)?;
vicsn marked this conversation as resolved.
Show resolved Hide resolved
}
}
// Remove the last newline.
program.pop();

write!(f, "{program}")
Ok(())
}
}

Expand Down