microcad_lang/syntax/call/
argument.rs1use crate::{ord_map::*, src_ref::*, syntax::*};
7
8#[derive(Clone, Debug)]
10pub struct Argument {
11 pub id: Option<Identifier>,
13 pub value: Expression,
15 pub src_ref: SrcRef,
17}
18
19impl Argument {
20 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}