Skip to main content

wasm_wave/wasm/
ty.rs

1use alloc::{borrow::Cow, boxed::Box};
2
3/// The kind of a [`WasmType`]. These correspond to the value types defined by the
4/// [Component Model design](https://github.com/WebAssembly/component-model/blob/673d5c43c3cc0f4aeb8996a5c0931af623f16808/design/mvp/WIT.md).
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6#[allow(missing_docs)]
7#[non_exhaustive]
8pub enum WasmTypeKind {
9    Bool,
10    S8,
11    S16,
12    S32,
13    S64,
14    U8,
15    U16,
16    U32,
17    U64,
18    F32,
19    F64,
20    Char,
21    String,
22    List,
23    FixedLengthList,
24    Record,
25    Tuple,
26    Variant,
27    Enum,
28    Option,
29    Result,
30    Flags,
31    Map,
32    #[doc(hidden)]
33    Unsupported,
34}
35
36impl core::fmt::Display for WasmTypeKind {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        f.write_str(match self {
39            WasmTypeKind::Bool => "bool",
40            WasmTypeKind::S8 => "s8",
41            WasmTypeKind::S16 => "s16",
42            WasmTypeKind::S32 => "s32",
43            WasmTypeKind::S64 => "s64",
44            WasmTypeKind::U8 => "u8",
45            WasmTypeKind::U16 => "u16",
46            WasmTypeKind::U32 => "u32",
47            WasmTypeKind::U64 => "u64",
48            WasmTypeKind::F32 => "f32",
49            WasmTypeKind::F64 => "f64",
50            WasmTypeKind::Char => "char",
51            WasmTypeKind::String => "string",
52            WasmTypeKind::List => "list",
53            WasmTypeKind::FixedLengthList => "list<_,N>",
54            WasmTypeKind::Record => "record",
55            WasmTypeKind::Tuple => "tuple",
56            WasmTypeKind::Variant => "variant",
57            WasmTypeKind::Enum => "enum",
58            WasmTypeKind::Option => "option",
59            WasmTypeKind::Result => "result",
60            WasmTypeKind::Flags => "flags",
61            WasmTypeKind::Map => "map",
62            WasmTypeKind::Unsupported => "<<UNSUPPORTED>>",
63        })
64    }
65}
66
67/// The WasmType trait may be implemented to represent types to be
68/// (de)serialized with WAVE, notably [`value::Type`](crate::value::Type).
69/// The `wasmtime` crate provides an impl for `wasmtime::component::Type`.
70///
71/// The `Self`-returning methods should be called only for corresponding
72/// [`WasmTypeKind`]s.
73pub trait WasmType: Clone + Sized {
74    /// Returns the [`WasmTypeKind`] of this type.
75    fn kind(&self) -> WasmTypeKind;
76
77    /// Returns the list element type or `None` if `self` is not a list type.
78    /// # Panics
79    /// Panics if the type is not implemented (the trait default).
80    fn list_element_type(&self) -> Option<Self> {
81        unimplemented!()
82    }
83    /// Returns an iterator of the record's field names and Types. The
84    /// iterator will be empty iff `self` is not a record type.
85    /// # Panics
86    /// Panics if the type is not implemented (the trait default).
87    fn record_fields(&self) -> Box<dyn Iterator<Item = (Cow<'_, str>, Self)> + '_> {
88        unimplemented!()
89    }
90    /// Returns an iterator of the tuple's field Types. The iterator will be
91    /// empty iff `self` is not a tuple type.
92    /// # Panics
93    /// Panics if the type is not implemented (the trait default).
94    fn tuple_element_types(&self) -> Box<dyn Iterator<Item = Self> + '_> {
95        unimplemented!()
96    }
97    /// Returns an iterator of the variant's case names and optional payload
98    /// Types. The iterator will be empty iff `self` is not a tuple type.
99    /// # Panics
100    /// Panics if the type is not implemented (the trait default).
101    fn variant_cases(&self) -> Box<dyn Iterator<Item = (Cow<'_, str>, Option<Self>)> + '_> {
102        unimplemented!()
103    }
104    /// Returns an iterator of the enum's case names. The iterator will be
105    /// empty iff `self` is not an enum type.
106    /// # Panics
107    /// Panics if the type is not implemented (the trait default).
108    fn enum_cases(&self) -> Box<dyn Iterator<Item = Cow<'_, str>> + '_> {
109        unimplemented!()
110    }
111    /// Returns the option's "some" type or `None` if `self` is not an option type.
112    /// # Panics
113    /// Panics if the type is not implemented (the trait default).
114    fn option_some_type(&self) -> Option<Self> {
115        unimplemented!()
116    }
117    /// Returns the result's optional "ok" and "err" Types or `None` if `self`
118    /// is not a result type.
119    /// # Panics
120    /// Panics if the type is not implemented (the trait default).
121    fn result_types(&self) -> Option<(Option<Self>, Option<Self>)> {
122        unimplemented!()
123    }
124    /// Returns an iterator of the flags' names. The iterator will be empty iff
125    /// `self` is not a flags type.
126    /// # Panics
127    /// Panics if the type is not implemented (the trait default).
128    fn flags_names(&self) -> Box<dyn Iterator<Item = Cow<'_, str>> + '_> {
129        unimplemented!()
130    }
131    /// Returns the map's key and value Types or `None` if `self` is not a map
132    /// type.
133    /// # Panics
134    /// Panics if the type is not implemented (the trait default).
135    fn map_key_value_types(&self) -> Option<(Self, Self)> {
136        unimplemented!()
137    }
138}
139
140macro_rules! maybe_unwrap_type {
141    ($ty:expr, $case:path) => {
142        match $ty {
143            $case(v) => Some(v),
144            _ => None,
145        }
146    };
147}
148pub(crate) use maybe_unwrap_type;