1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! A procedural macro which creates a "staged" builder for a type.
//!
//! Staged (also known as telescopic) builders are a style of infallible builders; code will not compile if any required
//! fields are not set. Specifically, the builder advances in order through a sequence of "stage" types, each
//! corresponding to a required field of the struct. The final stage has setters for all optional fields and the final
//! `.build()` method.
//!
//! See the documentation for [`#[staged_builder]`](staged_builder) for more details.
//!
//! # Examples
//!
//! ```
//! use staged_builder::staged_builder;
//!
//! #[staged_builder]
//! struct Person {
//!     #[builder(into)]
//!     name: String,
//!     age: u32,
//!     #[builder(list(item(type = Person)))]
//!     parents: Vec<Person>,
//! }
//!
//! # fn main() {
//! let person = Person::builder()
//!     .name("John Doe")
//!     .age(25)
//!     .build();
//! # }
//! ```
#![cfg_attr(not(doc), no_std)]

// Not part of the public API.
#[doc(hidden)]
pub use staged_builder_internals::__StagedBuilderInternalDerive;
#[doc(inline)]
pub use staged_builder_internals::staged_builder;

// Not part of the public API.
#[doc(hidden)]
pub mod __private {
    pub use core::convert::{From, Into};
    pub use core::default::Default;
    pub use core::iter::{Extend, FromIterator, IntoIterator, Iterator};
    pub use core::result::Result;

    #[inline]
    pub fn call_hack<T, R>(f: impl FnOnce(T) -> R, v: T) -> R {
        f(v)
    }
}

/// A trait for types which validate their state before construction finishes.
///
/// The generated builder will call this method if the `#[builder(validate)]` attribute is placed at the struct level.
pub trait Validate {
    /// The error returned for an invalid value.
    type Error;

    /// Validates the state of `self`.
    fn validate(&self) -> Result<(), Self::Error>;
}

/// An example type using [`#[staged_builder]`](staged_builder).
#[cfg(doc)]
#[staged_builder]
#[builder(crate = crate)]
pub struct ExamplePerson {
    #[builder(into)]
    pub name: String,
    pub age: u32,
    #[builder(list(item(type = ExamplePerson)))]
    pub parents: Vec<ExamplePerson>,
}