MCPcopy Create free account
hub / github.com/explodingcamera/tinywasm / parse_arg

Function parse_arg

crates/cli/src/value_parse.rs:14–34  ·  view source on GitHub ↗
(index: usize, ty: WasmType, value: &str)

Source from the content-addressed store, hash-verified

12}
13
14fn parse_arg(index: usize, ty: WasmType, value: &str) -> Result<WasmValue> {
15 let parsed = match ty {
16 WasmType::I32 => value.parse::<i32>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
17 WasmType::I64 => value.parse::<i64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
18 WasmType::F32 => value.parse::<f32>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
19 WasmType::F64 => value.parse::<f64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
20 WasmType::V128 => value
21 .parse::<i128>()
22 .map(|v| WasmValue::V128(v.to_le_bytes()))
23 .map_err(|e| format_error(index, ty, value, e))?,
24 WasmType::RefFunc | WasmType::RefExtern => {
25 bail!(
26 "unsupported CLI argument type at position {}: {}; use the embedding API for reference values",
27 index + 1,
28 format_wasm_type(ty)
29 )
30 }
31 };
32
33 Ok(parsed)
34}
35
36fn format_error(index: usize, ty: WasmType, value: &str, error: impl core::fmt::Display) -> eyre::Report {
37 eyre::eyre!("failed to parse argument {} as {} from `{value}`: {error}", index + 1, format_wasm_type(ty))

Callers 1

parse_invocation_argsFunction · 0.85

Calls 3

format_errorFunction · 0.85
mapMethod · 0.80
to_le_bytesMethod · 0.80

Tested by

no test coverage detected