comrak
comrak copied to clipboard
Use `bon` for an infallible and compile-time-checked builder
Hi! This PR replaces the usage of derive_builder with bon. What does it give us?
- The
build()method no longer returns aResult<T>, it returnsTinstead. So there are no potential panics and no need for error handling in runtime. For example: - The builder generated by
bonvalidates that the same field isn't set twice. For example, withbonI detected this mistake in tests wheremath_dollarsandmath_codewere set twice, becausebongenerated a compile error https://github.com/kivikakk/comrak/blob/1a33c63688dcfabc0bb5e61fc043765d20debf83/src/tests/api.rs#L62-L67 -
bongenerates aT::builder()method, which removes the need for importing theTBuildertype and usingTBuilder::default()syntax to create the builder. - If you ever need some more complex logic of building, or you want to avoid create a separate "parameters" struct, you can always generate a builder from a function or from a method with
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.
UPD: bon 2.2.1 added support for MSRV 1.59.0
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. :)
Thank you again! 🤍