Skip to main content

microcad_lang/builtin/
module_builder.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Builder pattern to build built-in modules.
5
6use crate::{
7    Identifier,
8    builtin::{BuiltinConstant, BuiltinWorkbenchDefinition},
9    lower::ir,
10    symbol::{Symbol, SymbolDef},
11    value::Value,
12};
13
14/// Builder pattern to build built-in modules.
15pub struct ModuleBuilder {
16    // Symbol to build.
17    module: Symbol,
18}
19
20impl ModuleBuilder {
21    /// Create new module symbol with a name.
22    pub fn new(id: impl Into<Identifier>) -> Self {
23        Self {
24            module: Symbol::new(
25                SymbolDef::Module(ir::ModuleDefinition::new(ir::Visibility::Public, id.into())),
26                None,
27            ),
28        }
29    }
30
31    /// Add a symbol to the module.
32    pub fn symbol(self, symbol: Symbol) -> Self {
33        Symbol::add_child(&self.module, symbol);
34        self
35    }
36
37    /// Add the symbol from a built-in workbench definition.
38    pub fn builtin<T: BuiltinWorkbenchDefinition>(self) -> Self {
39        self.symbol(T::symbol())
40    }
41
42    /// Add a public constant.
43    pub fn pub_const(self, id: &str, value: impl Into<Value>) -> Self {
44        let value = value.into();
45        self.symbol(Symbol::new_builtin(BuiltinConstant {
46            id: Identifier::no_ref(id),
47            value,
48            doc: None,
49        }))
50    }
51
52    /// Return our module symbol.
53    pub fn build(self) -> Symbol {
54        self.module
55    }
56}