1pub mod function_stub;
20pub use function_stub::*;
21
22use crate::{Composite, ConstDeclaration, Identifier, Indent, Mapping, NodeID, Program, ProgramId};
23use indexmap::IndexSet;
24use leo_span::{Span, Symbol};
25use serde::{Deserialize, Serialize};
26use std::fmt;
27
28#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
29pub enum Stub {
30 FromLeo {
32 program: Program,
33 parents: IndexSet<Symbol>, },
35 FromAleo {
37 program: AleoProgram,
38 parents: IndexSet<Symbol>, },
40}
41
42impl Stub {
43 pub fn imports(&self) -> Box<dyn Iterator<Item = &Symbol> + '_> {
45 match self {
46 Stub::FromLeo { program, .. } => Box::new(program.imports.keys()),
47 Stub::FromAleo { program, .. } => Box::new(program.imports.iter().map(|id| &id.name.name)),
48 }
49 }
50
51 pub fn add_parent(&mut self, parent: Symbol) {
54 match self {
55 Stub::FromLeo { parents, .. } | Stub::FromAleo { parents, .. } => {
56 parents.insert(parent);
57 }
58 }
59 }
60}
61
62impl fmt::Display for Stub {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 match self {
65 Stub::FromLeo { program, .. } => write!(f, "{program}"),
66 Stub::FromAleo { program, .. } => write!(f, "{program}"),
67 }
68 }
69}
70
71impl From<Program> for Stub {
72 fn from(program: Program) -> Self {
73 Stub::FromLeo { program, parents: IndexSet::new() }
74 }
75}
76
77impl From<AleoProgram> for Stub {
78 fn from(program: AleoProgram) -> Self {
79 Stub::FromAleo { program, parents: IndexSet::new() }
80 }
81}
82
83#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
85pub struct AleoProgram {
86 pub imports: Vec<ProgramId>,
88 pub stub_id: ProgramId,
90 pub consts: Vec<(Symbol, ConstDeclaration)>,
92 pub composites: Vec<(Symbol, Composite)>,
94 pub mappings: Vec<(Symbol, Mapping)>,
96 pub functions: Vec<(Symbol, FunctionStub)>,
98 pub span: Span,
100}
101
102impl Default for AleoProgram {
103 fn default() -> Self {
105 Self {
106 imports: Vec::new(),
107 stub_id: ProgramId {
108 name: Identifier::new(Symbol::intern(""), NodeID::default()),
109 network: Identifier::new(Symbol::intern(""), NodeID::default()),
110 },
111 consts: Vec::new(),
112 composites: Vec::new(),
113 mappings: Vec::new(),
114 functions: Vec::new(),
115 span: Span::default(),
116 }
117 }
118}
119
120impl fmt::Display for AleoProgram {
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122 writeln!(f, "stub {} {{", self.stub_id)?;
123 for import in self.imports.iter() {
124 writeln!(f, " import {import};")?;
125 }
126 for (_, mapping) in self.mappings.iter() {
127 writeln!(f, "{};", Indent(mapping))?;
128 }
129 for (_, composite) in self.composites.iter() {
130 writeln!(f, "{}", Indent(composite))?;
131 }
132 for (_, function) in self.functions.iter() {
133 writeln!(f, "{}", Indent(function))?;
134 }
135 write!(f, "}}")
136 }
137}