1mod fmt;
4mod func;
5mod ty;
6mod val;
7
8pub use fmt::{DisplayFunc, DisplayFuncArgs, DisplayFuncResults, DisplayType, DisplayValue};
9pub use func::WasmFunc;
10pub use ty::{WasmType, WasmTypeKind};
11pub use val::WasmValue;
12
13pub(crate) use ty::maybe_unwrap_type;
14pub(crate) use val::unwrap_val;
15
16pub 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#[derive(Debug)]
30#[allow(missing_docs)]
31pub 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
44impl 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
53impl 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
78impl std::error::Error for WasmValueError {}