use anyhow::{Context, Result};
use splicer::{compose, ComponentInput, ComposeRequest};
const WAT_PROVIDER_A: &str = r#"(component
(import "host:env/dep@0.1.0" (instance $dep
(export "get" (func (result u32)))
))
(alias export $dep "get" (func $f))
(instance $out (export "get" (func $f)))
(export "my:providers/a@0.1.0" (instance $out))
)"#;
const WAT_CONSUMER: &str = r#"(component
(import "my:providers/a@0.1.0" (instance $a
(export "get" (func (result u32)))
))
(alias export $a "get" (func $f))
(instance $out (export "get" (func $f)))
(export "my:consumer/app@0.1.0" (instance $out))
)"#;
fn main() -> Result<()> {
let tmp = tempfile::tempdir().context("create temp dir")?;
let provider_path = tmp.path().join("provider-a.wasm");
let consumer_path = tmp.path().join("consumer.wasm");
let provider_bytes = wat::parse_str(WAT_PROVIDER_A).context("compile provider WAT")?;
let consumer_bytes = wat::parse_str(WAT_CONSUMER).context("compile consumer WAT")?;
std::fs::write(&provider_path, &provider_bytes).context("write provider wasm")?;
std::fs::write(&consumer_path, &consumer_bytes).context("write consumer wasm")?;
println!(
"✓ wrote provider and consumer wasm to {}",
tmp.path().display()
);
let out = compose(ComposeRequest {
components: vec![
ComponentInput {
alias: Some("provider-a".to_string()),
path: provider_path.clone(),
},
ComponentInput {
alias: Some("consumer".to_string()),
path: consumer_path.clone(),
},
],
package_name: "example:composition".to_string(),
})?;
println!(
"✓ splicer::compose produced {} bytes of WAC source, {} dep(s)",
out.wac.len(),
out.wac_deps.len()
);
let composed = out.to_wasm().context("compose to wasm")?;
println!(
"✓ Bundle::to_wasm produced and validated {} bytes",
composed.len()
);
println!();
println!(
"All done — splicer's output became a composed component without leaving the process."
);
Ok(())
}