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

Improve field_defaults semantics #153

Open
ifiokjr opened this issue Sep 14, 2024 · 0 comments
Open

Improve field_defaults semantics #153

ifiokjr opened this issue Sep 14, 2024 · 0 comments

Comments

@ifiokjr
Copy link
Contributor

ifiokjr commented Sep 14, 2024

Description

Here is the current documentation for the field_defaults(...) attribute on TypedBuilder structs.

is structured like the #[builder(...)] attribute you can put on the fields and sets default options for fields of the type. If specific field need to revert some options to the default defaults they can prepend ! to the option they need to revert, and it would ignore the field defaults for that option in that field.

I have the following proposal.

  1. For the attributes strip_option and strip_bool there should be a way to only apply them to fields that are valid. Currently, when setting strip_option you need to use ! to negate it on non-option fields. This should not be required. Perhaps ignore_invalid as a sub attribute flag to prevent this being a breaking change.
  2. In feat: add strip_option(fallback = field_opt) #150, support was added for fallback methods with this syntax strip_option(fallback = custom_name_opt). This currently doesn't work for field_defaults. There should be an option available for strip_option which allows setting a fallback_prefix and fallback_suffix which adds the fallback to all optional methods automatically.

Possible API

use typed_builder::TypedBuilder;

#[derive(TypedBuilder)]
#[builder(field_defaults(default, setter(strip_option(ignore_invalid, fallback_suffix = "_opt")))]
struct Foo {
    // Defaults to None, options-stripping is performed and a fallback 
    // method of `x_opt` is available.
    x: Option<i32>,

    // Defaults to 0, no longer need `!strip_option`:
    y: i32,

    // Defaults to Some(13), option-stripping is performed and a fallback 
    // method of `z_opt` is available:
    #[builder(default = Some(13))]
    z: Option<i32>,
}
@ifiokjr ifiokjr changed the title Improved field_defaults semantics Improve field_defaults semantics Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant