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

mod codegen;

#[derive(Clone, Debug)]
pub struct VariantType {
    pub symbol: WasmSymbol,
    pub fields: BTreeMap<String, StructureItem>,
    pub span: FileSpan,
}

impl From<VariantType> for WasmType {
    fn from(value: VariantType) -> Self {
        WasmType::Variant(Box::new(value))
    }
}

impl VariantType {
    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: StructureItem) {
        self.fields.insert(field.name(), field);
    }
    pub fn with_fields<I>(mut self, fields: I) -> Self
    where
        I: IntoIterator<Item = StructureItem>,
    {
        for field in fields {
            self.set_field(field);
        }
        self
    }
}