1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Builder pattern to build up a context
use std::rc::Rc;
use microcad_lang::{builtin::*, eval::*, resolve::*, syntax::*};
/// Context builder.
pub struct ContextBuilder {
context: Context,
}
impl ContextBuilder {
/// Create new context.
pub fn new(
root: Identifier,
symbols: SymbolMap,
sources: Sources,
output: Box<dyn Output>,
) -> Self {
Self {
context: Context::new(root, symbols, sources, output),
}
}
/// Create a new context from a source file and capture output (see [`Output`]).
///
/// # Arguments
/// - `root`: Resolved root source file.
/// - `builtin`: The builtin library.
/// - `search_paths`: Paths to search for external libraries (e.g. the standard library).
pub fn from_source_captured(
root: Rc<SourceFile>,
search_paths: &[std::path::PathBuf],
) -> ResolveResult<Self> {
let root_id = root.id();
let sources = Sources::load(root, search_paths)?;
let mut symbols = sources.resolve()?;
symbols.add_node(crate::builtin_module());
Ok(
Self::new(root_id, symbols, sources, Box::new(Capture::new()))
.importers(crate::builtin_importers())
.exporters(crate::builtin_exporters()),
)
}
/// Set importers to context.
pub fn importers(mut self, importers: ImporterRegistry) -> Self {
self.context.set_importers(importers);
self
}
/// Set exporters to context.
pub fn exporters(mut self, exporters: ExporterRegistry) -> Self {
self.context.set_exporters(exporters);
self
}
/// Build context.
pub fn build(self) -> Context {
self.context
}
}