Skip to content

Commit

Permalink
Separate different files out into different generate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Dec 4, 2024
1 parent 97c86b0 commit 859a02f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 107 deletions.
18 changes: 9 additions & 9 deletions crates/ubrn_bindgen/src/bindings/gen_cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Ok(HppWrapper::new(ci, config, module).render()?)
}

pub(crate) fn generate_bindings(
pub(crate) fn generate_cpp(
ci: &ComponentInterface,
config: &Config,
module: &ModuleMetadata,
) -> Result<CppBindings> {
let hpp = HppWrapper::new(ci, config, module).render()?;
let cpp = CppWrapper::new(ci, config, module).render()?;
Ok(CppBindings { hpp, cpp })
) -> Result<String> {
Ok(CppWrapper::new(ci, config, module).render()?)
}

#[derive(Template)]
Expand Down
86 changes: 34 additions & 52 deletions crates/ubrn_bindgen/src/bindings/gen_typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use self::{
filters::{ffi_converter_name, type_name},
oracle::CodeOracle,
};

use crate::{
bindings::{
extensions::{
Expand All @@ -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<TsBindings> {
let codegen = CodegenWrapper::new(ci, config, module, switches)
) -> Result<String> {
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<String> {
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,
Expand All @@ -109,20 +93,19 @@ struct FrontendWrapper<'a> {
imported_converters: BTreeMap<(String, String), BTreeSet<String>>,
}

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<TypeRenderer<'a>> for TsApiWrapper<'a> {
type Error = anyhow::Error;

fn try_from(types: TypeRenderer<'a>) -> Result<Self> {
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,
Expand All @@ -131,7 +114,7 @@ impl<'a> FrontendWrapper<'a> {
type_imports,
exported_converters,
imported_converters,
}
})
}
}

Expand All @@ -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,
Expand Down
103 changes: 57 additions & 46 deletions crates/ubrn_bindgen/src/react_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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<ReactNativeConfig>],
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<ReactNativeConfig>],
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(())
}
}
Expand Down Expand Up @@ -67,43 +108,13 @@ impl BindingGenerator for ReactNativeBindingGenerator {
settings: &GenerationSettings,
components: &[Component<Self::Config>],
) -> 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(())
}
}
Expand Down

0 comments on commit 859a02f

Please sign in to comment.