-
Notifications
You must be signed in to change notification settings - Fork 30
Librarify the plug command #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ | |
|
|
||
| pub(crate) mod encoding; | ||
| mod graph; | ||
| mod plug; | ||
|
|
||
| pub use graph::*; | ||
| pub use plug::*; | ||
| pub use wac_types as types; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use crate::{types::SubtypeChecker, CompositionGraph, PackageId}; | ||
| use thiserror::Error; | ||
|
|
||
| /// Represents an error that can occur when plugging components together. | ||
| #[derive(Debug, Error)] | ||
| pub enum PlugError { | ||
| /// The socket component had no matching imports for the plugs that were provided | ||
| #[error("the socket component had no matching imports for the plugs that were provided")] | ||
| NoPlugHappened, | ||
| /// An error occurred when building the composition graph | ||
| #[error("an error occurred when building the composition graph")] | ||
| GraphError { | ||
| /// The underlying graph error | ||
| #[source] | ||
| source: anyhow::Error, | ||
| }, | ||
| } | ||
|
|
||
| /// Take the exports of the plug components and plug them into the socket component. | ||
| /// | ||
| /// Note that the `PackageId`s in `plugs` and `socket` must be registered with the `graph` | ||
| /// before calling this function. | ||
| pub fn plug( | ||
| graph: &mut CompositionGraph, | ||
| plugs: Vec<PackageId>, | ||
| socket: PackageId, | ||
| ) -> Result<(), PlugError> { | ||
| let socket_instantiation = graph.instantiate(socket); | ||
|
|
||
| for plug in plugs { | ||
| let mut plug_exports = Vec::new(); | ||
| let mut cache = Default::default(); | ||
| let mut checker = SubtypeChecker::new(&mut cache); | ||
| for (name, plug_ty) in &graph.types()[graph[plug].ty()].exports { | ||
| if let Some(socket_ty) = graph.types()[graph[socket].ty()].imports.get(name) { | ||
| if checker | ||
| .is_subtype(*plug_ty, graph.types(), *socket_ty, graph.types()) | ||
| .is_ok() | ||
| { | ||
| plug_exports.push(name.clone()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Instantiate the plug component | ||
| let mut plug_instantiation = None; | ||
| for plug_name in plug_exports { | ||
| log::debug!("using export `{plug_name}` for plug"); | ||
| let plug_instantiation = | ||
| *plug_instantiation.get_or_insert_with(|| graph.instantiate(plug)); | ||
| let export = graph | ||
| .alias_instance_export(plug_instantiation, &plug_name) | ||
| .map_err(|err| PlugError::GraphError { source: err.into() })?; | ||
| graph | ||
| .set_instantiation_argument(socket_instantiation, &plug_name, export) | ||
| .map_err(|err| PlugError::GraphError { source: err.into() })?; | ||
| } | ||
| } | ||
|
|
||
| // Check we've actually done any plugging. | ||
| if graph | ||
| .get_instantiation_arguments(socket_instantiation) | ||
| .next() | ||
| .is_none() | ||
| { | ||
| return Err(PlugError::NoPlugHappened); | ||
| } | ||
|
|
||
| // Export all exports from the socket component. | ||
| for name in graph.types()[graph[socket].ty()] | ||
| .exports | ||
| .keys() | ||
| .cloned() | ||
| .collect::<Vec<_>>() | ||
| { | ||
| let export = graph | ||
| .alias_instance_export(socket_instantiation, &name) | ||
| .map_err(|err| PlugError::GraphError { source: err.into() })?; | ||
|
|
||
| graph | ||
| .export(export, &name) | ||
| .map_err(|err| PlugError::GraphError { source: err.into() })?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.