Skip to main content

bon_sandbox/state_mod/
comprehensive.rs

1//! Comprehensive example of the generated builder and its typestate API.
2//!
3//! The preliminary reading of [Typestate API](https://bon-rs.com/guide/typestate-api)
4//! guide is recommended to understand how the pieces in this example fit together.
5//!
6//! This module contains a struct [`Example`] that was annotated with [`#[derive(Builder)]`](bon::Builder).
7//! The config [`#[builder(state_mod(vis = "pub"))]`](https://bon-rs.com/reference/builder/top-level/state_mod)
8//! was applied to make the generated builder's typestate API public and visible here in the docs.
9//!
10//! The following was generated by the macro:
11//! - [`ExampleBuilder`] - the builder struct itself
12//! - [`example_builder`] - the builder's typestate API module
13
14/// Example struct with the `#[derive(Builder)]` annotation.
15#[derive(bon::Builder)]
16#[builder(state_mod(vis = "pub"))]
17pub struct Example {
18    required: u32,
19
20    optional: Option<u32>,
21
22    #[builder(default)]
23    default: u32,
24
25    #[builder(overwritable)]
26    overwritable_required: u32,
27
28    #[builder(overwritable)]
29    overwritable_optional: Option<u32>,
30
31    #[builder(overwritable, default = 2 * 2 + 3)]
32    overwritable_default: u32,
33
34    #[builder(required)]
35    required_option: Option<u64>,
36
37    /// # Errors
38    ///
39    /// Non-integer strings will be rejected.
40    #[builder(with = |x: &str| -> Result<_, std::num::ParseIntError> { x.parse() })]
41    with: u32,
42}