wasm_wave/value/
ty.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::{borrow::Cow, sync::Arc};

use crate::wasm::{maybe_unwrap_type, WasmType, WasmTypeKind};

/// The [`WasmType`] of a [`Value`](super::Value).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Type(pub(super) TypeEnum);

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum TypeEnum {
    Simple(SimpleType),
    List(Arc<ListType>),
    Record(Arc<RecordType>),
    Tuple(Arc<TupleType>),
    Variant(Arc<VariantType>),
    Enum(Arc<EnumType>),
    Option(Arc<OptionType>),
    Result(Arc<ResultType>),
    Flags(Arc<FlagsType>),
}

#[allow(missing_docs)]
impl Type {
    pub const BOOL: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::Bool)));
    pub const S8: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::S8)));
    pub const S16: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::S16)));
    pub const S32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::S32)));
    pub const S64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::S64)));
    pub const U8: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U8)));
    pub const U16: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U16)));
    pub const U32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U32)));
    pub const U64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U64)));
    pub const F32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::F32)));
    pub const F64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::F64)));
    pub const CHAR: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::Char)));
    pub const STRING: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::String)));

    /// Returns the simple type of the given `kind`. Returns None if the kind
    /// represents a parameterized type.
    pub fn simple(kind: WasmTypeKind) -> Option<Self> {
        is_simple(kind).then_some(Self(TypeEnum::Simple(SimpleType(kind))))
    }

    #[doc(hidden)]
    pub const fn must_simple(kind: WasmTypeKind) -> Self {
        if !is_simple(kind) {
            panic!("kind is not simple");
        }
        Self(TypeEnum::Simple(SimpleType(kind)))
    }

    /// Returns a list type with the given element type.
    pub fn list(element_type: impl Into<Self>) -> Self {
        let element = element_type.into();
        Self(TypeEnum::List(Arc::new(ListType { element })))
    }

    /// Returns a record type with the given field types. Returns None if
    /// `fields` is empty.
    pub fn record<T: Into<Box<str>>>(
        field_types: impl IntoIterator<Item = (T, Self)>,
    ) -> Option<Self> {
        let fields = field_types
            .into_iter()
            .map(|(name, ty)| (name.into(), ty))
            .collect::<Box<[_]>>();
        if fields.is_empty() {
            return None;
        }
        Some(Self(TypeEnum::Record(Arc::new(RecordType { fields }))))
    }

    /// Returns a tuple type with the given element types. Returns None if
    /// `element_types` is empty.
    pub fn tuple(element_types: impl Into<Box<[Self]>>) -> Option<Self> {
        let elements = element_types.into();
        if elements.is_empty() {
            return None;
        }
        Some(Self(TypeEnum::Tuple(Arc::new(TupleType { elements }))))
    }

    /// Returns a variant type with the given case names and optional payloads.
    /// Returns None if `cases` is empty.
    pub fn variant<T: Into<Box<str>>>(
        cases: impl IntoIterator<Item = (T, Option<Self>)>,
    ) -> Option<Self> {
        let cases = cases
            .into_iter()
            .map(|(name, ty)| (name.into(), ty))
            .collect::<Box<[_]>>();
        if cases.is_empty() {
            return None;
        }
        Some(Self(TypeEnum::Variant(Arc::new(VariantType { cases }))))
    }

    /// Returns an enum type with the given case names. Returns None if `cases`
    /// is empty.
    pub fn enum_ty<T: Into<Box<str>>>(cases: impl IntoIterator<Item = T>) -> Option<Self> {
        let cases = cases.into_iter().map(Into::into).collect::<Box<[_]>>();
        if cases.is_empty() {
            return None;
        }
        Some(Self(TypeEnum::Enum(Arc::new(EnumType { cases }))))
    }

    /// Returns an option type with the given "some" type.
    pub fn option(some: Self) -> Self {
        Self(TypeEnum::Option(Arc::new(OptionType { some })))
    }

    /// Returns a result type with the given optional "ok" and "err" payloads.
    pub fn result(ok: Option<Self>, err: Option<Self>) -> Self {
        Self(TypeEnum::Result(Arc::new(ResultType { ok, err })))
    }

    /// Returns a flags type with the given flag names. Returns None if `flags`
    /// is empty.
    pub fn flags<T: Into<Box<str>>>(flags: impl IntoIterator<Item = T>) -> Option<Self> {
        let flags = flags.into_iter().map(Into::into).collect::<Box<[_]>>();
        if flags.is_empty() {
            return None;
        }
        Some(Self(TypeEnum::Flags(Arc::new(FlagsType { flags }))))
    }

    /// Returns a [`Type`] matching the given [`WasmType`]. Returns None if the
    /// given type is unsupported or otherwise invalid.
    pub fn from_wasm_type(ty: &impl WasmType) -> Option<Self> {
        super::convert::from_wasm_type(ty)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SimpleType(WasmTypeKind);

const fn is_simple(kind: WasmTypeKind) -> bool {
    use WasmTypeKind::*;
    matches!(
        kind,
        Bool | S8 | S16 | S32 | S64 | U8 | U16 | U32 | U64 | F32 | F64 | Char | String
    )
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListType {
    pub(super) element: Type,
}

#[derive(Debug, PartialEq, Eq)]
pub struct RecordType {
    pub(super) fields: Box<[(Box<str>, Type)]>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct TupleType {
    pub(super) elements: Box<[Type]>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct VariantType {
    pub(super) cases: Box<[(Box<str>, Option<Type>)]>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct EnumType {
    pub(super) cases: Box<[Box<str>]>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct OptionType {
    pub(super) some: Type,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ResultType {
    pub(super) ok: Option<Type>,
    pub(super) err: Option<Type>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct FlagsType {
    pub(super) flags: Box<[Box<str>]>,
}

impl WasmType for Type {
    fn kind(&self) -> WasmTypeKind {
        match self.0 {
            TypeEnum::Simple(simple) => simple.0,
            TypeEnum::List(_) => WasmTypeKind::List,
            TypeEnum::Record(_) => WasmTypeKind::Record,
            TypeEnum::Tuple(_) => WasmTypeKind::Tuple,
            TypeEnum::Variant(_) => WasmTypeKind::Variant,
            TypeEnum::Enum(_) => WasmTypeKind::Enum,
            TypeEnum::Option(_) => WasmTypeKind::Option,
            TypeEnum::Result(_) => WasmTypeKind::Result,
            TypeEnum::Flags(_) => WasmTypeKind::Flags,
        }
    }

    fn list_element_type(&self) -> Option<Self> {
        let list = maybe_unwrap_type!(&self.0, TypeEnum::List)?;
        Some(list.element.clone())
    }

    fn record_fields(&self) -> Box<dyn Iterator<Item = (Cow<str>, Self)> + '_> {
        let TypeEnum::Record(record) = &self.0 else {
            return Box::new(std::iter::empty());
        };
        Box::new(
            record
                .fields
                .iter()
                .map(|(name, ty)| (name.as_ref().into(), ty.clone())),
        )
    }

    fn tuple_element_types(&self) -> Box<dyn Iterator<Item = Self> + '_> {
        let TypeEnum::Tuple(tuple) = &self.0 else {
            return Box::new(std::iter::empty());
        };
        Box::new(tuple.elements.iter().cloned())
    }

    fn variant_cases(&self) -> Box<dyn Iterator<Item = (Cow<str>, Option<Self>)> + '_> {
        let TypeEnum::Variant(variant) = &self.0 else {
            return Box::new(std::iter::empty());
        };
        Box::new(
            variant
                .cases
                .iter()
                .map(|(name, ty)| (name.as_ref().into(), ty.clone())),
        )
    }

    fn enum_cases(&self) -> Box<dyn Iterator<Item = Cow<str>> + '_> {
        let TypeEnum::Enum(enum_) = &self.0 else {
            return Box::new(std::iter::empty());
        };
        Box::new(enum_.cases.iter().map(|name| name.as_ref().into()))
    }

    fn option_some_type(&self) -> Option<Self> {
        let option = maybe_unwrap_type!(&self.0, TypeEnum::Option)?;
        Some(option.some.clone())
    }

    fn result_types(&self) -> Option<(Option<Self>, Option<Self>)> {
        let result = maybe_unwrap_type!(&self.0, TypeEnum::Result)?;
        Some((result.ok.clone(), result.err.clone()))
    }

    fn flags_names(&self) -> Box<dyn Iterator<Item = Cow<str>> + '_> {
        let TypeEnum::Flags(flags) = &self.0 else {
            return Box::new(std::iter::empty());
        };
        Box::new(flags.flags.iter().map(|name| name.as_ref().into()))
    }
}

impl std::fmt::Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        crate::wasm::DisplayType(self).fmt(f)
    }
}