leo_ast/stub/
mod.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! A stub contains function templates as well as definitions for mappings, structs, records, and constants.
18
19pub mod function_stub;
20pub use function_stub::*;
21
22use crate::{Composite, ConstDeclaration, Identifier, Indent, Mapping, NodeID, ProgramId};
23use leo_span::{Span, Symbol};
24use serde::{Deserialize, Serialize};
25use std::fmt;
26
27/// Stores the Leo stub abstract syntax tree.
28#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
29pub struct Stub {
30    /// A vector of imported programs.
31    pub imports: Vec<ProgramId>,
32    /// The stub id
33    pub stub_id: ProgramId,
34    /// A vector of const definitions.
35    pub consts: Vec<(Symbol, ConstDeclaration)>,
36    /// A vector of struct definitions.
37    pub structs: Vec<(Symbol, Composite)>,
38    /// A vector of mapping definitions.
39    pub mappings: Vec<(Symbol, Mapping)>,
40    /// A vector of function stub definitions.
41    pub functions: Vec<(Symbol, FunctionStub)>,
42    /// The span associated with the stub.
43    pub span: Span,
44}
45
46impl Default for Stub {
47    /// Constructs an empty program stub
48    fn default() -> Self {
49        Self {
50            imports: Vec::new(),
51            stub_id: ProgramId {
52                name: Identifier::new(Symbol::intern(""), NodeID::default()),
53                network: Identifier::new(Symbol::intern(""), NodeID::default()),
54            },
55            consts: Vec::new(),
56            structs: Vec::new(),
57            mappings: Vec::new(),
58            functions: Vec::new(),
59            span: Span::default(),
60        }
61    }
62}
63
64impl fmt::Display for Stub {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        writeln!(f, "stub {} {{", self.stub_id)?;
67        for import in self.imports.iter() {
68            writeln!(f, "    import {import};")?;
69        }
70        for (_, mapping) in self.mappings.iter() {
71            writeln!(f, "{};", Indent(mapping))?;
72        }
73        for (_, struct_) in self.structs.iter() {
74            writeln!(f, "{}", Indent(struct_))?;
75        }
76        for (_, function) in self.functions.iter() {
77            writeln!(f, "{}", Indent(function))?;
78        }
79        write!(f, "}}")
80    }
81}