microcad_lang/syntax/module/
module_definition.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Module definition syntax element.
5
6use crate::{rc::*, src_ref::*, syntax::*};
7
8/// Module definition.
9#[derive(Debug, Clone)]
10pub struct ModuleDefinition {
11    /// Visibility of the module.
12    pub visibility: Visibility,
13    /// Name of the module.
14    pub id: Identifier,
15    /// Module body.
16    pub body: Body,
17    /// Source code reference.
18    pub src_ref: SrcRef,
19}
20
21impl ModuleDefinition {
22    /// Create a new module definition.
23    pub fn new(visibility: Visibility, id: Identifier) -> Rc<Self> {
24        Rc::new(Self {
25            visibility,
26            id,
27            body: Body::default(),
28            src_ref: SrcRef(None),
29        })
30    }
31}
32
33impl SrcReferrer for ModuleDefinition {
34    fn src_ref(&self) -> SrcRef {
35        self.src_ref.clone()
36    }
37}
38
39impl TreeDisplay for ModuleDefinition {
40    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
41        writeln!(f, "{:depth$}ModuleDefinition '{}':", "", self.id)?;
42        depth.indent();
43        self.body.tree_print(f, depth)
44    }
45}