use crate::{
Composite,
ConstDeclaration,
Constructor,
Function,
Indent,
Interface,
Mapping,
ProgramId,
StorageVariable,
Type,
};
use leo_span::{Span, Symbol};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProgramScope {
pub program_id: ProgramId,
pub parents: Vec<(Span, Type)>,
pub consts: Vec<(Symbol, ConstDeclaration)>,
pub composites: Vec<(Symbol, Composite)>,
pub mappings: Vec<(Symbol, Mapping)>,
pub storage_variables: Vec<(Symbol, StorageVariable)>,
pub functions: Vec<(Symbol, Function)>,
pub interfaces: Vec<(Symbol, Interface)>,
pub constructor: Option<Constructor>,
pub span: Span,
}
impl fmt::Display for ProgramScope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "program {} {{", self.program_id)?;
for (_, const_decl) in self.consts.iter() {
writeln!(f, "{};", Indent(const_decl))?;
}
if let Some(constructor) = &self.constructor {
writeln!(f, "{}", Indent(constructor))?;
}
for (_, composite_) in self.composites.iter() {
writeln!(f, "{}", Indent(composite_))?;
}
for (_, mapping) in self.mappings.iter() {
writeln!(f, "{};", Indent(mapping))?;
}
for (_, function) in self.functions.iter().filter(|f| f.1.variant.is_entry()) {
writeln!(f, "{}", Indent(function))?;
}
writeln!(f, "}}")?;
for (_, function) in self.functions.iter().filter(|f| !f.1.variant.is_entry()) {
writeln!(f, "{}", Indent(function))?;
}
Ok(())
}
}