microcad_lang/syntax/expression/
tuple_expression.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Tuple expression.
5
6use crate::{src_ref::*, syntax::*};
7
8/// Tuple expression, e.g. `(x=1+2,4,z=9)`.
9#[derive(Clone, Debug, Default)]
10pub struct TupleExpression {
11    /// List of tuple members.
12    pub args: ArgumentList,
13    /// Source code reference
14    pub src_ref: SrcRef,
15}
16
17impl SrcReferrer for TupleExpression {
18    fn src_ref(&self) -> crate::src_ref::SrcRef {
19        self.src_ref.clone()
20    }
21}
22
23impl std::fmt::Display for TupleExpression {
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        write!(
26            f,
27            "({})",
28            self.args
29                .iter()
30                .map(|arg| if let Some(name) = &arg.id {
31                    format!("{} = {}", &name, arg.value)
32                } else {
33                    arg.to_string()
34                })
35                .collect::<Vec<String>>()
36                .join(", ")
37        )?;
38        Ok(())
39    }
40}
41
42impl TreeDisplay for TupleExpression {
43    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
44        writeln!(f, "{:depth$}TupleExpression:", "")?;
45        depth.indent();
46        self.args.tree_print(f, depth)
47    }
48}