From 7d167c2fadb370a33dd65b387dbab9d053f103a9 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Wed, 6 Dec 2023 14:55:53 +0100 Subject: feat: cli now has options to specify arguments and function to run Signed-off-by: Henry Gressmann --- crates/cli/src/args.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 crates/cli/src/args.rs (limited to 'crates/cli/src/args.rs') diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs new file mode 100644 index 0000000..4023be6 --- /dev/null +++ b/crates/cli/src/args.rs @@ -0,0 +1,48 @@ +use std::str::FromStr; +use tinywasm::WasmValue; + +#[derive(Debug)] +pub struct WasmArg(WasmValue); + +pub fn to_wasm_args(args: Vec) -> Vec { + args.into_iter().map(|a| a.into()).collect() +} + +impl From for WasmValue { + fn from(value: WasmArg) -> Self { + value.0 + } +} + +impl FromStr for WasmArg { + type Err = String; + fn from_str(s: &str) -> std::prelude::v1::Result { + let [ty, val]: [&str; 2] = s + .split(':') + .collect::>() + .try_into() + .map_err(|e| format!("invalid arguments: {:?}", e))?; + + let arg: WasmValue = match ty { + "i32" => val + .parse::() + .map_err(|e| format!("invalid argument value for i32: {e:?}"))? + .into(), + "i64" => val + .parse::() + .map_err(|e| format!("invalid argument value for i64: {e:?}"))? + .into(), + "f32" => val + .parse::() + .map_err(|e| format!("invalid argument value for f32: {e:?}"))? + .into(), + "f64" => val + .parse::() + .map_err(|e| format!("invalid argument value for f64: {e:?}"))? + .into(), + t => return Err(format!("Invalid arg type: {}", t)), + }; + + Ok(WasmArg(arg)) + } +} -- cgit v1.3.1