summaryrefslogtreecommitdiff
path: root/crates/cli/src/bin.rs
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-04-26 19:44:34 +0200
committerHenry <mail@henrygressmann.de>2026-04-26 19:44:34 +0200
commit463a3a6f56a5ca06cda890fd6fe7f16485ef70a0 (patch)
treebfa657a2de16c04cd36f4b6c61e77b370fcd46fc /crates/cli/src/bin.rs
parent27004a8738c7a3c32ac4fb6966f32647b4c96db7 (diff)
feat: new tinywasm cli
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/cli/src/bin.rs')
-rw-r--r--crates/cli/src/bin.rs119
1 files changed, 5 insertions, 114 deletions
diff --git a/crates/cli/src/bin.rs b/crates/cli/src/bin.rs
index 7e32fed..592fda7 100644
--- a/crates/cli/src/bin.rs
+++ b/crates/cli/src/bin.rs
@@ -1,118 +1,9 @@
-use std::str::FromStr;
-
-use argh::FromArgs;
-use args::WasmArg;
+use clap::Parser;
use eyre::Result;
-use log::{debug, info};
-use tinywasm::{Module, ModuleInstance, types::WasmValue};
-
-use crate::args::to_wasm_args;
-mod args;
-mod util;
-
-#[cfg(feature = "wat")]
-mod wat;
-
-#[derive(FromArgs)]
-/// `TinyWasm` CLI
-struct TinyWasmCli {
- #[argh(subcommand)]
- nested: TinyWasmSubcommand,
-
- /// log level: trace, debug, info, warn, or error
- #[argh(option, short = 'l', default = "\"info\".to_string()")]
- log_level: String,
-}
-
-#[derive(FromArgs)]
-#[argh(subcommand)]
-enum TinyWasmSubcommand {
- Run(Run),
-}
-
-enum Engine {
- Main,
-}
-
-impl FromStr for Engine {
- type Err = String;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- match s {
- "main" => Ok(Self::Main),
- _ => Err(format!("unknown engine: {s}")),
- }
- }
-}
-
-#[derive(FromArgs)]
-/// run a wasm file
-#[argh(subcommand, name = "run")]
-struct Run {
- /// wasm file to run
- #[argh(positional)]
- wasm_file: String,
-
- /// exported function to run; omit to only instantiate and run start
- #[argh(option, short = 'f')]
- func: Option<String>,
-
- /// arguments passed to the function in type:value form
- #[argh(option, short = 'a')]
- args: Vec<WasmArg>,
-
- /// engine to use (currently only `main`)
- #[argh(option, short = 'e', default = "Engine::Main")]
- engine: Engine,
-}
+use tinywasm_cli::{Cli, run_cli};
fn main() -> Result<()> {
- let args: TinyWasmCli = argh::from_env();
- let level = match args.log_level.as_str() {
- "trace" => log::LevelFilter::Trace,
- "debug" => log::LevelFilter::Debug,
- "warn" => log::LevelFilter::Warn,
- "error" => log::LevelFilter::Error,
- "info" => log::LevelFilter::Info,
- other => return Err(eyre::eyre!("invalid log level `{other}`; expected trace, debug, info, warn, or error")),
- };
-
- pretty_env_logger::formatted_builder().filter_level(level).init();
- let cwd = std::env::current_dir()?;
-
- match args.nested {
- TinyWasmSubcommand::Run(Run { wasm_file, engine, args, func }) => {
- debug!("args: {args:?}");
-
- let path = cwd.join(&wasm_file);
- let module = match wasm_file.ends_with(".wat") {
- #[cfg(feature = "wat")]
- true => {
- let wat = std::fs::read_to_string(path)?;
- let wasm = wat::wat2wasm(&wat);
- tinywasm::parse_bytes(&wasm)?
- }
- #[cfg(not(feature = "wat"))]
- true => return Err(eyre::eyre!("wat support is not enabled in this build")),
- false => tinywasm::parse_file(path)?,
- };
-
- match engine {
- Engine::Main => run(module, func, &to_wasm_args(args)),
- }
- }
- }
-}
-
-fn run(module: Module, func: Option<String>, args: &[WasmValue]) -> Result<()> {
- let mut store = tinywasm::Store::default();
- let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
-
- if let Some(func) = func {
- let func = instance.func_untyped(&store, &func)?;
- let res = func.call(&mut store, args)?;
- info!("{res:?}");
- }
-
- Ok(())
+ let cli = Cli::parse();
+ pretty_env_logger::formatted_builder().filter_level(cli.log_level.into()).init();
+ run_cli(cli)
}