uniffi_bindgen 0.31.2

a multi-language bindings generator for rust (codegen and cli tooling)
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Organize the metadata, transforming it from a simple list to a more tree-like structure.

use anyhow::{anyhow, bail, Result};
use std::collections::{btree_map::Entry, BTreeMap};

use super::nodes::*;
use uniffi_pipeline::Node;

/// Converts `uniffi_meta` items into the initial IR.
///
/// Usage:
/// * Call [Self::add_metadata_item] with all `uniffi_meta` items in the interface (also [Self::add_module_docstring])
/// * Call [Self::try_into_initial_ir] to construct a `initial::Root` node.
#[derive(Default)]
pub struct UniffiMetaConverter {
    // Use BTreeMap for each of these so that things stay consistent, regardless of how the metadata is ordered.
    // There are 2 important names here we must not mix up. The namespace name and the rust crate name.
    // Our metadata usually carries "module_path" reflecting the crate name.
    // This maps module_paths to namespace names.
    module_path_map: BTreeMap<String, String>,
    // Everything below here keyed by namespace name, not the module_path.
    // Top level modules
    namespaces: BTreeMap<String, Namespace>,
    // Top-level type definitions and functions, keyed by module name
    module_docstrings: BTreeMap<String, String>,
    module_toml: BTreeMap<String, toml::Table>,
    functions: BTreeMap<String, BTreeMap<String, Function>>,
    records: BTreeMap<String, BTreeMap<String, Record>>,
    callback_interfaces: BTreeMap<String, BTreeMap<String, CallbackInterface>>,
    enums: BTreeMap<String, BTreeMap<String, Enum>>,
    custom_types: BTreeMap<String, BTreeMap<String, CustomType>>,
    interfaces: BTreeMap<String, BTreeMap<String, Interface>>,
    // Child items, keyed by module name + parent name
    constructors: BTreeMap<(String, String), BTreeMap<String, Constructor>>,
    methods: BTreeMap<(String, String), BTreeMap<String, Method>>,
    trait_methods: BTreeMap<(String, String), BTreeMap<String, TraitMethod>>,
    uniffi_traits: BTreeMap<(String, String), BTreeMap<String, UniffiTrait>>,
    trait_impls: BTreeMap<(String, String), BTreeMap<uniffi_meta::Type, ObjectTraitImpl>>,
}

/// Utility trait used to insert metadata items into a BTreeMap, but bail on duplicates
trait InsertUnique<K, V> {
    fn insert_unique(&mut self, k: K, v: V) -> Result<()>;
}

impl<K, V> InsertUnique<K, V> for BTreeMap<K, V>
where
    K: std::fmt::Debug + Ord,
    V: std::fmt::Debug + PartialEq,
{
    fn insert_unique(&mut self, k: K, v: V) -> Result<()> {
        match self.entry(k) {
            Entry::Vacant(e) => {
                e.insert(v);
                Ok(())
            }
            Entry::Occupied(e) => {
                if e.get() != &v {
                    bail!(
                        "Conflicting metadata types:\nold: {:?}\nnew: {v:?}",
                        e.get()
                    );
                }
                Ok(())
            }
        }
    }
}

impl UniffiMetaConverter {
    /// Add a [uniffi_meta::Metadata] item to be converted
    pub fn add_metadata_item(&mut self, meta: uniffi_meta::Metadata) -> Result<()> {
        match meta {
            uniffi_meta::Metadata::Namespace(namespace) => {
                self.module_path_map
                    .insert(namespace.crate_name.clone(), namespace.name.clone());
                // Insert a new module
                self.namespaces.insert_unique(
                    namespace.name.clone(),
                    Namespace {
                        crate_name: namespace.crate_name,
                        docstring: None,
                        config_toml: None,
                        name: namespace.name,
                        functions: vec![],
                        type_definitions: vec![],
                    },
                )?;
            }
            uniffi_meta::Metadata::Func(func) => {
                self.functions
                    .entry(module_path_to_namespace(&func.module_path))
                    .or_default()
                    .insert_unique(func.name.clone(), Function::try_from_node(func)?)?;
            }
            uniffi_meta::Metadata::Record(rec) => {
                self.records
                    .entry(module_path_to_namespace(&rec.module_path))
                    .or_default()
                    .insert_unique(rec.name.clone(), Record::try_from_node(rec)?)?;
            }
            uniffi_meta::Metadata::Enum(en) => {
                self.enums
                    .entry(module_path_to_namespace(&en.module_path))
                    .or_default()
                    .insert_unique(en.name.clone(), Enum::try_from_node(en)?)?;
            }
            uniffi_meta::Metadata::Object(int) => {
                self.interfaces
                    .entry(module_path_to_namespace(&int.module_path))
                    .or_default()
                    .insert_unique(int.name.clone(), Interface::try_from_node(int)?)?;
            }
            uniffi_meta::Metadata::CallbackInterface(cbi) => {
                self.callback_interfaces
                    .entry(module_path_to_namespace(&cbi.module_path))
                    .or_default()
                    .insert_unique(cbi.name.clone(), CallbackInterface::try_from_node(cbi)?)?;
            }
            uniffi_meta::Metadata::CustomType(custom) => {
                self.custom_types
                    .entry(module_path_to_namespace(&custom.module_path))
                    .or_default()
                    .insert_unique(custom.name.clone(), CustomType::try_from_node(custom)?)?;
            }
            uniffi_meta::Metadata::Constructor(cons) => {
                self.constructors
                    .entry((
                        module_path_to_namespace(&cons.module_path),
                        cons.self_name.to_string(),
                    ))
                    .or_default()
                    .insert_unique(cons.name.clone(), Constructor::try_from_node(cons)?)?;
            }
            uniffi_meta::Metadata::Method(meth) => {
                self.methods
                    .entry((
                        module_path_to_namespace(&meth.module_path),
                        meth.self_name.to_string(),
                    ))
                    .or_default()
                    .insert_unique(meth.name.clone(), Method::try_from_node(meth)?)?;
            }
            uniffi_meta::Metadata::TraitMethod(meth) => {
                self.trait_methods
                    .entry((
                        module_path_to_namespace(&meth.module_path),
                        meth.trait_name.to_string(),
                    ))
                    .or_default()
                    .insert_unique(meth.name.clone(), TraitMethod::try_from_node(meth)?)?;
            }
            uniffi_meta::Metadata::UniffiTrait(ut) => {
                let meth = match &ut {
                    uniffi_meta::UniffiTraitMetadata::Debug { fmt } => fmt,
                    uniffi_meta::UniffiTraitMetadata::Display { fmt } => fmt,
                    uniffi_meta::UniffiTraitMetadata::Eq { eq, .. } => eq,
                    uniffi_meta::UniffiTraitMetadata::Hash { hash } => hash,
                    uniffi_meta::UniffiTraitMetadata::Ord { cmp } => cmp,
                };

                self.uniffi_traits
                    .entry((
                        module_path_to_namespace(&meth.module_path),
                        meth.self_name.to_string(),
                    ))
                    .or_default()
                    .insert_unique(ut.name().to_string(), UniffiTrait::try_from_node(ut)?)?;
            }
            uniffi_meta::Metadata::ObjectTraitImpl(imp) => {
                let (module_path, name) = match &imp.ty {
                    uniffi_meta::Type::Object {
                        module_path, name, ..
                    }
                    | uniffi_meta::Type::Record {
                        module_path, name, ..
                    }
                    | uniffi_meta::Type::Enum {
                        module_path, name, ..
                    }
                    | uniffi_meta::Type::Custom {
                        module_path, name, ..
                    }
                    | uniffi_meta::Type::CallbackInterface {
                        module_path, name, ..
                    } => (module_path, name),
                    _ => bail!("Invalid ObjectTraitImpl type: {:?}", imp.ty),
                };
                self.trait_impls
                    .entry((module_path_to_namespace(module_path), name.to_string()))
                    .or_default()
                    .insert_unique(imp.trait_ty.clone(), ObjectTraitImpl::try_from_node(imp)?)?;
            }
            uniffi_meta::Metadata::UdlFile(_) => (),
        }
        Ok(())
    }

    pub fn add_module_config_toml(
        &mut self,
        module_name: String,
        table: toml::Table,
    ) -> Result<()> {
        self.module_toml.insert_unique(module_name, table)?;
        Ok(())
    }

    /// Add a docstring for a module,
    ///
    /// This is currently UDL-specific.  Eventually, we should probably make this another metadata
    /// items
    pub fn add_module_docstring(&mut self, namespace: String, docstring: String) -> Result<()> {
        self.module_docstrings.insert_unique(namespace, docstring)
    }

    pub fn try_into_initial_ir(mut self) -> Result<Root> {
        let mut root = Root {
            namespaces: self.namespaces.into_iter().collect(),
            cdylib: None,
        };

        // Move child items into their parents
        for (namespace_name, docstring) in self.module_docstrings {
            // already the namespace name, so no need to convert.
            let namespace = root.namespaces.get_mut(&namespace_name).ok_or_else(|| {
                anyhow!("namespace specified in toml doesn't exist: {namespace_name:?}")
            })?;
            namespace.docstring = Some(docstring);
        }
        for (namespace_name, table) in self.module_toml {
            // already the namespace name, so no need to convert.
            // we should maybe ignore an error here?
            let namespace = root.namespaces.get_mut(&namespace_name).ok_or_else(|| {
                anyhow!("namespace specified in toml doesn't exist: {namespace_name:?}")
            })?;
            // ideally `namespace.config_toml` would be a `toml::Table`, but all members must implement `Node`.
            namespace.config_toml = Some(toml::to_string(&table)?);
        }
        for (module_path, funcs) in self.functions {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .functions
                .extend(funcs.into_values());
        }
        for (module_path, list) in self.records {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .type_definitions
                .extend(
                    list.into_values()
                        .map(|mut r| {
                            let key = (module_path.clone(), r.name.clone());
                            if let Some(methods) = self.methods.remove(&key) {
                                r.methods.extend(methods.into_values());
                            }
                            if let Some(constructors) = self.constructors.remove(&key) {
                                r.constructors.extend(constructors.into_values())
                            }
                            if let Some(uniffi_traits) = self.uniffi_traits.remove(&key) {
                                r.uniffi_traits.extend(uniffi_traits.into_values())
                            }
                            Ok(TypeDefinition::Record(r))
                        })
                        .collect::<Result<Vec<_>>>()?,
                )
        }
        for (module_path, list) in self.enums {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .type_definitions
                .extend(
                    list.into_values()
                        .map(|mut e| {
                            let key = (module_path.clone(), e.name.clone());
                            if let Some(methods) = self.methods.remove(&key) {
                                e.methods.extend(methods.into_values());
                            }
                            if let Some(constructors) = self.constructors.remove(&key) {
                                e.constructors.extend(constructors.into_values())
                            }
                            if let Some(uniffi_traits) = self.uniffi_traits.remove(&key) {
                                e.uniffi_traits.extend(uniffi_traits.into_values())
                            }
                            Ok(TypeDefinition::Enum(e))
                        })
                        .collect::<Result<Vec<_>>>()?,
                )
        }
        for (module_path, list) in self.custom_types {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .type_definitions
                .extend(list.into_values().map(TypeDefinition::Custom));
        }
        // Collect child items for interfaces and callback interfaces
        for (module_path, list) in self.interfaces {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .type_definitions
                .extend(
                    list.into_values()
                        .map(|mut int| {
                            let key = (module_path.clone(), int.name.clone());
                            if let Some(methods) = self.methods.remove(&key) {
                                if self.trait_methods.contains_key(&key) {
                                    // Trait methods have an explicit index, so mixing them with
                                    // regular methods won't work.
                                    bail!("{} contains both methods and trait methods", int.name)
                                }
                                int.methods.extend(methods.into_values());
                            } else if let Some(trait_methods) = self.trait_methods.remove(&key) {
                                int.methods.extend(Self::convert_trait_methods(
                                    trait_methods.into_values().collect(),
                                ));
                            }
                            if let Some(constructors) = self.constructors.remove(&key) {
                                int.constructors.extend(constructors.into_values())
                            }
                            if let Some(uniffi_traits) = self.uniffi_traits.remove(&key) {
                                int.uniffi_traits.extend(uniffi_traits.into_values())
                            }
                            if let Some(trait_impls) = self.trait_impls.remove(&key) {
                                int.trait_impls.extend(trait_impls.into_values())
                            }
                            Ok(TypeDefinition::Interface(int))
                        })
                        .collect::<Result<Vec<_>>>()?,
                )
        }
        for (module_path, list) in self.callback_interfaces {
            get_namespace(&self.module_path_map, &mut root, &module_path)?
                .type_definitions
                .extend(list.into_values().map(|mut cbi| {
                    let key = (module_path.clone(), cbi.name.clone());
                    if let Some(trait_methods) = self.trait_methods.remove(&key) {
                        cbi.methods.extend(Self::convert_trait_methods(
                            trait_methods.into_values().collect(),
                        ));
                    }
                    TypeDefinition::CallbackInterface(cbi)
                }))
        }
        if !self.constructors.is_empty() {
            bail!("Leftover constructors: {:?}", self.constructors)
        }
        if !self.methods.is_empty() {
            bail!("Leftover methods: {:?}", self.methods)
        }
        if !self.trait_methods.is_empty() {
            bail!("Leftover trait_methods: {:?}", self.trait_methods)
        }
        if !self.uniffi_traits.is_empty() {
            bail!("Leftover uniffi_traits: {:?}", self.uniffi_traits)
        }
        if !self.trait_impls.is_empty() {
            bail!("Leftover trait_impls: {:?}", self.trait_impls)
        }
        // set the namespace names
        root.try_visit_mut(|ty: &mut Type| match ty {
            Type::Interface {
                module_path,
                namespace,
                ..
            }
            | Type::Record {
                module_path,
                namespace,
                ..
            }
            | Type::Enum {
                module_path,
                namespace,
                ..
            }
            | Type::CallbackInterface {
                module_path,
                namespace,
                ..
            }
            | Type::Custom {
                module_path,
                namespace,
                ..
            } => {
                *namespace = get_namespace_name(&self.module_path_map, module_path)?.to_string();
                Ok(())
            }
            _ => Ok(()),
        })?;
        Ok(root)
    }

    fn convert_trait_methods(mut trait_methods: Vec<TraitMethod>) -> impl Iterator<Item = Method> {
        trait_methods.sort_by_key(|tm| tm.index);
        trait_methods.into_iter().map(Self::convert_trait_method)
    }

    fn convert_trait_method(trait_method: TraitMethod) -> Method {
        Method {
            name: trait_method.name,
            is_async: trait_method.is_async,
            inputs: trait_method.inputs,
            return_type: trait_method.return_type,
            throws: trait_method.throws,
            checksum: trait_method.checksum,
            docstring: trait_method.docstring,
        }
    }
}

fn module_path_to_namespace(module_path: &str) -> String {
    module_path.split("::").next().unwrap().to_string()
}

fn get_namespace_name<'a>(
    module_path_map: &'a BTreeMap<String, String>,
    module_path: &str,
) -> Result<&'a str> {
    let crate_name = module_path.split("::").next().unwrap();
    module_path_map
        .get(crate_name)
        .map(String::as_str)
        .ok_or_else(|| anyhow!("module lookup failed: {module_path:?}"))
}

fn get_namespace<'a>(
    module_path_map: &BTreeMap<String, String>,
    root: &'a mut Root,
    module_path: &str,
) -> Result<&'a mut Namespace> {
    let name = get_namespace_name(module_path_map, module_path)?;
    root.namespaces
        .get_mut(name)
        .ok_or_else(|| anyhow!("root module lookup failed: {module_path:?}"))
}