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
use std::{collections::HashMap, path::PathBuf};

use wapm_toml::Bindings;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MetadataTable {
    #[serde(alias = "wapm")]
    pub wasmer: Wasmer,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Wasmer {
    pub namespace: String,
    pub package: Option<String>,
    pub wasmer_extra_flags: Option<String>,
    pub abi: wapm_toml::Abi,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fs: Option<HashMap<String, PathBuf>>,
    pub bindings: Option<Bindings>,
}

#[cfg(test)]
mod tests {
    use serde::Deserialize;
    use wapm_toml::{WaiBindings, WitBindings};

    use super::*;

    #[test]
    fn parse_wai_bindings() {
        let table = toml::toml! {
            [wasmer]
            namespace = "wasmer"
            abi = "none"
            bindings = { wai-version = "0.1.0", exports = "hello-world.wai" }
        };
        let should_be = MetadataTable {
            wasmer: Wasmer {
                namespace: "wasmer".to_string(),
                package: None,
                wasmer_extra_flags: None,
                abi: wapm_toml::Abi::None,
                fs: None,
                bindings: Some(Bindings::Wai(WaiBindings {
                    exports: Some("hello-world.wai".into()),
                    imports: Vec::new(),
                    wai_version: "0.1.0".parse().unwrap(),
                })),
            },
        };

        let got = MetadataTable::deserialize(table).unwrap();

        assert_eq!(got, should_be);
    }

    #[test]
    fn parse_wit_bindings() {
        let table = toml::toml! {
            [wasmer]
            namespace = "wasmer"
            abi = "none"
            bindings = { wit-bindgen = "0.1.0", wit-exports = "hello-world.wit" }
        };
        let should_be = MetadataTable {
            wasmer: Wasmer {
                namespace: "wasmer".to_string(),
                package: None,
                wasmer_extra_flags: None,
                abi: wapm_toml::Abi::None,
                fs: None,
                bindings: Some(Bindings::Wit(WitBindings {
                    wit_bindgen: "0.1.0".parse().unwrap(),
                    wit_exports: "hello-world.wit".into(),
                })),
            },
        };

        let got = MetadataTable::deserialize(table).unwrap();

        assert_eq!(got, should_be);
    }
}