flutter_rust_bridge icon indicating copy to clipboard operation
flutter_rust_bridge copied to clipboard

Fix bug with conflicting names in enum name and variant

Open Roms1383 opened this issue 3 years ago • 9 comments

Fixes #406

Checklist

  • [x] An issue to be fixed by this PR is listed above.
  • [x] New tests are added to ensure new features are working. End-to-end tests are usually in the ./frb_example/pure_dart example, more specifically, rust/src/api.rs and dart/lib/main.dart.
  • [x] The code generator has been run, and code changes are commited. (Otherwise, CI will fail.)
  • [ ] If this PR adds/changes features, documentations (in the ./book folder) are updated.
  • [x] CI is passing.

Roms1383 avatar Jul 31 '22 15:07 Roms1383

Ok so far I got the generator generates files that compile, but somehow I broke the tests :boom: :boom: :boom: 😅 I definitely could use some help here ! ^^


Also, as you will see from regenerated files, the naming might not be to your liking. What I came up here is to rename the enum's variants generated classes for example. So now they are generated in the form enum_name_variant_name_Variant. For example this:

#[derive(Debug, Clone)]
#[frb(freezed)]
pub enum Speed {
    Unknown,
    GPS(f64),
}

Will generate e.g. a class like Speed_Unknown_Variant and Speed_GPS_Variant.

:warning: Please note that I only modified the generated classes / variables / etc only where necessary, leaving all the rest of the code untouched.


:microscope: I initially generated git patches files to investigate (and the error with conflicting names becomes quite apparent) but I can't join them with *.patch extension, so I'm gonna rename them as *.txt.

conflicting_sample.enum_variant_matches_another_enum_name.txt conflicting_sample.enum_variant_matches_another_enum_variant.txt working_sample.txt

Roms1383 avatar Jul 31 '22 16:07 Roms1383

Will generate e.g. a class like Speed_Unknown_Variant and Speed_GPS_Variant.

Another proposal: Shall we call it SpeedUnknown, SpeedGPS etc, unconditionally (i.e. always name like that)? I feel it a bit more like Speed::Unknown. And moreover, in freezed, it is not uncommon to see namings like this.

Since the great enhanced enum feature is impl by desdaemon, /cc @Desdaemon what do you think?

fzyzcjy avatar Jul 31 '22 22:07 fzyzcjy

but somehow I broke the tests

CI looks good so far :)

Anyway I will wait until this PR is marked as ready for review and then review it.

fzyzcjy avatar Jul 31 '22 22:07 fzyzcjy

CI looks good so far :)

hey yeah, I was suprised because on my machine just test-pure fails (to load dynamic library) :)

click to see error details
cd frb_example/pure_dart/dart && dart pub get && dart lib/main.dart ../rust/target/debug/libflutter_rust_bridge_example.dylib
Resolving dependencies... (1.2s)
Got dependencies!
flutter_rust_bridge example program start (dylibPath=../rust/target/debug/libflutter_rust_bridge_example.dylib)
construct api
Unhandled exception:
Invalid argument(s): Failed to load dynamic library '../rust/target/debug/libflutter_rust_bridge_example.dylib': dlopen(../rust/target/debug/libflutter_rust_bridge_example.dylib, 0x0001): tried: '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/./../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/../../../../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/Frameworks/../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/./../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/../../../../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '/opt/homebrew/Cellar/dart/2.17.6/libexec/bin/Frameworks/../rust/target/debug/libflutter_rust_bridge_example.dylib' (no such file), '../rust/target/debug/libflutter_rust_bridge_example.dylib' (relative path not allowed in hardened program)
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:12:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:23:12)
#2      main (package:flutter_rust_bridge_example/main.dart:12:32)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
error: Recipe `test-pure` failed on line 46 with exit code 255

Roms1383 avatar Aug 01 '22 02:08 Roms1383

What is weird regarding the failure at running just test-pure locally is that it cannot find the compiled libflutter_rust_bridge_example.dylib inside ../rust/target/debug/ but I re-ran cargo build already, checked its presence and still get the same error.

Roms1383 avatar Aug 01 '22 08:08 Roms1383

"relative path not allowed in hardened program"

is that some cause?

Anyway, as long as CI is happy we are surely happy :)

fzyzcjy avatar Aug 01 '22 08:08 fzyzcjy

ok @fzyzcjy waiting for your review :)

Roms1383 avatar Aug 01 '22 13:08 Roms1383

Great job! Since it looks nontrivial, I will review it tomorrow morning (~8hr from now) :)

fzyzcjy avatar Aug 01 '22 13:08 fzyzcjy

Oops misclicked that button. Meant to click that for another PR

fzyzcjy avatar Aug 05 '22 04:08 fzyzcjy