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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! Simple-builder is a simple-as-possible implementation of the builder pattern for arbitrary Rust structs.
//! It seeks to provide an easy to use interface for common use cases, such as web request with
//! many optional parameters.
//!
//! ## Implementation
//! The derived builders offer compile-time guarantees that no incorrect instance will be created
//! without super-linear compile times since required parameters go directly in the `new` method of
//! the builder.
//!
//! There are two downsides to this:
//! - The derived builders have lengthy constructors for structs with many required parameters.
//! This is arguably no longer a builder pattern, and as such isn't suitable for structs with many
//! required parameters.
//! - Calling `build` twice will result in a panic, since it owns and consumes the passed in data.
//!
//! Other implementations may suit your needs better if you have many _required_ parameters,
//! need to clone your builders, or have strict requirements for compile-time type checking of
//! required parameters and intermediate states.
pub use simple_builder_macro::Builder;

#[derive(Builder, Debug, PartialEq)]
struct TestItemOptionals {
    a: Option<String>,
    b: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Builder, Debug, PartialEq)]
    struct TestItem {
        #[builder(required)]
        a: i32,
        #[builder(required)]
        b: i32,
    }

    #[derive(Builder, Debug, PartialEq)]
    struct TestItemOptionals {
        a: Option<String>,
        b: Option<bool>,
    }

    #[derive(Builder, Debug, PartialEq)]
    struct TestItemMixedRequiredOptionals {
        a: Option<String>,
        b: Option<bool>,
        #[builder(required)]
        c: i32,
    }

    #[derive(Builder, Debug, PartialEq)]
    struct TestItemMixedRequiredOptionalReferences<'t> {
        a: Option<&'t str>,
        #[builder(required)]
        b: &'t str,
    }

    #[test]
    fn test_developer_experience() {
        let t = trybuild::TestCases::new();
        t.compile_fail("tests/ui/*.rs");
    }

    #[test]
    fn test_builder_method_on_original_struct() {
        let expected_item = TestItem { a: 0, b: 1 };

        let mut builder = TestItem::builder(0, 1);

        let test_item = builder.build();

        assert_eq!(expected_item, test_item);
    }

    #[test]
    #[should_panic(
        expected = "Option must be Some(T) for required fields. Builder may have already been consumed by calling `build`"
    )]
    fn test_builder_panic_on_second_build_call() {
        let mut builder = TestItem::builder(0, 1);

        let _first_build = builder.build();
        let _second_build = builder.build();
    }

    #[test]
    fn test_empty_builder_none_required() {
        let expected_item = TestItem { a: 0, b: 1 };

        let mut builder = TestItemBuilder::new(0, 1);

        let test_item = builder.build();

        assert_eq!(expected_item, test_item);
    }

    #[test]
    fn test_builder_with_required() {
        let expected_item = TestItemOptionals { a: None, b: None };

        let mut builder = TestItemOptionals::builder();

        let test_item = builder.build();

        assert_eq!(expected_item, test_item);
    }

    #[test]
    fn test_builder_optional_all_present() {
        let expected_item = TestItemOptionals {
            a: Some("string".to_string()),
            b: Some(false),
        };

        let mut builder = TestItemOptionalsBuilder::new();

        let item = builder.a("string".to_string()).b(false).build();

        assert_eq!(expected_item, item);
    }

    #[test]
    fn test_builder_optional_with_none() {
        let expected_item = TestItemOptionals {
            a: None,
            b: Some(false),
        };

        let mut builder = TestItemOptionalsBuilder::new();

        let item = builder.b(false).build();

        assert_eq!(expected_item, item);
    }

    #[test]
    fn test_builder_mixed_required_and_optional() {
        let expected_item = TestItemMixedRequiredOptionals {
            a: Some("string".to_string()),
            b: None,
            c: 42,
        };

        let mut builder = TestItemMixedRequiredOptionalsBuilder::new(42);

        let item = builder.a("string".to_string()).build();

        assert_eq!(expected_item, item);
    }

    #[test]
    fn test_builder_mixed_required_and_optional_references() {
        let expected_item = TestItemMixedRequiredOptionalReferences {
            a: Some("a"),
            b: "string",
        };

        let mut builder = TestItemMixedRequiredOptionalReferencesBuilder::new("string");

        let item = builder.a("a").build();

        assert_eq!(expected_item, item);
    }

    #[test]
    fn test_builder_ownership() {
        #[derive(Debug, PartialEq, Eq)]
        struct Item {
            field: i32,
        }

        #[derive(Builder, Debug, Eq, PartialEq)]
        struct TestItem {
            a: Option<Item>,
            #[builder(required)]
            b: Item,
        }

        let a = Item { field: 0 };
        let a1 = Item { field: 0 };

        let b = Item { field: 1 };
        let b1 = Item { field: 1 };

        let mut builder = TestItem::builder(b);

        let item = builder.a(a).build();

        let expected_item = TestItem { a: Some(a1), b: b1 };

        assert_eq!(expected_item, item);
    }

    #[test]
    fn test_builder_with_required_trait_and_lifetimes() {
        trait Marker {}

        #[derive(Debug, Eq, PartialEq, PartialOrd)]
        struct GenericT {
            num: i64,
        }

        impl Marker for GenericT {}

        #[derive(Builder, Debug, Eq, PartialEq)]
        struct TestItemGenericAddress<'t, 'u, T, U>
        where
            T: Marker,
            U: Marker,
        {
            a: Option<&'t T>,
            #[builder(required)]
            b: &'u U,
        }

        let test_struct_t = GenericT { num: 42 };
        let test_struct_u = GenericT { num: 1 };

        let expected_item = TestItemGenericAddress {
            a: Some(&test_struct_t),
            b: &test_struct_u,
        };

        let mut builder = TestItemGenericAddress::builder(&test_struct_u);

        let item = builder.a(&test_struct_t).build();

        assert_eq!(expected_item, item);
    }
}