fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.18k stars 289 forks source link

[experimental-feature] Support when library name is different from crate name #2173

Open blopker opened 3 months ago

blopker commented 3 months ago

Describe the bug

I'm not sure if this is a bug or feature request. I'm trying to auto generate bindings for the crate here: https://github.com/Lymphatus/libcaesium. It has a [lib] section in the Cargo file that has a name declared, caesium, which seems to cause issues.

My flutter_rust_bridge.yaml:

rust_input: crate::api,libcaesium
dart_output: lib/src/rust
rust_root: rust/

This is able to generate the bindings, but they won't compile because the bindings try to import with libcaesium, when it needs to use caesium.

I get a lot of errors similar to:

SEVERE:     --> src/frb_generated.rs:2296:20
SEVERE:      |
SEVERE: 2296 |     for FrbWrapper<libcaesium::ChromaSubsampling>
SEVERE:      |                    ^^^^^^^^^^ use of undeclared crate or module `libcaesium`

Thank you!

Steps to reproduce

  1. Add libcaesium to a Flutter project by including libcaesium = { git = "https://github.com/Lymphatus/libcaesium.git", tag = "0.15.5" } in Cargo.toml
  2. Add libcaesium to flutter_rust_bridge.yaml rust_input
  3. Run flutter_rust_bridge_codegen generate
  4. Try to compile the generated code

Logs

SEVERE: error[E0433]: failed to resolve: use of undeclared crate or module `libcaesium`
SEVERE:     --> src/frb_generated.rs:1963:16
SEVERE:      |
SEVERE: 1963 |         return libcaesium::GifParameters {
SEVERE:      |                ^^^^^^^^^^ use of undeclared crate or module `libcaesium`
SEVERE:      |
SEVERE: help: there is a crate or module with a similar name
SEVERE:      |
SEVERE: 1963 |         return caesium::GifParameters {
SEVERE:      |                ~~~~~~~
SEVERE: 
SEVERE: error[E0433]: failed to resolve: use of undeclared crate or module `libcaesium`
SEVERE:     --> src/frb_generated.rs:1976:20
SEVERE:      |
SEVERE: 1976 | impl SseDecode for libcaesium::JpegParameters {
SEVERE:      |                    ^^^^^^^^^^ use of undeclared crate or module `libcaesium`
SEVERE:      |
SEVERE: help: there is a crate or module with a similar name
SEVERE:      |
SEVERE: 1976 | impl SseDecode for caesium::JpegParameters {
SEVERE:      |                    ~~~~~~~

Expected behavior

No response

Generated binding code

No response

OS

MacOS

Version of flutter_rust_bridge_codegen

2.0.0

Flutter info

[✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-US)
    • Flutter version 3.22.2 on channel stable at /Users/blopker/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 761747bfc5 (4 weeks ago), 2024-06-05 22:15:13 +0200
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/blopker/Library/Android/sdk
    • Platform android-34, build-tools 33.0.1
    • ANDROID_HOME = /Users/blopker/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.90.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.90.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 126.0.6478.127
    ! Error: Browsing on the local area network for iPhone (2). Ensure the device is unlocked and attached with
      a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Version of clang++

No response

Additional context

No response

welcome[bot] commented 3 months ago

Hi! Thanks for opening your first issue here! :smile:

blopker commented 3 months ago

I should add that making rust_input: crate::api,caesium errors with:

ror: package ID specification `caesium` did not match any packages
    Did you mean `libcaesium`?

When trying to generate the bindings.

fzyzcjy commented 3 months ago

It has a [lib] section in the Cargo file that has a name declared, caesium, which seems to cause issues.

I think so, frb does not yet understand this field.

I'm not sure if this is a bug or feature request.

So yes it looks like a feature request

I guess it is not hard to fix it. Rough idea:

Feel free to PR for this! Alternatively, I may work on it in the next batch.

blopker commented 3 months ago

Great! I thought I might just be holding it wrong, but good to know it's unimplemented. My workaround is just to make a small Rust wrapper, which was easy enough.

I'm new to Rust, but I may end up trying to do it.

Anyway, thanks for this amazing project! It's been wonderful to work with.

fzyzcjy commented 3 months ago

You are welcome and looking forward to the PR! The wrapper looks reasonable since the API looks small.