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.29k stars 301 forks source link

Getting started with third party packages #2352

Closed ethicnology closed 2 weeks ago

ethicnology commented 1 month ago

Is your feature request related to a problem? Please describe. Hey there, I love to see it getting more and more mature. I would like to start to use it on some projects but I'm facing "getting started" problem. The one-liner to generate a flutter app with some rust functions is great but when it comes to third parties libraries I feel lost. I've read various sections of the documentation but it miss step by step approach so we can step in without many doubts

Describe the solution you'd like

Describe what you've tried

  1. flutter_rust_bridge one liner
  2. cd rust
  3. Import a third party library: cargo add nostr
  4. cd ..
  5. flutter_rust_bridge_codegen generate

error:

flutter_rust_bridge_codegen-2.5.0/src/library/codegen/ir/mir/ty/enumeration.rs:51:32:
enum_pool does not contain MirTypeEnumRef { ident: MirEnumIdent(NamespacedName { namespace: Namespace { joined_path: "nostr::nips::nip19" }, name: "Nip19" }), is_exception: false }
thread 'main' panicked at flutter_rust_bridge_codegen-2.5.0/src/library/codegen/ir/mir/ty/enumeration.rs:51:32:
enum_pool does not contain MirTypeEnumRef { ident: MirEnumIdent(NamespacedName { namespace: Namespace { joined_path: "nostr::nips::nip19" }, name: "Nip19" }), is_exception: false }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Additional context I give here the example with the nostr third party package but I do not seek a specific answer for this, I'm looking for a more comprehensive way of using your tool his limits and how to work around.

fzyzcjy commented 1 month ago

Hi, automatically scanning a whole third party package is experimental feature and may not support too complex arbitrary packages (since there are so many corner cases for Rust). Looks like nostr may contain some edge cases that does not support yet. But feel free to use the https://cjycode.com/flutter_rust_bridge/guides/third-party/manual manual way or to improve this experimental feature!

ethicnology commented 1 month ago

Thanks to your last answer I've took the manual way

I've created a simple function that consume the third party crate:

use nostr::key::{Keys, SecretKey};

#[flutter_rust_bridge::frb(init)]
pub fn init_app() {
    flutter_rust_bridge::setup_default_user_utils();
}

#[flutter_rust_bridge::frb(sync)]
pub fn generate_nostr_keys() -> (String, String) {
    let keys = Keys::generate();
    let secret = keys.secret_key().to_secret_hex();
    let public = keys.public_key().to_string();
    return (secret, public);
}

While it's work fine on macos and ios, I'm facing this error targeting chrome with build-web related to secp256k1, probably a dependency of the third-party crate nostr

The following warnings were emitted during compilation:

warning: secp256k1-sys@0.10.1: error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
warning: secp256k1-sys@0.10.1: 1 error generated.
warning: secp256k1-sys@0.10.1: error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
warning: secp256k1-sys@0.10.1: 1 error generated.

error: failed to run custom build command for `secp256k1-sys v0.10.1`

…

warning: build failed, waiting for other jobs to finish...
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "rust" && "cargo" "build" "--lib" "--target" "wasm32-unknown-unknown" "-Z" "build-std=std,panic_abort"

Is there a work-around ?

ethicnology commented 1 month ago

I also had trouble building on ios and macos using web_socket_channel but this has nothing to do with rust third-party package, it's a compatibility issue between flutter_rust_bridge and web_socket_channel

fzyzcjy commented 1 month ago

Hi, that looks like a Rust compilation error. Thus maybe google / ask on the spcfici crate secp256k1-sys, since this is not flutter_rust_bridge can control.

fzyzcjy commented 2 weeks ago

Close since this seems to be solved, but feel free to reopen if having any questions!

github-actions[bot] commented 3 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.