microcad_lang/syntax/module/
module_definition.rs1use std::rc::Rc;
7
8use microcad_lang_base::{SrcRef, SrcReferrer, TreeDisplay, TreeState};
9
10use crate::syntax::*;
11
12#[derive(Clone, Default)]
14pub struct ModuleDefinition {
15 pub keyword_ref: SrcRef,
17 pub doc: Option<DocBlock>,
19 pub visibility: Visibility,
21 pub(crate) id: Identifier,
23 pub body: Option<Body>,
25}
26
27impl ModuleDefinition {
28 pub fn new(visibility: Visibility, id: Identifier) -> Rc<Self> {
30 Rc::new(Self {
31 visibility,
32 id,
33 ..Default::default()
34 })
35 }
36}
37
38impl Identifiable for ModuleDefinition {
39 fn id_ref(&self) -> &Identifier {
40 &self.id
41 }
42}
43
44impl SrcReferrer for ModuleDefinition {
45 fn src_ref(&self) -> SrcRef {
46 self.id.src_ref()
47 }
48}
49
50impl TreeDisplay for ModuleDefinition {
51 fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
52 if let Some(body) = &self.body {
53 writeln!(
54 f,
55 "{:depth$}ModuleDefinition {visibility}'{id}':",
56 "",
57 id = self.id,
58 visibility = self.visibility,
59 )?;
60 depth.indent();
61 if let Some(doc) = &self.doc {
62 doc.tree_print(f, depth)?;
63 }
64 body.tree_print(f, depth)
65 } else {
66 writeln!(
67 f,
68 "{:depth$}ModuleDefinition {visibility}'{id}' (external)",
69 "",
70 id = self.id,
71 visibility = self.visibility,
72 )?;
73 if let Some(doc) = &self.doc {
74 doc.tree_print(f, depth)?;
75 }
76 Ok(())
77 }
78 }
79}
80
81impl std::fmt::Display for ModuleDefinition {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(
84 f,
85 "{visibility}mod {id}",
86 id = self.id,
87 visibility = self.visibility,
88 )
89 }
90}
91
92impl std::fmt::Debug for ModuleDefinition {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 write!(
95 f,
96 "{visibility}mod {id:?}",
97 id = self.id,
98 visibility = self.visibility,
99 )
100 }
101}