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

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

Closed
Dampfwalze opened this issue Feb 19, 2024 · 3 comments · Fixed by #1780
Closed

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

Dampfwalze opened this issue Feb 19, 2024 · 3 comments · Fixed by #1780
Labels
bug Something isn't working

Comments

@Dampfwalze
Copy link
Contributor

Dampfwalze commented Feb 19, 2024

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

@Dampfwalze Dampfwalze added the bug Something isn't working label Feb 19, 2024
@Dampfwalze
Copy link
Contributor Author

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)

@Dampfwalze Dampfwalze changed the title Generation failing when non-enum defaut value and dart_enums_style: true Generation failing when non-enum default value and dart_enums_style: true Feb 19, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Feb 19, 2024

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

Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants