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
#[macro_use]
extern crate serde_derive;

pub const SCHEMA_VERSION: &str = "3";

#[derive(Deserialize)]
pub struct ProgramOnlySchema {
    pub schema_version: String,
    pub version: String,
}

#[derive(Deserialize, Serialize)]
pub struct Program {
    pub exports: Vec<Export>,
    pub enums: Vec<Enum>,
    pub imports: Vec<Import>,
    pub structs: Vec<Struct>,
    pub version: String,
    pub schema_version: String,
}

#[derive(Deserialize, Serialize)]
pub struct Import {
    pub module: Option<String>,
    pub js_namespace: Option<String>,
    pub kind: ImportKind,
}

#[derive(Deserialize, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum ImportKind {
    Function(ImportFunction),
    Static(ImportStatic),
    Type(ImportType),
}

#[derive(Deserialize, Serialize)]
pub struct ImportFunction {
    pub shim: String,
    pub catch: bool,
    pub method: bool,
    pub js_new: bool,
    pub structural: bool,
    pub getter: Option<String>,
    pub setter: Option<String>,
    pub class: Option<String>,
    pub function: Function,
}

#[derive(Deserialize, Serialize)]
pub struct ImportStatic {
    pub name: String,
    pub shim: String,
}

#[derive(Deserialize, Serialize)]
pub struct ImportType {
}

#[derive(Deserialize, Serialize)]
pub struct Export {
    pub class: Option<String>,
    pub method: bool,
    pub constructor: Option<String>,
    pub function: Function,
}

#[derive(Deserialize, Serialize)]
pub struct Enum {
    pub name: String,
    pub variants: Vec<EnumVariant>,
}

#[derive(Deserialize, Serialize)]
pub struct EnumVariant {
    pub name: String,
    pub value: u32
}

#[derive(Deserialize, Serialize)]
pub struct Function {
    pub name: String,
}

#[derive(Deserialize, Serialize)]
pub struct Struct {
    pub name: String,
    pub fields: Vec<StructField>,
}

#[derive(Deserialize, Serialize)]
pub struct StructField {
    pub name: String,
}

pub fn new_function(struct_name: &str) -> String {
    let mut name = format!("__wbg_");
    name.extend(struct_name
        .chars()
        .flat_map(|s| s.to_lowercase()));
    name.push_str("_new");
    return name
}

pub fn free_function(struct_name: &str) -> String {
    let mut name = format!("__wbg_");
    name.extend(struct_name
        .chars()
        .flat_map(|s| s.to_lowercase()));
    name.push_str("_free");
    return name
}

pub fn free_function_export_name(function_name: &str) -> String {
    function_name.to_string()
}

pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
    let mut name = struct_
        .chars()
        .flat_map(|s| s.to_lowercase())
        .collect::<String>();
    name.push_str("_");
    name.push_str(f);
    return name
}

pub fn struct_field_get(struct_: &str, f: &str) -> String {
    let mut name = String::from("__wbg_get_");
    name.extend(struct_
        .chars()
        .flat_map(|s| s.to_lowercase()));
    name.push_str("_");
    name.push_str(f);
    return name
}

pub fn struct_field_set(struct_: &str, f: &str) -> String {
    let mut name = String::from("__wbg_set_");
    name.extend(struct_
        .chars()
        .flat_map(|s| s.to_lowercase()));
    name.push_str("_");
    name.push_str(f);
    return name
}

pub fn version() -> String {
    let mut v = env!("CARGO_PKG_VERSION").to_string();
    if let Some(s) = option_env!("WBG_VERSION") {
        v.push_str(" (");
        v.push_str(s);
        v.push_str(")");
    }
    return v
}