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

Replace typed_builder with bon #9

Merged
merged 3 commits into from
Oct 10, 2024
Merged

Replace typed_builder with bon #9

merged 3 commits into from
Oct 10, 2024

Conversation

jssblck
Copy link
Member

@jssblck jssblck commented Oct 10, 2024

Overview

Replaces typed_builder with bon.

Primarily this is because it's annoying to have to do this:

let org_id = Some(1234);
let locator = StrictLocator::builder()
    .fetcher(Fetcher::Git)
    .package("...")
    .revision("...");
let locator = match org_id {
    None => locator.build(),
    Some(org_id) => locator.org_id(org_id).build(),
};

With bon, we can do this instead:

let org_id = Some(1234);
let locator = StrictLocator::builder()
    .fetcher(Fetcher::Git)
    .package("...")
    .revision("...")
    .maybe_org_id(org_id)
    .build();

And we can still build when we know we have a field:

let locator = StrictLocator::builder()
    .fetcher(Fetcher::Git)
    .package("...")
    .revision("...")
    .org_id(1234)
    .build();

Bon also generally encourages cleaner patterns (e.g. by requiring Into implementations instead of custom setters), generates nicer error messages, and generates cleaner code.

Acceptance criteria

Existing tests pass (demonstrating that this isn't a breaking change) and new functionality exists.

Testing plan

Automated testing

Metrics

None

Risks

At worst this might be secretly breaking for some edge case type conversion, but that should be easy enough for callers to work around if that's the case.

References

None

Checklist

  • I added tests for this PR's change (or explained in the PR description why tests don't make sense).

@jssblck jssblck requested review from a team and spatten and removed request for a team October 10, 2024 19:09
@Veetaha
Copy link

Veetaha commented Oct 10, 2024

by requiring Into implementations instead of custom setters

I don't think it was one of the goals, rather it's a missing feature which could be worked around by falling back to creating a builder from a function.

Anyway, I'm going to add #[builder(with = closure)] syntax to define custom setters, and make it possible to add custom methods to the builder in the next release (elastio/bon#145).

@jssblck
Copy link
Member Author

jssblck commented Oct 10, 2024

@Veetaha good to know, thanks for the comment!

Honestly I like the current approach anyway: I'm a big fan of type-driven design, so doing it the way we have to today in bon is a positive to me. But for sure, adding this as an option certainly seems to not be a bad thing.

Copy link

@spatten spatten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me. I like the new syntax and the new tests :)

@jssblck jssblck merged commit adb3137 into main Oct 10, 2024
10 checks passed
@jssblck jssblck deleted the bon branch October 10, 2024 22:20
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

Successfully merging this pull request may close these issues.

3 participants