use anyhow::{Context, Result};
use clap::Parser;
use wasm_tools::wit::WitResolve;
use wit_component::{StringEncoding, embed_component_metadata};
#[derive(Parser)]
pub struct Opts {
#[clap(flatten)]
resolve: WitResolve,
#[clap(flatten)]
io: wasm_tools::InputOutput,
#[clap(long, value_name = "ENCODING")]
encoding: Option<StringEncoding>,
#[clap(short, long)]
world: Option<String>,
#[clap(long)]
validate: bool,
#[clap(long, short = 't')]
wat: bool,
#[clap(flatten)]
dylib_opts: wit_dylib::DylibOpts,
}
impl Opts {
pub fn general_opts(&self) -> &wasm_tools::GeneralOpts {
self.io.general_opts()
}
pub fn run(mut self) -> Result<()> {
let (resolve, pkg_id) = self.resolve.load()?;
let world = resolve.select_world(&[pkg_id], self.world.as_deref())?;
let mut wasm = wit_dylib::create(&resolve, world, Some(&mut self.dylib_opts));
self.dylib_opts.async_.ensure_all_used()?;
embed_component_metadata(
&mut wasm,
&resolve,
world,
self.encoding.unwrap_or(StringEncoding::UTF8),
)?;
if self.validate {
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&wasm)
.context("generated adapter was not valid")?;
}
self.io.output_wasm(&wasm, self.wat)?;
Ok(())
}
}