use hdi::prelude::*;
pub fn is_host_short_circuit(error: &WasmError) -> bool {
matches!(error.error, WasmErrorInner::HostShortCircuit(_))
}
pub fn invalid_or_host_error(error: WasmError) -> ExternResult<ValidateCallbackResult> {
if is_host_short_circuit(&error) {
Err(error)
} else {
Ok(ValidateCallbackResult::Invalid(error.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn host_short_circuit() -> WasmError {
WasmError {
file: file!().to_string(),
line: line!(),
error: WasmErrorInner::HostShortCircuit(Vec::new()),
}
}
fn guest_error(msg: &str) -> WasmError {
WasmError {
file: file!().to_string(),
line: line!(),
error: WasmErrorInner::Guest(msg.to_string()),
}
}
#[test]
fn is_host_short_circuit_true_for_host_short_circuit() {
assert!(is_host_short_circuit(&host_short_circuit()));
}
#[test]
fn is_host_short_circuit_false_for_guest_error() {
assert!(!is_host_short_circuit(&guest_error("nope")));
}
#[test]
fn is_host_short_circuit_false_for_serialize_error() {
let err = WasmError {
file: file!().to_string(),
line: line!(),
error: WasmErrorInner::Serialize(SerializedBytesError::Deserialize("x".to_string())),
};
assert!(!is_host_short_circuit(&err));
}
#[test]
fn invalid_or_host_error_propagates_host_short_circuit() {
let res = invalid_or_host_error(host_short_circuit());
match res {
Err(e) => assert!(is_host_short_circuit(&e)),
other => panic!("expected Err(HostShortCircuit), got {:?}", other),
}
}
#[test]
fn invalid_or_host_error_collapses_guest_to_invalid() {
let res = invalid_or_host_error(guest_error("bad data"));
match res {
Ok(ValidateCallbackResult::Invalid(msg)) => assert!(msg.contains("bad data")),
other => panic!("expected Ok(Invalid), got {:?}", other),
}
}
}