colin-kiegel / rust-derive-builder

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

Allow specification of custom error type #191

Closed TedDriggs closed 3 years ago

TedDriggs commented 3 years ago

Crates may already have an error into which they'd like to feed builder failures. Add #[builder(build_fn(error = "..."))] to allow specifying that, and document the contract for that custom error type.

andy128k commented 3 years ago

Alternatively a simpler solution is possible. Just allow to make build() method and error type private.

#[derive(Builder)]
#[builder(error(private))]
#[builder(build_fn(name = "build_impl", private))]
struct Lorem {
    pub ipsum: u8,
}

impl LoremBuilder {
    pub fn build(&self) -> Result<Lorem, CustomError> {
        self.build_impl()
            .map_err(|private_lorem_builder_error| CustomError::new(... private_lorem_builder_error...))
    }
}