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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::path::PathBuf;
use std::rc::Rc;

use crate::model::*;

#[derive(Clone, Debug)]
pub(crate) enum Statement<D>
where
    D: DocReference,
{
    Constants(Handle<ConstantSet<D>>),
    StructDeclaration(StructDeclarationHandle),
    StructDefinition(StructType<D>),
    EnumDefinition(Handle<Enum<D>>),
    ErrorType(ErrorType<D>),
    ClassDeclaration(ClassDeclarationHandle),
    ClassDefinition(Handle<Class<D>>),
    StaticClassDefinition(Handle<StaticClass<D>>),
    InterfaceDefinition(InterfaceType<D>),
    IteratorDeclaration(Handle<AbstractIterator<D>>),
    CollectionDeclaration(Handle<Collection<D>>),
    FunctionDefinition(Handle<Function<D>>),
}

impl Statement<Unvalidated> {
    pub(crate) fn unique_name(&self) -> Option<&Name> {
        match self {
            Statement::Constants(x) => Some(&x.name),
            Statement::StructDeclaration(x) => Some(&x.name),
            Statement::StructDefinition(_) => {
                // the name is shared with the declaration
                None
            }
            Statement::EnumDefinition(x) => Some(&x.name),
            Statement::ErrorType(x) => Some(&x.exception_name),
            Statement::ClassDeclaration(x) => Some(&x.name),
            Statement::ClassDefinition(_) => {
                // the name is shared with the declaration
                None
            }
            Statement::StaticClassDefinition(x) => Some(&x.name),
            Statement::InterfaceDefinition(x) => Some(&x.untyped().name),
            Statement::IteratorDeclaration(_) => {
                // the name is derived in a language specific way
                None
            }
            Statement::CollectionDeclaration(_) => {
                // the name is derived in a language specific way
                None
            }
            Statement::FunctionDefinition(x) => Some(&x.name),
        }
    }
}

pub struct DeveloperInfo {
    /// Full name of the developer
    pub name: String,
    /// Email of the developer
    pub email: String,
    /// Name of the organization the developer is working for
    pub organization: String,
    /// Organization website URL
    pub organization_url: String,
}

/// metadata related to the library
pub struct LibraryInfo {
    /// Description of the library
    pub description: String,
    /// URL of the project
    pub project_url: String,
    /// GitHub organisation and repo name (e.g. stepfunc/oo_bindgen)
    pub repository: String,
    /// License name
    pub license_name: String,
    /// Short description of the license (to put on every generated file)
    pub license_description: Vec<String>,
    /// Path to the license file from the root directory
    pub license_path: PathBuf,
    /// List of developers
    pub developers: Vec<DeveloperInfo>,
    /// Logo of the company (in PNG)
    ///
    /// Use `include_bytes` to import the data
    pub logo_png: &'static [u8],
}

/// Settings that affect iterator function naming
#[derive(Debug)]
pub struct IteratorSettings {
    /// name of the C function which retrieve's the iterator's next value
    /// is automatically generated as `<c_ffi_prefix>_<iterator_class_name>_<next_function_suffix>`
    pub next_function_suffix: Name,
}

impl IteratorSettings {
    pub fn new(next_function_suffix: Name) -> IteratorSettings {
        Self {
            next_function_suffix,
        }
    }
}

impl Default for IteratorSettings {
    fn default() -> Self {
        Self {
            next_function_suffix: Name::create("next").unwrap(),
        }
    }
}

/// Settings that affect C interface member naming
#[derive(Debug)]
pub struct InterfaceSettings {
    /// Name of the C void* context variable, defaults to "ctx"
    pub context_variable_name: Name,
    /// Name of the function that destroys an interface when it is dropped, defaults to "on_destroy"
    pub destroy_func_name: Name,
}

impl InterfaceSettings {
    pub fn new(context_variable_name: Name, destroy_func_name: Name) -> Self {
        Self {
            context_variable_name,
            destroy_func_name,
        }
    }
}

impl Default for InterfaceSettings {
    fn default() -> Self {
        Self {
            context_variable_name: Name::create("ctx").unwrap(),
            destroy_func_name: Name::create("on_destroy").unwrap(),
        }
    }
}

/// Settings that affect class method naming
#[derive(Debug)]
pub struct ClassSettings {
    /// Methods in C always take an instance of the class at the first parameter.
    /// This setting controls the name automatically assigned to this parameter.
    ///
    /// This value defaults to "instance"
    pub method_instance_argument_name: Name,
    /// suffix for C destructors.
    /// The full C function name is automatically generated as `<c_ffi_prefix>_<class_name>_<class_destructor_suffix>`
    ///
    /// This value defaults to 'destroy'
    pub class_destructor_suffix: Name,
    /// suffix for C constructors.
    /// The full C function name is automatically generated as `<c_ffi_prefix>_<class_name>_<class_constructor_suffix>`
    ///
    /// This value defaults to 'new'
    pub class_constructor_suffix: Name,
}

impl ClassSettings {
    pub fn new(
        method_instance_argument_name: Name,
        class_destructor_suffix: Name,
        class_constructor_suffix: Name,
    ) -> Self {
        Self {
            method_instance_argument_name,
            class_destructor_suffix,
            class_constructor_suffix,
        }
    }
}

impl Default for ClassSettings {
    fn default() -> ClassSettings {
        Self {
            method_instance_argument_name: Name::create("instance").unwrap(),
            class_destructor_suffix: Name::create("destroy").unwrap(),
            class_constructor_suffix: Name::create("create").unwrap(),
        }
    }
}

/// Settings that affect how things are named in future-style callback interfaces
#[derive(Debug)]
pub struct FutureSettings {
    /// The name given to the success completion method on interface
    pub success_callback_method_name: Name,
    /// The name given to the result parameter of the success completion method
    pub success_single_parameter_name: Name,
    /// The name given to the failure completion method on interface
    pub failure_callback_method_name: Name,
    /// The name given to the error parameter of the failure completion method
    pub failure_single_parameter_name: Name,
    /// The name given to the final callback parameter of the async methods
    pub async_method_callback_parameter_name: Name,
}

impl FutureSettings {
    pub fn new(
        success_callback_method_name: Name,
        success_single_parameter_name: Name,
        failure_callback_method_name: Name,
        failure_single_parameter_name: Name,
        async_method_callback_parameter_name: Name,
    ) -> Self {
        Self {
            success_callback_method_name,
            success_single_parameter_name,
            failure_callback_method_name,
            failure_single_parameter_name,
            async_method_callback_parameter_name,
        }
    }
}

impl Default for FutureSettings {
    fn default() -> Self {
        Self {
            success_callback_method_name: Name::create("on_complete").unwrap(),
            success_single_parameter_name: Name::create("result").unwrap(),
            failure_callback_method_name: Name::create("on_failure").unwrap(),
            failure_single_parameter_name: Name::create("error").unwrap(),
            async_method_callback_parameter_name: Name::create("callback").unwrap(),
        }
    }
}

/// Settings that affect collection function naming
#[derive(Debug)]
pub struct CollectionSettings {
    /// name of the C function which creates a collection
    /// is automatically generated as `<c_ffi_prefix>_<collection_class_name>_<create_function_suffix>`
    pub create_function_suffix: Name,
    /// name of the C function which creates a collection
    /// is automatically generated as `<c_ffi_prefix>_<collection_class_name>_<add_function_suffix>`
    pub add_function_suffix: Name,
    /// name of the C function which destroys a collection
    /// is automatically generated as `<c_ffi_prefix>_<collection_class_name>_<destroy_function_suffix>`
    pub destroy_function_suffix: Name,
}

impl CollectionSettings {
    pub fn new(
        create_function_suffix: Name,
        add_function_suffix: Name,
        destroy_function_suffix: Name,
    ) -> Self {
        Self {
            create_function_suffix,
            add_function_suffix,
            destroy_function_suffix,
        }
    }
}

impl Default for CollectionSettings {
    fn default() -> CollectionSettings {
        Self {
            create_function_suffix: Name::create("create").unwrap(),
            add_function_suffix: Name::create("add").unwrap(),
            destroy_function_suffix: Name::create("destroy").unwrap(),
        }
    }
}

/// Settings that affect the names of things
#[derive(Debug)]
pub struct LibrarySettings {
    /// name of the library
    pub name: Name,
    /// prefix given to all API types, e.g. structs, enums, functions, etc
    pub c_ffi_prefix: Name,
    /// settings that control class generation
    pub class: ClassSettings,
    /// settings that control iterator generation
    pub iterator: IteratorSettings,
    /// settings that control collection generation
    pub collection: CollectionSettings,
    /// settings that control future-style interface generation
    pub future: FutureSettings,
    /// settings that control C interface member naming
    pub interface: InterfaceSettings,
}

impl LibrarySettings {
    /// create an RC to the settings that is cheaply cloned
    pub fn create<S: IntoName, R: IntoName>(
        name: S,
        c_ffi_prefix: R,
        class: ClassSettings,
        iterator: IteratorSettings,
        collection: CollectionSettings,
        future: FutureSettings,
        interface: InterfaceSettings,
    ) -> BindResult<Rc<Self>> {
        Ok(Rc::new(Self {
            name: name.into_name()?,
            c_ffi_prefix: c_ffi_prefix.into_name()?,
            class,
            iterator,
            collection,
            future,
            interface,
        }))
    }
}

pub struct Library {
    pub(crate) version: Version,
    pub(crate) info: Rc<LibraryInfo>,
    pub(crate) settings: Rc<LibrarySettings>,
    /// history of statements from which we can find other types
    statements: Vec<Statement<Validated>>,
}

impl Library {
    pub(crate) fn new(
        version: Version,
        info: Rc<LibraryInfo>,
        settings: Rc<LibrarySettings>,
        statements: Vec<Statement<Validated>>,
    ) -> Self {
        Self {
            version,
            info,
            settings,
            statements,
        }
    }

    pub(crate) fn statements(&self) -> impl Iterator<Item = &Statement<Validated>> {
        self.statements.iter()
    }

    pub(crate) fn functions(&self) -> impl Iterator<Item = &Handle<Function<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::FunctionDefinition(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn structs(&self) -> impl Iterator<Item = &StructType<Validated>> {
        self.statements
            .iter()
            .filter_map(|statement| match statement {
                Statement::StructDefinition(x) => Some(x),
                _ => None,
            })
    }

    pub(crate) fn constants(&self) -> impl Iterator<Item = &Handle<ConstantSet<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::Constants(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn enums(&self) -> impl Iterator<Item = &Handle<Enum<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::EnumDefinition(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn classes(&self) -> impl Iterator<Item = &Handle<Class<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::ClassDefinition(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn error_types(&self) -> impl Iterator<Item = &ErrorType<Validated>> {
        self.statements().filter_map(|statement| match statement {
            Statement::ErrorType(err) => Some(err),
            _ => None,
        })
    }

    pub(crate) fn static_classes(&self) -> impl Iterator<Item = &Handle<StaticClass<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::StaticClassDefinition(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn untyped_interfaces(&self) -> impl Iterator<Item = &Handle<Interface<Validated>>> {
        self.interfaces().map(|x| x.untyped())
    }

    pub(crate) fn interfaces(&self) -> impl Iterator<Item = &InterfaceType<Validated>> {
        self.statements
            .iter()
            .filter_map(|statement| match statement {
                Statement::InterfaceDefinition(t) => Some(t),
                _ => None,
            })
    }

    pub(crate) fn iterators(&self) -> impl Iterator<Item = &Handle<AbstractIterator<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::IteratorDeclaration(handle) => Some(handle),
            _ => None,
        })
    }

    pub(crate) fn collections(&self) -> impl Iterator<Item = &Handle<Collection<Validated>>> {
        self.statements().filter_map(|statement| match statement {
            Statement::CollectionDeclaration(handle) => Some(handle),
            _ => None,
        })
    }
}

impl From<UniversalStructDeclaration> for FunctionReturnStructDeclaration {
    fn from(x: UniversalStructDeclaration) -> Self {
        FunctionReturnStructDeclaration::new(x.inner)
    }
}