comrak icon indicating copy to clipboard operation
comrak copied to clipboard

Use `bon` for an infallible and compile-time-checked builder

Open Veetaha opened this issue 1 year ago • 1 comments

Hi! This PR replaces the usage of derive_builder with bon. What does it give us?

  • The build() method no longer returns a Result<T>, it returns T instead. So there are no potential panics and no need for error handling in runtime. For example: image
  • The builder generated by bon validates that the same field isn't set twice. For example, with bon I detected this mistake in tests where math_dollars and math_code were set twice, because bon generated a compile error https://github.com/kivikakk/comrak/blob/1a33c63688dcfabc0bb5e61fc043765d20debf83/src/tests/api.rs#L62-L67
  • bon generates a T::builder() method, which removes the need for importing the TBuilder type and using TBuilder::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.

Veetaha avatar Sep 08 '24 23:09 Veetaha

UPD: bon 2.2.1 added support for MSRV 1.59.0

Veetaha avatar Sep 10 '24 00:09 Veetaha

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. :)

kivikakk avatar Oct 22 '24 06:10 kivikakk

Thank you again! 🤍

kivikakk avatar Oct 22 '24 06:10 kivikakk