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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2015 Brendan Zabarauskas and the gl-rs developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet};

use super::{Registry, Type};
use utils::{multimap_append, multimap_insert};

#[derive(Debug, Clone)]
pub enum NamedType {
    Mixin(Mixin),
    Interface(Interface),
    Dictionary(Dictionary),
    Enum(Enum),
    Typedef(Type),
    Callback(Callback),
}

#[derive(Debug, Clone)]
pub struct Mixin {
    pub members: BTreeMap<String, Vec<Member>>,
}

#[derive(Debug, Clone)]
pub struct Interface {
    pub inherits: Option<String>,
    pub mixins: BTreeSet<String>,
    pub members: BTreeMap<String, Vec<Member>>,
    pub is_hidden: bool,
    pub has_class: bool,
    pub rendering_context: Option<&'static str>,
    pub doc_comment: String,
}

#[derive(Debug, Clone)]
pub struct Dictionary {
    pub inherits: Option<String>,
    pub fields: BTreeMap<String, Field>,
    pub is_hidden: bool,
}

#[derive(Debug, Clone)]
pub struct Enum {
    pub variants: BTreeSet<String>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Callback {
    pub args: Vec<Argument>,
    pub return_type: Option<Type>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Const {
    pub type_: Type,
    pub value: String,
}

#[derive(Debug, Eq, Clone)]
pub struct Argument {
    pub name: String,
    pub optional: bool,
    pub type_: Type,
    pub variadic: bool,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Operation {
    pub args: Vec<Argument>,
    pub return_type: Option<Type>,
    pub doc_comment: String,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Attribute {
    pub type_: Type,
    pub setter: bool,
    pub getter: bool,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Member {
    Const(Const),
    Operation(Operation),
    Attribute(Attribute),
}

#[derive(Debug, Clone)]
pub struct Field {
    pub type_: Type,
}

#[derive(Debug)]
pub struct VisitOptions {
    pub visit_mixins: bool,
}

impl NamedType {
    pub fn as_mixin(&self) -> Option<&Mixin> {
        if let &NamedType::Mixin(ref m) = self {
            Some(m)
        } else {
            None
        }
    }
    pub fn as_interface(&self) -> Option<&Interface> {
        if let &NamedType::Interface(ref i) = self {
            Some(i)
        } else {
            None
        }
    }
    pub fn as_dictionary(&self) -> Option<&Dictionary> {
        if let &NamedType::Dictionary(ref d) = self {
            Some(d)
        } else {
            None
        }
    }
    pub fn as_enum(&self) -> Option<&Enum> {
        if let &NamedType::Enum(ref e) = self {
            Some(e)
        } else {
            None
        }
    }
    pub fn as_typedef(&self) -> Option<&Type> {
        if let &NamedType::Typedef(ref t) = self {
            Some(t)
        } else {
            None
        }
    }
    pub fn as_mixin_mut(&mut self) -> Option<&mut Mixin> {
        if let &mut NamedType::Mixin(ref mut m) = self {
            Some(m)
        } else {
            None
        }
    }
    pub fn as_interface_mut(&mut self) -> Option<&mut Interface> {
        if let &mut NamedType::Interface(ref mut i) = self {
            Some(i)
        } else {
            None
        }
    }
    pub fn as_dictionary_mut(&mut self) -> Option<&mut Dictionary> {
        if let &mut NamedType::Dictionary(ref mut d) = self {
            Some(d)
        } else {
            None
        }
    }
    pub fn as_enum_mut(&mut self) -> Option<&mut Enum> {
        if let &mut NamedType::Enum(ref mut e) = self {
            Some(e)
        } else {
            None
        }
    }
    pub fn as_typedef_mut(&mut self) -> Option<&mut Type> {
        if let &mut NamedType::Typedef(ref mut t) = self {
            Some(t)
        } else {
            None
        }
    }
}

impl PartialEq for Argument {
    fn eq(&self, other: &Self) -> bool {
        self.type_ == other.type_
    }
}

impl Default for VisitOptions {
    fn default() -> Self {
        VisitOptions { visit_mixins: true }
    }
}

impl Dictionary {
    pub fn collect_fields<'a>(&'a self, registry: &'a Registry) -> BTreeMap<&'a str, &'a Field> {
        let mut fields = BTreeMap::new();

        // Inherits
        if let Some(inherit_name) = self.inherits.as_ref() {
            let inherit = registry
                .types
                .get(inherit_name)
                .and_then(NamedType::as_dictionary)
                .expect(inherit_name);
            fields.append(&mut inherit.collect_fields(registry));
        }

        // Fields
        for (name, field) in &self.fields {
            fields.insert(name, field);
        }

        fields
    }
}

impl Interface {
    pub fn collect_members<'a>(
        &'a self,
        registry: &'a Registry,
        options: &VisitOptions,
    ) -> BTreeMap<&'a str, Vec<&'a Member>> {
        let mut members = BTreeMap::new();

        // Mixins
        for mixin_name in &self.mixins {
            let mixin = registry
                .types
                .get(mixin_name)
                .and_then(NamedType::as_mixin)
                .expect(mixin_name);
            if options.visit_mixins {
                multimap_append(&mut members, mixin.collect_members(registry, options));
            }
        }

        // Members
        for (name, ms) in &self.members {
            for member in ms {
                multimap_insert(&mut members, &**name, member);
            }
        }

        members
    }
}

impl Mixin {
    pub fn collect_members<'a>(
        &'a self,
        _registry: &'a Registry,
        _options: &VisitOptions,
    ) -> BTreeMap<&'a str, Vec<&'a Member>> {
        let mut members = BTreeMap::new();

        // Members
        for (name, ms) in &self.members {
            for member in ms {
                multimap_insert(&mut members, &**name, member);
            }
        }

        members
    }
}