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
use crate::{
    helpers::{IntoWasm, WasmName},
    EncodingType, WasmSymbol, WasmType,
};
use nyar_error::FileSpan;
use std::collections::BTreeMap;
use wast::{
    component::{ComponentDefinedType, Enum},
    token::Span,
};
mod codegen;

#[derive(Clone, Debug)]
pub struct EnumerateType {
    pub symbol: WasmSymbol,
    pub fields: BTreeMap<u64, EncodingType>,
    pub span: FileSpan,
}

impl From<EnumerateType> for WasmType {
    fn from(value: EnumerateType) -> Self {
        WasmType::Enumerate(value)
    }
}

impl EnumerateType {
    pub fn new<S: Into<WasmSymbol>>(name: S) -> Self {
        Self { symbol: name.into(), fields: Default::default(), span: Default::default() }
    }
    pub fn name(&self) -> String {
        self.symbol.to_string()
    }
    pub fn set_field(&mut self, field: EncodingType) {
        self.fields.insert(field.value, field);
    }
    pub fn with_fields<I>(mut self, fields: I) -> Self
    where
        I: IntoIterator<Item = EncodingType>,
    {
        for field in fields {
            self.set_field(field);
        }
        self
    }
}