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