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
use std::{ops::AddAssign, sync::Arc};

pub use encoder::WastEncoder;
pub use wasi_module::WasiInstance;
pub use wasi_types::{WasiFunction, WasiParameter, WasiResource, WasiType};

mod encoder;
mod wasi_module;
mod wasi_types;

pub struct CanonicalWasi {
    pub name: Arc<str>,
    pub imports: Vec<CanonicalImport>,
}

pub enum CanonicalImport {
    Instance(WasiInstance),
}

impl Default for CanonicalWasi {
    fn default() -> Self {
        Self { name: Arc::from("root"), imports: vec![] }
    }
}

impl AddAssign<WasiInstance> for CanonicalWasi {
    fn add_assign(&mut self, rhs: WasiInstance) {
        self.imports.push(CanonicalImport::Instance(rhs));
    }
}

impl CanonicalWasi {
    pub fn add_instance(&mut self, instance: WasiInstance) {
        self.imports.push(CanonicalImport::Instance(instance));
    }
    pub fn encode(&self) -> String {
        let mut output = String::with_capacity(1024);
        let mut encoder = WastEncoder::new(&self, &mut output);
        encoder.encode().unwrap();
        output
    }
}