fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generation failing when non-enum default value and `dart_enums_style: true`

Dampfwalze opened this issue · comments

Describe the bug

When defining a default value that is not an enum, like this: #[frb(default = "10")], and the dart_enums_style option is set to true, the generator crashes with the following error:

thread 'main' panicked at C:\Users\pauls\.cargo\registry\src\index.crates.io-6f17d22bba15001f\flutter_rust_bridge_codegen-2.0.0-dev.24\src\library\codegen\generator\api_dart\spec_generator\class\field.rs:57:37:
called `Option::unwrap()` on a `None` value

File with error on line 57:

fn default_value_maybe_to_dart_style(value: &str, enable: bool) -> Cow<str> {
if enable {
default_value_to_dart_style(value).into()
} else {
value.into()
}
}
fn default_value_to_dart_style(value: &str) -> String {
let mut split = value.split('.');
let enum_name = split.next().unwrap();
let variant_name = split.next().unwrap().to_string();
let variant_name = make_string_keyword_safe(variant_name.to_case(Case::Camel));
format!("{enum_name}.{variant_name}")
}

The enable parameter is true when dart_enums_style is true.

For me it seems like non-enum values are not supported in any way by the renaming feature.

Steps to reproduce

#[frb]
pub struct MyStruct {
    #[frb(default = "10")]
    pub value: usize,
}

pub fn test(my_struct: MyStruct) {
    println!("{}", my_struct.value);
}

Logs

output.txt

Expected behavior

No response

Generated binding code

No response

OS

Windows

Version of flutter_rust_bridge_codegen

2.0.0-dev.24

Flutter info

No response

Version of clang++

No response

Additional context

No response

On that note, having default values in function parameters yields a Rust compiler error:

#[frb]
pub fn test(#[frb(default = "10")] value: usize) {
    println!("Hello, world!");
}
error: documentation comments cannot be applied to function parameters
 --> src\api.rs:3:1
  |
3 | #[frb]
  | ^^^^^^ doc comments are not allowed here
  |
  = note: this error originates in the attribute macro `frb` (in Nightly builds, run with -Z macro-backtrace for more info)

Looks like a bug, feel free to fix it (e.g. skip when there is no dot)! Alternatively, I will fix it in the next batch (hopefully within a week).

having default values in function parameters yields a Rust compiler error

Hmm, the #[frb(...)] is transformed to doc comments and seems it is not supported: rust-lang/rust#57525

/cc @Desdaemon who is working on PR #1676 in case it is somehow related

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.