Debian Codegraph @

third_party/rust/uniffi_bindgen/src/macro_metadata/ci.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use crate::interface::{CallbackInterface, ComponentInterface, Record, Type};
6 use anyhow::{bail, Context};
7 use uniffi_meta::{create_metadata_groups, group_metadata, EnumMetadata, Metadata, MetadataGroup};
8
9 /// Add Metadata items to the ComponentInterface
10 ///
11 /// This function exists to support the transition period where the `uniffi::export` macro can only
12 /// handle some components. This means that crates need to continue using UDL files to define the
13 /// parts of the components that aren't supported yet.
14 ///
15 /// To make things work, we generate a `ComponentInterface` from the UDL file, then combine it with
16 /// the `Metadata` items that the macro creates.
17 pub fn add_to_ci(
18 iface: &mut ComponentInterface,
19 metadata_items: Vec<Metadata>,
20 ) -> anyhow::Result<()> {
21 let mut group_map = create_metadata_groups(&metadata_items);
22 group_metadata(&mut group_map, metadata_items)?;
23 for group in group_map.into_values() {
24 if group.items.is_empty() {
25 continue;
26 }
27 if group.namespace.name != iface.namespace() {
28 let crate_name = group.namespace.crate_name;
29 bail!("Found metadata items from crate `{crate_name}`. Use the `--library` to generate bindings for multiple crates")
30 }
31 add_group_to_ci(iface, group)?;
32 }
33
34 Ok(())
35 }
36
37 /// Add items from a MetadataGroup to a component interface
38 pub fn add_group_to_ci(iface: &mut ComponentInterface, group: MetadataGroup) -> anyhow::Result<()> {
39 if group.namespace.name != iface.namespace() {
40 bail!(
41 "Namespace mismatch: {} - {}",
42 group.namespace.name,
43 iface.namespace()
44 );
45 }
46
47 for item in group.items {
48 add_item_to_ci(iface, item)?
49 }
50
51 iface
52 .derive_ffi_funcs()
53 .context("Failed to derive FFI functions")?;
54 iface
55 .check_consistency()
56 .context("ComponentInterface consistency error")?;
57 Ok(())
58 }
59
60 fn add_enum_to_ci(iface: &mut ComponentInterface, meta: EnumMetadata) -> anyhow::Result<()> {
61 let ty = Type::Enum {
62 name: meta.name.clone(),
63 module_path: meta.module_path.clone(),
64 };
65 iface.types.add_known_type(&ty)?;
66 iface.add_enum_definition(meta.try_into()?)?;
67 Ok(())
68 }
69
70 fn add_item_to_ci(iface: &mut ComponentInterface, item: Metadata) -> anyhow::Result<()> {
71 match item {
72 Metadata::Namespace(_) => unreachable!(),
73 Metadata::UdlFile(_) => (),
74 Metadata::Func(meta) => {
75 iface.add_function_definition(meta.into())?;
76 }
77 Metadata::Constructor(meta) => {
78 iface.add_constructor_meta(meta)?;
79 }
80 Metadata::Method(meta) => {
81 iface.add_method_meta(meta)?;
82 }
83 Metadata::Record(meta) => {
84 let ty = Type::Record {
85 name: meta.name.clone(),
86 module_path: meta.module_path.clone(),
87 };
88 iface.types.add_known_type(&ty)?;
89 let record: Record = meta.try_into()?;
90 iface.add_record_definition(record)?;
91 }
92 Metadata::Enum(meta) => {
93 add_enum_to_ci(iface, meta)?;
94 }
95 Metadata::Object(meta) => {
96 iface.types.add_known_type(&Type::Object {
97 module_path: meta.module_path.clone(),
98 name: meta.name.clone(),
99 imp: meta.imp,
100 })?;
101 iface.add_object_meta(meta)?;
102 }
103 Metadata::UniffiTrait(meta) => {
104 iface.add_uniffitrait_meta(meta)?;
105 }
106 Metadata::ObjectTraitImpl(meta) => {
107 iface.add_object_trait_impl(meta)?;
108 }
109 Metadata::CallbackInterface(meta) => {
110 iface.add_callback_interface_definition(CallbackInterface::try_from(meta)?)?;
111 }
112 Metadata::TraitMethod(meta) => {
113 iface.add_trait_method_meta(meta)?;
114 }
115 Metadata::CustomType(meta) => {
116 iface.types.add_known_type(&Type::Custom {
117 module_path: meta.module_path.clone(),
118 name: meta.name,
119 builtin: Box::new(meta.builtin),
120 })?;
121 }
122 }
123 Ok(())
124 }