-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Use bon
for an infallible and compile-time-checked builder
#466
Conversation
.strikethrough(exts.contains(&Extension::Strikethrough) || cli.gfm) | ||
.tagfilter(exts.contains(&Extension::Tagfilter) || cli.gfm) | ||
.table(exts.contains(&Extension::Table) || cli.gfm) | ||
.autolink(exts.contains(&Extension::Autolink) || cli.gfm) | ||
.tasklist(exts.contains(&Extension::Tasklist) || cli.gfm) | ||
.superscript(exts.contains(&Extension::Superscript)) | ||
.header_ids(cli.header_ids) | ||
.maybe_header_ids(cli.header_ids) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For optional fields (or fields with #[builder(default)]
) bon
generates two setters:
<field>(value: T)
maybe_<field>(value: Option<T>)
This is desingned this way so that changing a required field to optional is compatible, and it also simplifies the syntax for optional members by removing the need to wrap the value with Some()
using the <field>
setter like here, for example:
UPD: |
Thanks so much for all the effort you put into this! This is amazing, it looks really nice, thank you! I'll rebase it and get it passing CI. :) |
This is incomplete yet `bon` needs to support `cfg/cfg_attr` first to integrate into `comrak`
Thank you again! 🤍 |
Hi! This PR replaces the usage of
derive_builder
withbon
. What does it give us?build()
method no longer returns aResult<T>
, it returnsT
instead. So there are no potential panics and no need for error handling in runtime. For example:bon
validates that the same field isn't set twice. For example, withbon
I detected this mistake in tests wheremath_dollars
andmath_code
were set twice, becausebon
generated a compile errorcomrak/src/tests/api.rs
Lines 62 to 67 in 1a33c63
bon
generates aT::builder()
method, which removes the need for importing theTBuilder
type and usingTBuilder::default()
syntax to create the builder.bon
. Builders generated this way use the same API conventions and it is even possible to switch between struct/function syntax and keep your crate API the same (see e.g. switching from#[derive(Builder)]
to a#[builder]
on the method named "new")I understand this is a breaking change for this crate, but I think improvements are worth it.