| 1 |
//! Wasm type and value types |
| 2 |
|
| 3 |
mod fmt; |
| 4 |
mod func; |
| 5 |
mod ty; |
| 6 |
mod val; |
| 7 |
|
| 8 |
pub use fmt::{DisplayFunc, DisplayFuncArgs, DisplayFuncResults, DisplayType, DisplayValue}; |
| 9 |
pub use func::WasmFunc; |
| 10 |
pub use ty::{WasmType, WasmTypeKind}; |
| 11 |
pub use val::WasmValue; |
| 12 |
|
| 13 |
pub(crate) use ty::maybe_unwrap_type; |
| 14 |
pub(crate) use val::unwrap_val; |
| 15 |
|
| 16 |
/// Returns an error if the given [`WasmType`] is not of the given [`WasmTypeKind`]. |
| 17 |
pub fn ensure_type_kind(ty: &impl WasmType, kind: WasmTypeKind) -> Result<(), WasmValueError> { |
| 18 |
if ty.kind() == kind { |
| 19 |
Ok(()) |
| 20 |
} else { |
| 21 |
Err(WasmValueError::WrongTypeKind { |
| 22 |
ty: DisplayType(ty).to_string(), |
| 23 |
kind, |
| 24 |
}) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/// An error from creating a [`WasmValue`]. |
| 29 |
#[derive(Debug)] |
| 30 |
#[allow(missing_docs)] |
| 31 |
pub enum WasmValueError { |
| 32 |
MissingField(String), |
| 33 |
MissingPayload(String), |
| 34 |
UnexpectedPayload(String), |
| 35 |
UnknownCase(String), |
| 36 |
UnknownField(String), |
| 37 |
UnsupportedType(String), |
| 38 |
WrongNumberOfTupleValues { want: usize, got: usize }, |
| 39 |
WrongTypeKind { kind: WasmTypeKind, ty: String }, |
| 40 |
WrongValueType { ty: String, val: String }, |
| 41 |
Other(String), |
| 42 |
} |
| 43 |
|
| 44 |
impl WasmValueError { |
| 45 |
pub(crate) fn wrong_value_type(ty: &impl WasmType, val: &impl WasmValue) -> Self { |
| 46 |
Self::WrongValueType { |
| 47 |
ty: DisplayType(ty).to_string(), |
| 48 |
val: DisplayValue(val).to_string(), |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl std::fmt::Display for WasmValueError { |
| 54 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 55 |
match self { |
| 56 |
Self::MissingField(name) => { |
| 57 |
write!(f, "missing field {name:?}") |
| 58 |
} |
| 59 |
Self::MissingPayload(name) => write!(f, "missing payload for {name:?} case"), |
| 60 |
Self::UnexpectedPayload(name) => write!(f, "unexpected payload for {name:?} case"), |
| 61 |
Self::UnknownCase(name) => write!(f, "unknown case {name:?}"), |
| 62 |
Self::UnknownField(name) => write!(f, "unknown field {name:?}"), |
| 63 |
Self::UnsupportedType(ty) => write!(f, "unsupported type {ty}"), |
| 64 |
Self::WrongNumberOfTupleValues { want, got } => { |
| 65 |
write!(f, "expected {want} tuple elements; got {got}") |
| 66 |
} |
| 67 |
Self::WrongTypeKind { kind, ty } => { |
| 68 |
write!(f, "expected a {kind}; got {ty}") |
| 69 |
} |
| 70 |
Self::WrongValueType { ty, val } => { |
| 71 |
write!(f, "expected a {ty}; got {val}") |
| 72 |
} |
| 73 |
Self::Other(msg) => write!(f, "{msg}"), |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl std::error::Error for WasmValueError {} |