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