colin-kiegel / rust-derive-builder

derive builder implementation for rust structs
https://colin-kiegel.github.io/rust-derive-builder/
Apache License 2.0
1.32k stars 88 forks source link

Custom documentation for XBuilder::build #262

Closed bobbbay closed 2 years ago

bobbbay commented 2 years ago

Currently, the XBuilder::build documentation is generated as follows:


Builds a new X.

Errors

If a required field has not been initialized.


Is it possible to customize this documentation? For instance, I have a custom validation function that can fail for different reasons. I would like to mention that in my documentation.

TedDriggs commented 2 years ago

There's not support for changing the documentation through the macro, but it's trivial to create the effect you're looking for:

  1. Add #[builder(build_fn(private, name = "build_internal"))] to your struct so that derive_builder generates a build method for you but doesn't expose it externally.
  2. Add an inherent impl block for your builder in the same module as the deriving struct
  3. Add your own build method that has the same signature as build_internal
  4. Put your custom documentation on that method.

There's no reason we couldn't add a macro option for this, but it would need to be written inside quotation marks so the authoring experience would be pretty rough. For that reason, I'm inclined to think this approach of wrapping the build method is probably the better solution long-term, even if it is more code.

bobbbay commented 2 years ago

Yeah, I'm good with that solution. It's not too overcomplicated, and makes sense. Thank you!