@@ -15,8 +15,8 @@ use uniffi_meta::{
1515} ;
1616
1717use crate :: {
18- crate_name_from_cargo_toml, macro_metadata, BindgenCrateConfigSupplier , Component ,
19- ComponentInterface , Result ,
18+ crate_name_from_cargo_toml, interface , macro_metadata, pipeline , BindgenCrateConfigSupplier ,
19+ Component , ComponentInterface , Result ,
2020} ;
2121
2222/// Load metadata, component interfaces, configuration, etc. for binding generators.
@@ -61,7 +61,9 @@ impl<'config> BindgenLoader<'config> {
6161 match source_path. extension ( ) {
6262 Some ( ext) if ext. to_lowercase ( ) == "udl" => {
6363 let crate_name = crate_name_from_cargo_toml ( source_path) ?;
64- let group = uniffi_udl:: parse_udl ( & fs:: read_to_string ( source_path) ?, & crate_name) ?;
64+ let mut group =
65+ uniffi_udl:: parse_udl ( & fs:: read_to_string ( source_path) ?, & crate_name) ?;
66+ Self :: add_checksums ( & mut group) ;
6567 Ok ( HashMap :: from ( [ ( crate_name, group) ] ) )
6668 }
6769 _ => {
@@ -88,6 +90,39 @@ impl<'config> BindgenLoader<'config> {
8890 }
8991 }
9092
93+ /// Add checksums for metadata parsed from UDL
94+ fn add_checksums ( metadata_group : & mut MetadataGroup ) {
95+ // Need to temporarily take items BTree set, since we're technically mutating it's contents
96+ // Each item will be added back at the bottom of the for loop.
97+ let items = std:: mem:: take ( & mut metadata_group. items ) ;
98+ for mut meta in items {
99+ // Make sure metadata checksums are set
100+ match & mut meta {
101+ Metadata :: Func ( func) => {
102+ func. checksum = Some ( uniffi_meta:: checksum ( & interface:: Function :: from (
103+ func. clone ( ) ,
104+ ) ) ) ;
105+ }
106+ Metadata :: Method ( meth) => {
107+ // making a method is mildly tricky as we need a type for self.
108+ // for the purposes of a checksum we ignore self info from udl.
109+ let method_object =
110+ interface:: Method :: from_metadata ( meth. clone ( ) , uniffi_meta:: Type :: UInt8 ) ;
111+ meth. checksum = Some ( uniffi_meta:: checksum ( & method_object) ) ;
112+ }
113+ Metadata :: Constructor ( cons) => {
114+ cons. checksum = Some ( uniffi_meta:: checksum ( & interface:: Constructor :: from (
115+ cons. clone ( ) ,
116+ ) ) ) ;
117+ }
118+ // Note: UDL-based callbacks don't have checksum functions, don't set the
119+ // checksum for those.
120+ _ => ( ) ,
121+ }
122+ metadata_group. items . insert ( meta) ;
123+ }
124+ }
125+
91126 /// Load a [ComponentInterface] list
92127 ///
93128 /// This converts the metadata into `ComponentInterface` instances, which contains additional
@@ -182,6 +217,25 @@ impl<'config> BindgenLoader<'config> {
182217 . collect ( )
183218 }
184219
220+ /// Load a [pipeline::initial::Root] value from the metadata
221+ pub fn load_pipeline_initial_root (
222+ & self ,
223+ metadata : MetadataGroupMap ,
224+ ) -> Result < pipeline:: initial:: Root > {
225+ let mut metadata_converter = pipeline:: initial:: UniffiMetaConverter :: default ( ) ;
226+ for metadata_group in metadata. into_values ( ) {
227+ if let Some ( docstring) = metadata_group. namespace_docstring {
228+ metadata_converter
229+ . add_module_docstring ( metadata_group. namespace . name . clone ( ) , docstring) ;
230+ }
231+ metadata_converter. add_metadata_item ( Metadata :: Namespace ( metadata_group. namespace ) ) ?;
232+ for meta in metadata_group. items {
233+ metadata_converter. add_metadata_item ( meta) ?;
234+ }
235+ }
236+ metadata_converter. try_into_initial_ir ( )
237+ }
238+
185239 /// Get the basename for a source file
186240 ///
187241 /// This will remove any file extension.
@@ -206,4 +260,8 @@ impl<'config> BindgenLoader<'config> {
206260 Some ( ext) if ext. to_lowercase( ) == "udl"
207261 )
208262 }
263+
264+ pub fn filter_metadata_by_crate_name ( & self , metadata : & mut MetadataGroupMap , crate_name : & str ) {
265+ metadata. retain ( |_, metadata_group| metadata_group. namespace . crate_name == crate_name)
266+ }
209267}
0 commit comments