Skip to main content

microcad_lang/lower/ir/module/
module_definition.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Module definition syntax element.
5
6use std::rc::Rc;
7
8use microcad_lang_base::{Identifier, SrcRef, SrcReferrer};
9use microcad_lang_proc_macros::Identifiable;
10
11use crate::lower::ir;
12
13/// Module definition.
14#[derive(Clone, Debug, Default, Identifiable)]
15pub struct ModuleDefinition {
16    /// SrcRef of the `mod` keyword
17    pub keyword_ref: SrcRef,
18    /// Outer documentation.
19    pub doc: ir::DocBlock,
20    /// Visibility of the module.
21    pub visibility: ir::Visibility,
22    /// Name of the module.
23    pub(crate) id: ir::Identifier,
24    /// Module body. ('None' if file module)
25    pub body: Option<ir::Body>,
26}
27
28impl ModuleDefinition {
29    /// Create a new module definition.
30    pub fn new(visibility: ir::Visibility, id: Identifier) -> Rc<Self> {
31        Rc::new(Self {
32            visibility,
33            id,
34            ..Default::default()
35        })
36    }
37}
38
39impl SrcReferrer for ModuleDefinition {
40    fn src_ref(&self) -> SrcRef {
41        self.id.src_ref()
42    }
43}
44
45impl std::fmt::Display for ModuleDefinition {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(
48            f,
49            "{visibility}mod {id}",
50            id = self.id,
51            visibility = self.visibility,
52        )
53    }
54}