Skip to main content

microcad_lang/syntax/call/
argument_list.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! List of arguments syntax entities.
5
6use crate::{ord_map::*, syntax::*};
7use derive_more::{Deref, DerefMut};
8use microcad_lang_base::{Refer, SrcRef, SrcReferrer, TreeDisplay, TreeState};
9
10/// *Ordered map* of arguments in a [`Call`].
11#[derive(Clone, Default, Deref, DerefMut, PartialEq)]
12pub struct ArgumentList(pub Refer<OrdMap<Identifier, Argument>>);
13
14impl SrcReferrer for ArgumentList {
15    fn src_ref(&self) -> SrcRef {
16        self.0.src_ref()
17    }
18}
19
20impl std::fmt::Display for ArgumentList {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        write!(f, "{}", {
23            let mut v = self
24                .0
25                .value
26                .iter()
27                .map(|p| p.to_string())
28                .collect::<Vec<_>>();
29            v.sort();
30            v.join(", ")
31        })
32    }
33}
34
35impl std::fmt::Debug for ArgumentList {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{}", {
38            let mut v = self
39                .0
40                .value
41                .iter()
42                .map(|p| format!("{p:?}"))
43                .collect::<Vec<_>>();
44            v.sort();
45            v.join(", ")
46        })
47    }
48}
49
50impl TreeDisplay for ArgumentList {
51    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
52        writeln!(f, "{:depth$}ArgumentList:", "")?;
53        depth.indent();
54        self.0.value.iter().try_for_each(|p| p.tree_print(f, depth))
55    }
56}
57
58impl std::ops::Index<&Identifier> for ArgumentList {
59    type Output = Argument;
60
61    fn index(&self, name: &Identifier) -> &Self::Output {
62        self.0.get(name).expect("key not found")
63    }
64}
65
66impl std::ops::Index<usize> for ArgumentList {
67    type Output = Argument;
68
69    fn index(&self, idx: usize) -> &Self::Output {
70        &self.0.value[idx]
71    }
72}