Skip to content

Commit

Permalink
feature: add ComponentBuilder::load, with_exports to modify
Browse files Browse the repository at this point in the history
`Component`.
  • Loading branch information
greenhat committed Nov 29, 2024
1 parent 2b71558 commit 6ebfeae
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl formatter::PrettyPrint for ComponentImport {
}

/// A component export
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ComponentExport {
/// The module function that is being exported
pub function: FunctionIdent,
Expand Down Expand Up @@ -366,6 +366,17 @@ impl<'a> ComponentBuilder<'a> {
}
}

/// Create a new [ComponentBuilder] from a [Component].
pub fn load(component: Component, diagnostics: &'a DiagnosticsHandler) -> Self {
Self {
modules: component.modules,
imports: component.imports,
exports: component.exports,
entry: None,
diagnostics,
}
}

/// Set the entrypoint for the [Component] being built.
#[inline]
pub fn with_entrypoint(mut self, id: FunctionIdent) -> Self {
Expand All @@ -383,6 +394,15 @@ impl<'a> ComponentBuilder<'a> {
self.add_module(module).map(|_| self)
}

/// Replace the exports of the [Component] being built.
pub fn with_exports(
mut self,
exports: BTreeMap<InterfaceFunctionIdent, ComponentExport>,
) -> Self {
self.exports = exports;
self
}

/// Add `module` to the set of modules to link into the final [Component]
///
/// Returns `Err` if a module with the same name already exists
Expand Down Expand Up @@ -413,14 +433,22 @@ impl<'a> ComponentBuilder<'a> {
}
}

/// Add an import to the [Component] being built. Overwrites any existing import with the same
/// `function_id`.
pub fn add_import(&mut self, function_id: FunctionIdent, import: ComponentImport) {
self.imports.insert(function_id, import);
}

/// Add an export to the [Component] being built. Overwrites any existing export with the same
/// `name`.
pub fn add_export(&mut self, name: InterfaceFunctionIdent, export: ComponentExport) {
self.exports.insert(name, export);
}

pub fn exports(&self) -> &BTreeMap<InterfaceFunctionIdent, ComponentExport> {
&self.exports
}

pub fn build(self) -> Component {
assert!(!self.modules.is_empty(), "Cannot build a component with no modules");
Component {
Expand Down

0 comments on commit 6ebfeae

Please sign in to comment.