microcad_lang/syntax/call/
argument_list.rs

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