use camino::{Utf8Path, Utf8PathBuf};
use clap::{Parser, Subcommand, ValueEnum};
use std::str::FromStr;
use wasm_rquickjs::{
DependencySpec, EmbeddingMode, GenerationTarget, JsModuleSpec, NativeModuleSpec,
};
#[derive(ValueEnum, Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum WasiTarget {
#[default]
#[value(name = "wasi-p2")]
WasiP2,
#[value(name = "wasi-p3")]
WasiP3,
}
impl From<WasiTarget> for GenerationTarget {
fn from(value: WasiTarget) -> Self {
match value {
WasiTarget::WasiP2 => GenerationTarget::WasiP2,
WasiTarget::WasiP3 => GenerationTarget::WasiP3,
}
}
}
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
GenerateWrapperCrate {
#[arg(long, conflicts_with = "js_modules")]
js: Option<Utf8PathBuf>,
#[arg(long, conflicts_with = "js")]
js_modules: Vec<JsModuleSpecArg>,
#[arg(long)]
wit: Utf8PathBuf,
#[arg(long)]
output: Utf8PathBuf,
#[arg(long)]
world: Option<String>,
#[arg(long, value_enum, default_value_t = WasiTarget::WasiP2)]
target: WasiTarget,
#[arg(long)]
native_module: Vec<NativeModuleSpecArg>,
#[arg(long)]
dependency: Vec<DependencySpecArg>,
},
GenerateDTS {
#[arg(long)]
wit: Utf8PathBuf,
#[arg(long)]
output: Utf8PathBuf,
#[arg(long)]
world: Option<String>,
#[arg(long, value_enum, default_value_t = WasiTarget::WasiP2)]
target: WasiTarget,
},
Optimize {
#[arg(long)]
input: Utf8PathBuf,
#[arg(long)]
output: Utf8PathBuf,
#[arg(long, default_value = "wizer-initialize")]
init_func: String,
},
InjectJs {
#[arg(long)]
input: Utf8PathBuf,
#[arg(long)]
output: Utf8PathBuf,
#[arg(long, required = true)]
js: Vec<Utf8PathBuf>,
},
}
#[derive(Debug, Clone)]
pub struct NativeModuleSpecArg(pub NativeModuleSpec);
impl FromStr for NativeModuleSpecArg {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (name, module_def) = s
.split_once('=')
.ok_or_else(|| format!("Invalid native module spec: {s}"))?;
Ok(NativeModuleSpecArg(NativeModuleSpec {
name: name.to_string(),
module_def: module_def.to_string(),
}))
}
}
#[derive(Debug, Clone)]
pub struct DependencySpecArg(pub DependencySpec);
impl FromStr for DependencySpecArg {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (key, value) = s
.split_once('=')
.ok_or_else(|| format!("Invalid dependency spec: {s}"))?;
Ok(DependencySpecArg(DependencySpec {
key: key.trim().to_string(),
value: value.trim().to_string(),
}))
}
}
#[derive(Debug, Clone)]
pub struct JsModuleSpecArg {
pub name: String,
pub mode: EmbeddingMode,
}
impl From<JsModuleSpecArg> for JsModuleSpec {
fn from(value: JsModuleSpecArg) -> Self {
JsModuleSpec {
name: value.name,
mode: value.mode,
}
}
}
impl FromStr for JsModuleSpecArg {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.splitn(2, '=').collect();
if parts.len() != 2 {
return Err(format!("Invalid JS module spec: {s}"));
}
let name = parts[0].to_string();
let mode = match parts[1] {
"@composition" => EmbeddingMode::Composition,
"@slot" => EmbeddingMode::BinarySlot,
path => EmbeddingMode::EmbedFile(Utf8Path::new(path).to_path_buf()),
};
Ok(JsModuleSpecArg { name, mode })
}
}