pub mod function_stub;
pub use function_stub::*;
use crate::{Composite, ConstDeclaration, Identifier, Indent, Library, Mapping, NodeID, Program, ProgramId};
use indexmap::IndexSet;
use leo_span::{Span, Symbol};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Stub {
FromLeo {
program: Program,
parents: IndexSet<Symbol>, },
FromAleo {
program: AleoProgram,
parents: IndexSet<Symbol>, },
FromLibrary {
library: Library,
parents: IndexSet<Symbol>, },
}
impl Stub {
pub fn explicit_imports(&self) -> Box<dyn Iterator<Item = (Symbol, Span)> + '_> {
match self {
Stub::FromLeo { program, .. } => Box::new(program.imports.iter().map(|(sym, id)| (*sym, id.span()))),
Stub::FromAleo { program, .. } => {
Box::new(program.imports.iter().map(|id| (id.as_symbol(), Span::default())))
}
Stub::FromLibrary { .. } => Box::new(std::iter::empty()),
}
}
pub fn parents(&self) -> &IndexSet<Symbol> {
match self {
Stub::FromLeo { parents, .. } => parents,
Stub::FromAleo { parents, .. } => parents,
Stub::FromLibrary { parents, .. } => parents,
}
}
pub fn add_parent(&mut self, parent: Symbol) {
match self {
Stub::FromLeo { parents, .. } | Stub::FromAleo { parents, .. } | Stub::FromLibrary { parents, .. } => {
parents.insert(parent);
}
}
}
pub fn is_leo_program(&self) -> bool {
matches!(self, Self::FromLeo { .. })
}
pub fn is_aleo_program(&self) -> bool {
matches!(self, Self::FromAleo { .. })
}
pub fn is_library(&self) -> bool {
matches!(self, Self::FromLibrary { .. })
}
}
impl fmt::Display for Stub {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Stub::FromLeo { program, .. } => write!(f, "{program}"),
Stub::FromAleo { program, .. } => write!(f, "{program}"),
Stub::FromLibrary { library, .. } => write!(f, "{library}"),
}
}
}
impl From<Program> for Stub {
fn from(program: Program) -> Self {
Stub::FromLeo { program, parents: IndexSet::new() }
}
}
impl From<AleoProgram> for Stub {
fn from(program: AleoProgram) -> Self {
Stub::FromAleo { program, parents: IndexSet::new() }
}
}
impl From<Library> for Stub {
fn from(library: Library) -> Self {
Stub::FromLibrary { library, parents: IndexSet::new() }
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AleoProgram {
pub imports: Vec<ProgramId>,
pub stub_id: ProgramId,
pub consts: Vec<(Symbol, ConstDeclaration)>,
pub composites: Vec<(Symbol, Composite)>,
pub mappings: Vec<(Symbol, Mapping)>,
pub functions: Vec<(Symbol, FunctionStub)>,
pub span: Span,
}
impl Default for AleoProgram {
fn default() -> Self {
Self {
imports: Vec::new(),
stub_id: ProgramId {
name: Identifier::new(Symbol::intern(""), NodeID::default()),
network: Identifier::new(Symbol::intern(""), NodeID::default()),
},
consts: Vec::new(),
composites: Vec::new(),
mappings: Vec::new(),
functions: Vec::new(),
span: Span::default(),
}
}
}
impl fmt::Display for AleoProgram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "stub {} {{", self.stub_id)?;
for import in self.imports.iter() {
writeln!(f, " import {import};")?;
}
for (_, mapping) in self.mappings.iter() {
writeln!(f, "{};", Indent(mapping))?;
}
for (_, composite) in self.composites.iter() {
writeln!(f, "{}", Indent(composite))?;
}
for (_, function) in self.functions.iter() {
writeln!(f, "{}", Indent(function))?;
}
write!(f, "}}")
}
}