wasm_wave/wasm/ty.rs
1use std::{borrow::Cow, fmt::Debug};
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 FixedSizeList,
24 Record,
25 Tuple,
26 Variant,
27 Enum,
28 Option,
29 Result,
30 Flags,
31 #[doc(hidden)]
32 Unsupported,
33}
34
35impl std::fmt::Display for WasmTypeKind {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 f.write_str(match self {
38 WasmTypeKind::Bool => "bool",
39 WasmTypeKind::S8 => "s8",
40 WasmTypeKind::S16 => "s16",
41 WasmTypeKind::S32 => "s32",
42 WasmTypeKind::S64 => "s64",
43 WasmTypeKind::U8 => "u8",
44 WasmTypeKind::U16 => "u16",
45 WasmTypeKind::U32 => "u32",
46 WasmTypeKind::U64 => "u64",
47 WasmTypeKind::F32 => "f32",
48 WasmTypeKind::F64 => "f64",
49 WasmTypeKind::Char => "char",
50 WasmTypeKind::String => "string",
51 WasmTypeKind::List => "list",
52 WasmTypeKind::FixedSizeList => "list<_,N>",
53 WasmTypeKind::Record => "record",
54 WasmTypeKind::Tuple => "tuple",
55 WasmTypeKind::Variant => "variant",
56 WasmTypeKind::Enum => "enum",
57 WasmTypeKind::Option => "option",
58 WasmTypeKind::Result => "result",
59 WasmTypeKind::Flags => "flags",
60 WasmTypeKind::Unsupported => "<<UNSUPPORTED>>",
61 })
62 }
63}
64
65/// The WasmType trait may be implemented to represent types to be
66/// (de)serialized with WAVE, notably [`value::Type`](crate::value::Type).
67/// The `wasmtime` crate provides an impl for `wasmtime::component::Type`.
68///
69/// The `Self`-returning methods should be called only for corresponding
70/// [`WasmTypeKind`]s.
71pub trait WasmType: Clone + Sized {
72 /// Returns the [`WasmTypeKind`] of this type.
73 fn kind(&self) -> WasmTypeKind;
74
75 /// Returns the list element type or `None` if `self` is not a list type.
76 /// # Panics
77 /// Panics if the type is not implemented (the trait default).
78 fn list_element_type(&self) -> Option<Self> {
79 unimplemented!()
80 }
81 /// Returns an iterator of the record's field names and Types. The
82 /// iterator will be empty iff `self` is not a record type.
83 /// # Panics
84 /// Panics if the type is not implemented (the trait default).
85 fn record_fields(&self) -> Box<dyn Iterator<Item = (Cow<'_, str>, Self)> + '_> {
86 unimplemented!()
87 }
88 /// Returns an iterator of the tuple's field Types. The iterator will be
89 /// empty iff `self` is not a tuple type.
90 /// # Panics
91 /// Panics if the type is not implemented (the trait default).
92 fn tuple_element_types(&self) -> Box<dyn Iterator<Item = Self> + '_> {
93 unimplemented!()
94 }
95 /// Returns an iterator of the variant's case names and optional payload
96 /// Types. The iterator will be empty iff `self` is not a tuple type.
97 /// # Panics
98 /// Panics if the type is not implemented (the trait default).
99 fn variant_cases(&self) -> Box<dyn Iterator<Item = (Cow<'_, str>, Option<Self>)> + '_> {
100 unimplemented!()
101 }
102 /// Returns an iterator of the enum's case names. The iterator will be
103 /// empty iff `self` is not an enum type.
104 /// # Panics
105 /// Panics if the type is not implemented (the trait default).
106 fn enum_cases(&self) -> Box<dyn Iterator<Item = Cow<'_, str>> + '_> {
107 unimplemented!()
108 }
109 /// Returns the option's "some" type or `None` if `self` is not an option type.
110 /// # Panics
111 /// Panics if the type is not implemented (the trait default).
112 fn option_some_type(&self) -> Option<Self> {
113 unimplemented!()
114 }
115 /// Returns the result's optional "ok" and "err" Types or `None` if `self`
116 /// is not a result type.
117 /// # Panics
118 /// Panics if the type is not implemented (the trait default).
119 fn result_types(&self) -> Option<(Option<Self>, Option<Self>)> {
120 unimplemented!()
121 }
122 /// Returns an iterator of the flags' names. The iterator will be empty iff
123 /// `self` is not a flags type.
124 /// # Panics
125 /// Panics if the type is not implemented (the trait default).
126 fn flags_names(&self) -> Box<dyn Iterator<Item = Cow<'_, str>> + '_> {
127 unimplemented!()
128 }
129}
130
131macro_rules! maybe_unwrap_type {
132 ($ty:expr, $case:path) => {
133 match $ty {
134 $case(v) => Some(v),
135 _ => None,
136 }
137 };
138}
139pub(crate) use maybe_unwrap_type;