Introduce bool shorthand (`#[builder(flag)]`)

Open
#142 15 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust

Research direction

The issue names no files or tests. Start by locating the Builder derive macro's handling of boolean fields and review existing setter-generation tests; completion means supporting the proposed true and false shorthand while preserving ordinary boolean setters.

Written by the indexing model from the issue text.

Description

feature request

Hey there,

Bon is great but it still feels weird to write .property(true) for booleans and I feel that an option to shorten this usage would be a nice addition.

The syntax is just an example of how I would see it implemented :

use bon::Builder;

#[derive(Builder)]
struct User {
    id: u32,
    #[builder(bool)] // add some attribute to a boolean property
    color: bool,
}

And then use it without specifying true :

let user = User::builder()
    .id(1)
    .color() // true
    .build();

For falsy values, one could think of adding the same method with a prefix (no_ by default) :

use bon::Builder;

#[derive(Builder)]
struct User {
    id: u32,
    #[builder(bool, false = "no")] // optional, defaults to "no"
    color: bool,
}

And then :

let user = User::builder()
    .id(1)
    .no_color() // false
    .build();

let user = User::builder()
    .id(1)
    .color() // still valid : true
    .build();

Feedback is welcome, it would greatly improve my builders!

A note for the community from the maintainers

Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.

Dominant language
Rust
Stars
2.1k
Forks
46
Avg merge
53m
Merged PRs (30d)
5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from elastio/bon

All issues in elastio/bon

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.