Skip to content

Commit

Permalink
Merge pull request #1865 from fzyzcjy/feat/1858
Browse files Browse the repository at this point in the history
Fix not exporting some struct types needed for customizing handlers; Fix ignoring user-provided custom handler objects
  • Loading branch information
fzyzcjy authored Apr 8, 2024
2 parents 4f9c6a2 + df36256 commit 8805811
Show file tree
Hide file tree
Showing 33 changed files with 443 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "REPLACE_ME_FRB_VERSION";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "REPLACE_ME_FRB_VERSION";

// Section: executor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn generate(
code_header: Acc::new(|_| vec![(generate_code_header() + "\n\n").into()]),
file_attributes: Acc::new_common(vec![FILE_ATTRIBUTES.to_string().into()]),
imports: generate_imports(&cache.distinct_types, context),
executor: Acc::new_common(vec![generate_executor(context.ir_pack).into()]),
executor: Acc::new_common(vec![generate_handler(context.ir_pack).into()]),
boilerplate: generate_boilerplate(
context.config.default_stream_sink_codec,
context.config.default_rust_opaque_codec,
Expand Down Expand Up @@ -152,7 +152,7 @@ fn generate_boilerplate(
default_rust_opaque = RustOpaque{default_rust_opaque_codec},
default_rust_auto_opaque = RustAutoOpaque{default_rust_opaque_codec},
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "{version}";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "{version}";
"#,
version = env!("CARGO_PKG_VERSION"),
)
Expand Down Expand Up @@ -198,9 +198,9 @@ fn generate_boilerplate(
// }
// }

fn generate_executor(ir_pack: &IrPack) -> String {
if ir_pack.has_executor {
"/* nothing since executor detected */".to_owned()
fn generate_handler(ir_pack: &IrPack) -> String {
if let Some(existing_handler) = &ir_pack.existing_handler {
format!("pub use {};", existing_handler.rust_style())
} else {
r#"flutter_rust_bridge::frb_generated_default_handler!();"#.to_owned()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::codegen::generator::wire::rust::spec_generator::base::*;
use crate::codegen::generator::wire::rust::spec_generator::misc::ty::WireRustGeneratorMiscTrait;
use crate::codegen::generator::wire::rust::spec_generator::output_code::WireRustOutputCode;
use crate::codegen::ir::ty::IrTypeTrait;
use crate::library::misc::consts::HANDLER_NAME;
use itertools::Itertools;

impl<'a> WireRustGeneratorMiscTrait for DartFnWireRustGenerator<'a> {
Expand Down Expand Up @@ -32,7 +33,7 @@ impl<'a> WireRustGeneratorMiscTrait for DartFnWireRustGenerator<'a> {
async fn body(dart_opaque: flutter_rust_bridge::DartOpaque, {parameter_names_and_types}) -> {return_type} {{
let args = vec![{into_dart_expressions}];
let message = FLUTTER_RUST_BRIDGE_HANDLER.dart_fn_invoke(dart_opaque, args).await;
let message = {HANDLER_NAME}.dart_fn_invoke(dart_opaque, args).await;
<{return_type}>::sse_decode_single(message)
}}
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/codegen/ir/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct IrPack {
pub funcs: Vec<IrFunc>,
pub struct_pool: IrStructPool,
pub enum_pool: IrEnumPool,
pub has_executor: bool,
pub existing_handler: Option<NamespacedName>,
pub unused_types: Vec<NamespacedName>,
}

Expand Down
4 changes: 3 additions & 1 deletion frb_codegen/src/library/codegen/parser/misc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::library::misc::consts::HANDLER_NAME;

pub(crate) fn parse_has_executor(source_rust_content: &str) -> bool {
source_rust_content.contains(&"static ref FLUTTER_RUST_BRIDGE_HANDLER".to_string())
source_rust_content.contains(&format!("static {HANDLER_NAME}"))
}
22 changes: 20 additions & 2 deletions frb_codegen/src/library/codegen/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) mod type_parser;
mod unused_checker;

use crate::codegen::dumper::Dumper;
use crate::codegen::ir::namespace::{Namespace, NamespacedName};
use crate::codegen::ir::pack::IrPack;
use crate::codegen::misc::GeneratorProgressBarPack;
use crate::codegen::parser::function_extractor::extract_generalized_functions_from_file;
Expand All @@ -21,6 +22,8 @@ use crate::codegen::parser::type_alias_resolver::resolve_type_aliases;
use crate::codegen::parser::type_parser::TypeParser;
use crate::codegen::parser::unused_checker::get_unused_types;
use crate::codegen::ConfigDumpContent;
use crate::library::misc::consts::HANDLER_NAME;
use anyhow::ensure;
use itertools::Itertools;
use log::trace;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -88,15 +91,30 @@ pub(crate) fn parse(
.sorted_by_cached_key(|func| func.name.clone())
.collect_vec();

let has_executor = (file_data_arr.iter()).any(|file| parse_has_executor(&file.content));
let existing_handlers = (file_data_arr.iter())
.filter(|file| parse_has_executor(&file.content))
.map(|file| {
NamespacedName::new(
Namespace::new_from_rust_crate_path(&file.path, &config.rust_crate_dir).unwrap(),
HANDLER_NAME.to_owned(),
)
})
.collect_vec();
ensure!(
existing_handlers.len() <= 1,
// frb-coverage:ignore-start
// This will stop the whole generator and tell the users, so we do not care about testing it
"Should have at most one custom handler"
);
// frb-coverage:ignore-end

let (struct_pool, enum_pool) = type_parser.consume();

let mut ans = IrPack {
funcs: ir_funcs,
struct_pool,
enum_pool,
has_executor,
existing_handler: existing_handlers.first().cloned(),
unused_types: vec![],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"wrapper_name": null
}
},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -372,7 +373,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {
"crate::api/MyGenericStruct": {
"comments": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"wrapper_name": null
}
},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -221,7 +222,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {
"crate::api/MyStruct": {
"comments": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enum_pool": {},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -44,7 +45,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {},
"unused_types": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enum_pool": {},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -79,7 +80,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {},
"unused_types": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enum_pool": {},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -129,7 +130,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {},
"unused_types": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enum_pool": {},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand All @@ -23,7 +24,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {},
"unused_types": []
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"enum_pool": {},
"existing_handler": null,
"funcs": [],
"has_executor": false,
"struct_pool": {},
"unused_types": [
"crate::api/UnusedStruct",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"wrapper_name": null
}
},
"existing_handler": null,
"funcs": [
{
"codec_mode_pack": {
Expand Down Expand Up @@ -85,7 +86,6 @@
"rust_async": false
}
],
"has_executor": false,
"struct_pool": {
"crate::another_file/StructInAnotherFile": {
"comments": [],
Expand Down
2 changes: 1 addition & 1 deletion frb_example/dart_build_rs/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
2 changes: 1 addition & 1 deletion frb_example/dart_minimal/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
2 changes: 1 addition & 1 deletion frb_example/deliberate_bad/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
2 changes: 1 addition & 1 deletion frb_example/flutter_via_create/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
2 changes: 1 addition & 1 deletion frb_example/gallery/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_opaque = RustOpaqueMoi,
default_rust_auto_opaque = RustAutoOpaqueMoi,
);
const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.30";

// Section: executor

Expand Down
13 changes: 13 additions & 0 deletions frb_example/pure_dart/rust/src/api/custom_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// FRB_INTERNAL_GENERATOR: {"forbiddenDuplicatorModes": ["sync", "rustAsync", "sse", "sync sse", "rustAsync sse"]}

#[allow(unused_imports)]
use crate::frb_generated::FLUTTER_RUST_BRIDGE_CODEGEN_VERSION;

// This file demonstrates how to use a custom handler.
// Usually there is no need for this, and the default handler is used.
//
// Here, for simplicity, we still generate code for default handler.
// But surely you can copy-paste the content of that macro and modify according to your needs.
flutter_rust_bridge::frb_generated_default_handler!();

// NOTE: For more tests about customizing handler types and contents, please visit `src/auxiliary/custom_handler.rs`
1 change: 1 addition & 0 deletions frb_example/pure_dart/rust/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod comment;
#[cfg(target_os = "non_existent_os")]
pub mod conditionally_compiled_module;
pub mod constructor;
pub mod custom_handler;
pub mod customization;
pub mod dart_dynamic;
pub mod dart_fn;
Expand Down
Loading

0 comments on commit 8805811

Please sign in to comment.