mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 06:15:33 +00:00
Return parse error strings rather than printing them in invoke parse
This commit is contained in:
parent
0a9c41b313
commit
bc8d5a542c
32
src/utils.rs
32
src/utils.rs
@ -8,12 +8,12 @@ pub fn is_wasm_binary(binary: &[u8]) -> bool {
|
|||||||
binary.starts_with(&[b'\0', b'a', b's', b'm'])
|
binary.starts_with(&[b'\0', b'a', b's', b'm'])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum InvokeError {
|
pub enum InvokeError {
|
||||||
CouldNotFindFunction,
|
CouldNotFindFunction,
|
||||||
ExportNotFunction,
|
ExportNotFunction,
|
||||||
WrongNumArgs { expected: u16, found: u16 },
|
WrongNumArgs { expected: u16, found: u16 },
|
||||||
CouldNotParseArgs,
|
CouldNotParseArg(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses arguments for the `--invoke` flag on the run command
|
/// Parses arguments for the `--invoke` flag on the run command
|
||||||
@ -40,6 +40,7 @@ pub fn parse_args(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let parameter_types = signature.params();
|
let parameter_types = signature.params();
|
||||||
|
let mut arg_error = None;
|
||||||
|
|
||||||
if args.len() != parameter_types.len() {
|
if args.len() != parameter_types.len() {
|
||||||
return Err(InvokeError::WrongNumArgs {
|
return Err(InvokeError::WrongNumArgs {
|
||||||
@ -57,35 +58,50 @@ pub fn parse_args(
|
|||||||
.parse::<i32>()
|
.parse::<i32>()
|
||||||
.map(|v| Some(Value::I32(v)))
|
.map(|v| Some(Value::I32(v)))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
eprintln!("Failed to parse `{:?}` as an `i32`", argument);
|
arg_error = Some(InvokeError::CouldNotParseArg(format!(
|
||||||
|
"Failed to parse `{:?}` as an `i32`",
|
||||||
|
argument
|
||||||
|
)));
|
||||||
None
|
None
|
||||||
}),
|
}),
|
||||||
Type::I64 => argument
|
Type::I64 => argument
|
||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.map(|v| Some(Value::I64(v)))
|
.map(|v| Some(Value::I64(v)))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
eprintln!("Failed to parse `{:?}` as an `i64`", argument);
|
arg_error = Some(InvokeError::CouldNotParseArg(format!(
|
||||||
|
"Failed to parse `{:?}` as an `i64`",
|
||||||
|
argument
|
||||||
|
)));
|
||||||
None
|
None
|
||||||
}),
|
}),
|
||||||
Type::V128 => argument
|
Type::V128 => argument
|
||||||
.parse::<u128>()
|
.parse::<u128>()
|
||||||
.map(|v| Some(Value::V128(v)))
|
.map(|v| Some(Value::V128(v)))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
eprintln!("Failed to parse `{:?}` as an `i64`", argument);
|
arg_error = Some(InvokeError::CouldNotParseArg(format!(
|
||||||
|
"Failed to parse `{:?}` as an `i128`",
|
||||||
|
argument
|
||||||
|
)));
|
||||||
None
|
None
|
||||||
}),
|
}),
|
||||||
Type::F32 => argument
|
Type::F32 => argument
|
||||||
.parse::<f32>()
|
.parse::<f32>()
|
||||||
.map(|v| Some(Value::F32(v)))
|
.map(|v| Some(Value::F32(v)))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
eprintln!("Failed to parse `{:?}` as an `f32`", argument);
|
arg_error = Some(InvokeError::CouldNotParseArg(format!(
|
||||||
|
"Failed to parse `{:?}` as an `f32`",
|
||||||
|
argument
|
||||||
|
)));
|
||||||
None
|
None
|
||||||
}),
|
}),
|
||||||
Type::F64 => argument
|
Type::F64 => argument
|
||||||
.parse::<f64>()
|
.parse::<f64>()
|
||||||
.map(|v| Some(Value::F64(v)))
|
.map(|v| Some(Value::F64(v)))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
eprintln!("Failed to parse `{:?}` as an `f64`", argument);
|
arg_error = Some(InvokeError::CouldNotParseArg(format!(
|
||||||
|
"Failed to parse `{:?}` as an `f64`",
|
||||||
|
argument
|
||||||
|
)));
|
||||||
None
|
None
|
||||||
}),
|
}),
|
||||||
} {
|
} {
|
||||||
@ -98,7 +114,7 @@ pub fn parse_args(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.map_or_else(
|
.map_or_else(
|
||||||
|| Err(InvokeError::CouldNotParseArgs),
|
|| Err(arg_error.unwrap()),
|
||||||
|arguments: Vec<Value>| Ok(arguments),
|
|arguments: Vec<Value>| Ok(arguments),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user