Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not exporting some struct types needed for customizing handlers; Fix ignoring user-provided custom handler objects #1865

Merged
merged 39 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ded5204
chore: empty
fzyzcjy Apr 8, 2024
0d0bc05
feat: custom handler demo
fzyzcjy Apr 8, 2024
fa40c1a
feat: more
fzyzcjy Apr 8, 2024
8b360dd
chore: codegen
fzyzcjy Apr 8, 2024
8b8fac9
chore: codegen
fzyzcjy Apr 8, 2024
ba2bde4
chore: empty
fzyzcjy Apr 8, 2024
697e1f6
chore: comment
fzyzcjy Apr 8, 2024
66b48cc
chore: use const
fzyzcjy Apr 8, 2024
0812765
fix: err
fzyzcjy Apr 8, 2024
965e7c6
chore: temp demo
fzyzcjy Apr 8, 2024
cd81898
chore: codegen
fzyzcjy Apr 8, 2024
1900ccf
fix: name
fzyzcjy Apr 8, 2024
fbc7d6c
feat: use
fzyzcjy Apr 8, 2024
bc934db
feat: read
fzyzcjy Apr 8, 2024
a7d6e9c
chore: empty map
fzyzcjy Apr 8, 2024
a8dc81c
chore: more
fzyzcjy Apr 8, 2024
a7d744a
chore: more
fzyzcjy Apr 8, 2024
e73ab0a
feat: namespace
fzyzcjy Apr 8, 2024
9fa1beb
chore: comment
fzyzcjy Apr 8, 2024
4ac721a
fix: compile
fzyzcjy Apr 8, 2024
602ff9e
chore: codegen
fzyzcjy Apr 8, 2024
d80bd08
feat: pub use to be consistent
fzyzcjy Apr 8, 2024
73d2be5
chore: rm temp
fzyzcjy Apr 8, 2024
6e852a3
chore: codegen
fzyzcjy Apr 8, 2024
7f64658
chore: empty test
fzyzcjy Apr 8, 2024
5268531
chore: simple cp
fzyzcjy Apr 8, 2024
1c03c02
chore: export
fzyzcjy Apr 8, 2024
1c06200
feat: first
fzyzcjy Apr 8, 2024
61f7bef
chore: empty
fzyzcjy Apr 8, 2024
aecf1c3
feat: more
fzyzcjy Apr 8, 2024
c59b2c4
chore: rename
fzyzcjy Apr 8, 2024
9552f13
feat: more
fzyzcjy Apr 8, 2024
b5ab8a0
feat: doc
fzyzcjy Apr 8, 2024
c25bb52
chore: template
fzyzcjy Apr 8, 2024
a39837a
chore: codegen
fzyzcjy Apr 8, 2024
fbd8583
chore: lint
fzyzcjy Apr 8, 2024
61b84fa
Merge branch 'master' into feat/1858
fzyzcjy Apr 8, 2024
4511502
chore: goldens
fzyzcjy Apr 8, 2024
df36256
chore: lint
fzyzcjy Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading