1pub mod function_stub;
20pub use function_stub::*;
21
22use crate::{Composite, ConstDeclaration, Identifier, Indent, Library, 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 FromLibrary {
42 library: Library,
43 parents: IndexSet<Symbol>, },
45}
46
47impl Stub {
48 pub fn explicit_imports(&self) -> Box<dyn Iterator<Item = (Symbol, Span)> + '_> {
51 match self {
52 Stub::FromLeo { program, .. } => Box::new(program.imports.iter().map(|(sym, id)| (*sym, id.span()))),
53 Stub::FromAleo { program, .. } => {
54 Box::new(program.imports.iter().map(|id| (id.as_symbol(), Span::default())))
55 }
56 Stub::FromLibrary { .. } => Box::new(std::iter::empty()),
57 }
58 }
59
60 pub fn parents(&self) -> &IndexSet<Symbol> {
62 match self {
63 Stub::FromLeo { parents, .. } => parents,
64 Stub::FromAleo { parents, .. } => parents,
65 Stub::FromLibrary { parents, .. } => parents,
66 }
67 }
68
69 pub fn add_parent(&mut self, parent: Symbol) {
72 match self {
73 Stub::FromLeo { parents, .. } | Stub::FromAleo { parents, .. } | Stub::FromLibrary { parents, .. } => {
74 parents.insert(parent);
75 }
76 }
77 }
78
79 pub fn is_leo_program(&self) -> bool {
81 matches!(self, Self::FromLeo { .. })
82 }
83
84 pub fn is_aleo_program(&self) -> bool {
86 matches!(self, Self::FromAleo { .. })
87 }
88
89 pub fn is_library(&self) -> bool {
91 matches!(self, Self::FromLibrary { .. })
92 }
93}
94
95impl fmt::Display for Stub {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 match self {
98 Stub::FromLeo { program, .. } => write!(f, "{program}"),
99 Stub::FromAleo { program, .. } => write!(f, "{program}"),
100 Stub::FromLibrary { library, .. } => write!(f, "{library}"),
101 }
102 }
103}
104
105impl From<Program> for Stub {
106 fn from(program: Program) -> Self {
107 Stub::FromLeo { program, parents: IndexSet::new() }
108 }
109}
110
111impl From<AleoProgram> for Stub {
112 fn from(program: AleoProgram) -> Self {
113 Stub::FromAleo { program, parents: IndexSet::new() }
114 }
115}
116
117impl From<Library> for Stub {
118 fn from(library: Library) -> Self {
119 Stub::FromLibrary { library, parents: IndexSet::new() }
120 }
121}
122
123#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
125pub struct AleoProgram {
126 pub imports: Vec<ProgramId>,
128 pub stub_id: ProgramId,
130 pub consts: Vec<(Symbol, ConstDeclaration)>,
132 pub composites: Vec<(Symbol, Composite)>,
134 pub mappings: Vec<(Symbol, Mapping)>,
136 pub functions: Vec<(Symbol, FunctionStub)>,
138 pub span: Span,
140}
141
142impl Default for AleoProgram {
143 fn default() -> Self {
145 Self {
146 imports: Vec::new(),
147 stub_id: ProgramId {
148 name: Identifier::new(Symbol::intern(""), NodeID::default()),
149 network: Identifier::new(Symbol::intern(""), NodeID::default()),
150 },
151 consts: Vec::new(),
152 composites: Vec::new(),
153 mappings: Vec::new(),
154 functions: Vec::new(),
155 span: Span::default(),
156 }
157 }
158}
159
160impl fmt::Display for AleoProgram {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162 writeln!(f, "stub {} {{", self.stub_id)?;
163 for import in self.imports.iter() {
164 writeln!(f, " import {import};")?;
165 }
166 for (_, mapping) in self.mappings.iter() {
167 writeln!(f, "{};", Indent(mapping))?;
168 }
169 for (_, composite) in self.composites.iter() {
170 writeln!(f, "{}", Indent(composite))?;
171 }
172 for (_, function) in self.functions.iter() {
173 writeln!(f, "{}", Indent(function))?;
174 }
175 write!(f, "}}")
176 }
177}