microcad_lang/syntax/call/
argument.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! A single argument
5
6use crate::{ord_map::*, src_ref::*, syntax::*};
7
8/// Argument in a [`Call`].
9#[derive(Clone, Debug)]
10pub struct Argument {
11    /// Name of the argument
12    pub id: Option<Identifier>,
13    /// Value of the argument
14    pub value: Expression,
15    /// Source code reference
16    pub src_ref: SrcRef,
17}
18
19impl Argument {
20    /// Returns the name, if self.name is some. If self.name is None, try to extract the name from the expression
21    pub fn derived_name(&self) -> Option<Identifier> {
22        match &self.id {
23            Some(name) => Some(name.clone()),
24            None => self.value.single_identifier().cloned(),
25        }
26    }
27}
28
29impl SrcReferrer for Argument {
30    fn src_ref(&self) -> SrcRef {
31        self.src_ref.clone()
32    }
33}
34
35impl OrdMapValue<Identifier> for Argument {
36    fn key(&self) -> Option<Identifier> {
37        self.id.clone()
38    }
39}
40
41impl std::fmt::Display for Argument {
42    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43        match self.id {
44            Some(ref id) => write!(f, "{id:?} = {}", self.value),
45            None => write!(f, "{}", self.value),
46        }
47    }
48}
49
50impl TreeDisplay for Argument {
51    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
52        match self.id {
53            Some(ref id) => writeln!(f, "{:depth$}Argument '{id:?}':", "")?,
54            None => writeln!(f, "{:depth$}Argument:", "")?,
55        };
56        depth.indent();
57        self.value.tree_print(f, depth)
58    }
59}