diff --git a/uniffi_bindgen/src/lib.rs b/uniffi_bindgen/src/lib.rs index a248a617c2..6d32bd4784 100644 --- a/uniffi_bindgen/src/lib.rs +++ b/uniffi_bindgen/src/lib.rs @@ -115,6 +115,7 @@ use crate::interface::{ Variant, }; pub use interface::ComponentInterface; +pub use library_mode::find_components; use scaffolding::RustScaffolding; use uniffi_meta::Type; @@ -297,7 +298,8 @@ pub fn generate_external_bindings( let config = { let crate_config = load_toml_file(Some(&crate_root.join("uniffi.toml"))) .context("failed to load {crate_root}/uniffi.toml")?; - let toml_value = overridden_config_value(crate_config, config_file_override)?; + let toml_value = + overridden_config_value(crate_config.unwrap_or_default(), config_file_override)?; binding_generator.new_config(&toml_value)? }; @@ -497,10 +499,9 @@ fn load_toml_file(source: Option<&Utf8Path>) -> Result, + mut config: toml::value::Table, config_file_override: Option<&Utf8Path>, ) -> Result { - let mut config = config.unwrap_or_default(); let override_config = load_toml_file(config_file_override).context("override config")?; if let Some(override_config) = override_config { merge_toml(&mut config, override_config); diff --git a/uniffi_bindgen/src/library_mode.rs b/uniffi_bindgen/src/library_mode.rs index b8e919bfbf..3a6a3f0133 100644 --- a/uniffi_bindgen/src/library_mode.rs +++ b/uniffi_bindgen/src/library_mode.rs @@ -22,17 +22,18 @@ use crate::{ use anyhow::bail; use camino::Utf8Path; use std::{collections::HashMap, fs}; +use toml::value::Table as TomlTable; use uniffi_meta::{ create_metadata_groups, fixup_external_type, group_metadata, Metadata, MetadataGroup, }; /// Generate foreign bindings /// +/// This replicates the current process used for generating the builtin bindings. +/// External bindings authors should consider using [find_components], which provides a simpler +/// interface and allows for more flexibility in how the external bindings are generated. +/// /// Returns the list of sources used to generate the bindings, in no particular order. -// XXX - we should consider killing this function and replace it with a function -// which just locates the `Components` and returns them, leaving the filtering -// and actual generation to the callers, which also would allow removing the potentially -// confusing crate_name param. pub fn generate_bindings( library_path: &Utf8Path, crate_name: Option, @@ -42,11 +43,10 @@ pub fn generate_bindings( out_dir: &Utf8Path, try_format_code: bool, ) -> Result>> { - let mut components = find_components(config_supplier, library_path)? + let mut components = find_components(library_path, config_supplier)? .into_iter() - .map(|ci| { - let crate_toml = config_supplier.get_toml(ci.crate_name())?; - let toml_value = overridden_config_value(crate_toml, config_file_override)?; + .map(|Component { ci, config }| { + let toml_value = overridden_config_value(config, config_file_override)?; let config = binding_generator.new_config(&toml_value)?; Ok(Component { ci, config }) }) @@ -90,10 +90,17 @@ pub fn calc_cdylib_name(library_path: &Utf8Path) -> Option<&str> { None } -fn find_components( - config_supplier: &dyn BindgenCrateConfigSupplier, +/// Find UniFFI components from a shared library file +/// +/// This method inspects the library file and creates [ComponentInterface] instances for each +/// component used to build it. It parses the UDL files from `uniffi::include_scaffolding!` macro +/// calls. +/// +/// `config_supplier` is used to find UDL files on disk and load config data. +pub fn find_components( library_path: &Utf8Path, -) -> Result> { + config_supplier: &dyn BindgenCrateConfigSupplier, +) -> Result>> { let items = macro_metadata::extract_from_library(library_path)?; let mut metadata_groups = create_metadata_groups(&items); group_metadata(&mut metadata_groups, items)?; @@ -128,7 +135,10 @@ fn find_components( ci.add_metadata(metadata)?; }; ci.add_metadata(group)?; - Ok(ci) + let config = config_supplier + .get_toml(ci.crate_name())? + .unwrap_or_default(); + Ok(Component { ci, config }) }) .collect() }