diff --git a/crates/ubrn_bindgen/src/bindings/gen_cpp/mod.rs b/crates/ubrn_bindgen/src/bindings/gen_cpp/mod.rs index 12c58d11..b1a0a611 100644 --- a/crates/ubrn_bindgen/src/bindings/gen_cpp/mod.rs +++ b/crates/ubrn_bindgen/src/bindings/gen_cpp/mod.rs @@ -23,20 +23,20 @@ use crate::bindings::{ metadata::ModuleMetadata, }; -#[derive(Debug, Default)] -pub(crate) struct CppBindings { - pub(crate) hpp: String, - pub(crate) cpp: String, +pub(crate) fn generate_hpp( + ci: &ComponentInterface, + config: &Config, + module: &ModuleMetadata, +) -> Result { + Ok(HppWrapper::new(ci, config, module).render()?) } -pub(crate) fn generate_bindings( +pub(crate) fn generate_cpp( ci: &ComponentInterface, config: &Config, module: &ModuleMetadata, -) -> Result { - let hpp = HppWrapper::new(ci, config, module).render()?; - let cpp = CppWrapper::new(ci, config, module).render()?; - Ok(CppBindings { hpp, cpp }) +) -> Result { + Ok(CppWrapper::new(ci, config, module).render()?) } #[derive(Template)] diff --git a/crates/ubrn_bindgen/src/bindings/gen_typescript/mod.rs b/crates/ubrn_bindgen/src/bindings/gen_typescript/mod.rs index 3f90bb2e..8a396765 100644 --- a/crates/ubrn_bindgen/src/bindings/gen_typescript/mod.rs +++ b/crates/ubrn_bindgen/src/bindings/gen_typescript/mod.rs @@ -32,7 +32,6 @@ use self::{ filters::{ffi_converter_name, type_name}, oracle::CodeOracle, }; - use crate::{ bindings::{ extensions::{ @@ -41,62 +40,47 @@ use crate::{ metadata::ModuleMetadata, type_map::TypeMap, }, - SwitchArgs, + switches::SwitchArgs, }; -#[derive(Default)] -pub(crate) struct TsBindings { - pub(crate) codegen: String, - pub(crate) frontend: String, -} - -pub(crate) fn generate_bindings( +pub(crate) fn generate_api_code( ci: &ComponentInterface, config: &Config, module: &ModuleMetadata, switches: &SwitchArgs, type_map: &TypeMap, -) -> Result { - let codegen = CodegenWrapper::new(ci, config, module, switches) +) -> Result { + let types = TypeRenderer::new(ci, config, module, switches, type_map); + TsApiWrapper::try_from(types)? .render() - .context("generating codegen bindings failed")?; - let frontend = FrontendWrapper::new(ci, config, module, switches, type_map) - .render() - .context("generating frontend javascript failed")?; + .context("generating frontend typescript failed") +} - Ok(TsBindings { codegen, frontend }) +pub(crate) fn generate_lowlevel_code( + ci: &ComponentInterface, + module: &ModuleMetadata, +) -> Result { + LowlevelTsWrapper::new(ci, module) + .render() + .context("generating lowlevel typescipt failed") } #[derive(Template)] #[template(syntax = "ts", escape = "none", path = "wrapper-ffi.ts")] -struct CodegenWrapper<'a> { +struct LowlevelTsWrapper<'a> { ci: &'a ComponentInterface, - #[allow(unused)] - config: &'a Config, module: &'a ModuleMetadata, - #[allow(unused)] - switches: &'a SwitchArgs, } -impl<'a> CodegenWrapper<'a> { - fn new( - ci: &'a ComponentInterface, - config: &'a Config, - module: &'a ModuleMetadata, - switches: &'a SwitchArgs, - ) -> Self { - Self { - ci, - config, - module, - switches, - } +impl<'a> LowlevelTsWrapper<'a> { + fn new(ci: &'a ComponentInterface, module: &'a ModuleMetadata) -> Self { + Self { ci, module } } } #[derive(Template)] #[template(syntax = "ts", escape = "none", path = "wrapper.ts")] -struct FrontendWrapper<'a> { +struct TsApiWrapper<'a> { ci: &'a ComponentInterface, #[allow(unused)] config: &'a Config, @@ -109,20 +93,19 @@ struct FrontendWrapper<'a> { imported_converters: BTreeMap<(String, String), BTreeSet>, } -impl<'a> FrontendWrapper<'a> { - pub fn new( - ci: &'a ComponentInterface, - config: &'a Config, - module: &'a ModuleMetadata, - switches: &'a SwitchArgs, - type_map: &'a TypeMap, - ) -> Self { - let type_renderer = TypeRenderer::new(ci, config, module, switches, type_map); - let type_helper_code = type_renderer.render().unwrap(); - let type_imports = type_renderer.imports.into_inner(); - let exported_converters = type_renderer.exported_converters.into_inner(); - let imported_converters = type_renderer.imported_converters.into_inner(); - Self { +impl<'a> TryFrom> for TsApiWrapper<'a> { + type Error = anyhow::Error; + + fn try_from(types: TypeRenderer<'a>) -> Result { + let type_helper_code = types.render()?; + let type_imports = types.imports.into_inner(); + let exported_converters = types.exported_converters.into_inner(); + let imported_converters = types.imported_converters.into_inner(); + let module = types.module; + let config = types.config; + let ci = types.ci; + let switches = types.switches; + Ok(Self { module, config, ci, @@ -131,7 +114,7 @@ impl<'a> FrontendWrapper<'a> { type_imports, exported_converters, imported_converters, - } + }) } } @@ -141,9 +124,8 @@ impl<'a> FrontendWrapper<'a> { /// process. Make sure to only call `render()` once. #[derive(Template)] #[template(syntax = "ts", escape = "none", path = "Types.ts")] -pub struct TypeRenderer<'a> { +struct TypeRenderer<'a> { ci: &'a ComponentInterface, - #[allow(unused)] config: &'a Config, #[allow(unused)] module: &'a ModuleMetadata, diff --git a/crates/ubrn_bindgen/src/react_native.rs b/crates/ubrn_bindgen/src/react_native.rs index 45967afd..8a121579 100644 --- a/crates/ubrn_bindgen/src/react_native.rs +++ b/crates/ubrn_bindgen/src/react_native.rs @@ -12,12 +12,7 @@ use serde::{Deserialize, Serialize}; use uniffi_bindgen::{BindingGenerator, Component, GenerationSettings}; use crate::{ - bindings::{ - gen_cpp::{self, CppBindings}, - gen_typescript::{self, TsBindings}, - metadata::ModuleMetadata, - type_map::TypeMap, - }, + bindings::{gen_cpp, gen_typescript, metadata::ModuleMetadata, type_map::TypeMap}, switches::SwitchArgs, }; @@ -36,9 +31,55 @@ impl ReactNativeBindingGenerator { } } - pub(crate) fn format_code(&self) -> Result<()> { - gen_typescript::format_directory(&self.ts_dir)?; - gen_cpp::format_directory(&self.cpp_dir)?; + fn generate_ts( + components: &[Component], + switches: &SwitchArgs, + out_dir: &Utf8PathBuf, + try_format_code: bool, + ) -> std::result::Result<(), anyhow::Error> { + let type_map = TypeMap::from(components); + for component in components { + let module = ModuleMetadata::from(component); + + let api_ts = gen_typescript::generate_api_code( + &component.ci, + &component.config.typescript, + &module, + switches, + &type_map, + )?; + let api_ts_path = out_dir.join(module.ts_filename()); + fs::write(api_ts_path, api_ts)?; + + let lowlevel_ts = gen_typescript::generate_lowlevel_code(&component.ci, &module)?; + let lowlevel_ts_path = out_dir.join(module.ts_ffi_filename()); + fs::write(lowlevel_ts_path, lowlevel_ts)?; + } + if try_format_code { + gen_typescript::format_directory(out_dir)?; + } + Ok(()) + } + + fn generate_cpp( + components: &[Component], + out_dir: &Utf8PathBuf, + try_format_code: bool, + ) -> Result<(), anyhow::Error> { + for component in components { + let module = ModuleMetadata::from(component); + + let cpp = gen_cpp::generate_cpp(&component.ci, &component.config.cpp, &module)?; + let cpp_path = out_dir.join(module.cpp_filename()); + fs::write(cpp_path, cpp)?; + + let hpp = gen_cpp::generate_hpp(&component.ci, &component.config.cpp, &module)?; + let hpp_path = out_dir.join(module.hpp_filename()); + fs::write(hpp_path, hpp)?; + } + if try_format_code { + gen_cpp::format_directory(out_dir)?; + } Ok(()) } } @@ -67,43 +108,13 @@ impl BindingGenerator for ReactNativeBindingGenerator { settings: &GenerationSettings, components: &[Component], ) -> Result<()> { - let type_map = TypeMap::from(components); - - let out_dir = &self.ts_dir; - for component in components { - let ci = &component.ci; - let module = ModuleMetadata::from(component); - let config = &component.config; - let TsBindings { codegen, frontend } = gen_typescript::generate_bindings( - ci, - &config.typescript, - &module, - &self.switches, - &type_map, - )?; - - let codegen_path = out_dir.join(module.ts_ffi_filename()); - fs::write(codegen_path, codegen)?; - - let frontend_path = out_dir.join(module.ts_filename()); - fs::write(frontend_path, frontend)?; - } - - let out_dir = &self.cpp_dir; - for component in components { - let ci = &component.ci; - let module: ModuleMetadata = component.into(); - let config = &component.config; - - let CppBindings { hpp, cpp } = gen_cpp::generate_bindings(ci, &config.cpp, &module)?; - let cpp_path = out_dir.join(module.cpp_filename()); - let hpp_path = out_dir.join(module.hpp_filename()); - fs::write(cpp_path, cpp)?; - fs::write(hpp_path, hpp)?; - } - if settings.try_format_code { - self.format_code()?; - } + Self::generate_ts( + components, + &self.switches, + &self.ts_dir, + settings.try_format_code, + )?; + Self::generate_cpp(components, &self.cpp_dir, settings.try_format_code)?; Ok(()) } }