leo_ast/expressions/
struct_init.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
17use super::*;
18
19use itertools::Itertools as _;
20
21/// An initializer for a single field / variable of a struct initializer expression.
22/// That is, in `Foo { bar: 42, baz }`, this is either `bar: 42`, or `baz`.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct StructVariableInitializer {
25    /// The name of the field / variable to be initialized.
26    pub identifier: Identifier,
27    /// The expression to initialize the field with.
28    /// When `None`, a binding, in scope, with the name will be used instead.
29    pub expression: Option<Expression>,
30    /// The span of the node.
31    pub span: Span,
32    /// The ID of the node.
33    pub id: NodeID,
34}
35
36crate::simple_node_impl!(StructVariableInitializer);
37
38impl fmt::Display for StructVariableInitializer {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        if let Some(expr) = &self.expression {
41            write!(f, "{}: {expr}", self.identifier)
42        } else {
43            write!(f, "{}", self.identifier)
44        }
45    }
46}
47
48/// A struct initialization expression, e.g., `Foo { bar: 42, baz }`.
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct StructExpression {
51    /// The name of the structure type to initialize.
52    pub name: Identifier,
53    /// Initializer expressions for each of the fields in the struct.
54    ///
55    /// N.B. Any functions or member constants in the struct definition
56    /// are excluded from this list.
57    pub members: Vec<StructVariableInitializer>,
58    /// A span from `name` to `}`.
59    pub span: Span,
60    /// The ID of the node.
61    pub id: NodeID,
62}
63
64impl fmt::Display for StructExpression {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        write!(f, "{} {{", self.name)?;
67        if !self.members.is_empty() {
68            write!(f, " ")?;
69        }
70        write!(f, "{}", self.members.iter().format(", "))?;
71        if !self.members.is_empty() {
72            write!(f, " ")?;
73        }
74        write!(f, "}}")
75    }
76}
77
78impl From<StructExpression> for Expression {
79    fn from(value: StructExpression) -> Self {
80        Expression::Struct(value)
81    }
82}
83
84crate::simple_node_impl!(StructExpression);