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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
//! Various utility macros to aid runtime crate writers.
/// Define a new builder struct, along with a method to create it, and setters.
///
/// ## Examples
///
/// The builder macro takes a list of field definitions, each with four elements:
/// ```txt
/// set_optional_field, optional_field, String, "An optional field which may or may not be set when `.build()` is called.",
/// ^ The setter name, ^ The field name, ^ The type, ^ The documentation for the field and setter methods.
/// ```
///
/// The following example creates a new builder struct, along with a method to create it, and setters
/// for a struct `MyConfig` with three fields:
///
/// ```
/// use std::collections::HashMap;
/// use std::sync::{Arc, Mutex};
/// use aws_smithy_runtime_api::{builder, builder_methods, builder_struct};
///
/// struct MyConfig {
/// optional_field: Option<String>,
/// optional_field_with_a_default: f64,
/// required_field_with_no_default: Arc<Mutex<HashMap<String, String>>>,
/// }
///
/// impl MyConfig {
/// pub fn builder() -> Builder {
/// Builder::new()
/// }
/// }
///
/// builder!(
/// set_optional_field, optional_field, String, "An optional field which may or may not be set when `.build()` is called.",
/// set_optional_field_with_a_default, optional_field_with_a_default, f64, "An optional field that will default to `f64::MAX` if it's unset when `.build()` is called.",
/// set_required_field_with_no_default, required_field_with_no_default, HashMap<String, String>, "A required field that will cause the builder to panic if it's unset when `.build()` is called."
/// );
///
/// impl Builder {
/// fn build(self) -> MyConfig {
/// MyConfig {
/// optional_field: self.optional_field,
/// optional_field_with_a_default: self.optional_field_with_a_default.unwrap_or(f64::MAX),
/// required_field_with_no_default: Arc::new(Mutex::new(
/// self.required_field_with_no_default.expect("'required_field_with_no_default' is required")
/// )),
/// }
/// }
/// }
/// ```
///
/// In this example, the result of macro expansion would look like this:
///
/// ```
/// # use std::collections::HashMap;
/// # use std::sync::{Arc, Mutex};
/// #[derive(Clone, Debug, Default)]
/// pub struct Builder {
/// #[doc = "An optional field which may or may not be set when `.build()` is called."]
/// optional_field: Option<String>,
/// #[doc = "An optional field that will default to `f64::MAX` if it's unset when `.build()` is called."]
/// optional_field_with_a_default: Option<f64>,
/// #[doc = "A required field that will cause the builder to panic if it's unset when `.build()` is called."]
/// required_field_with_no_default: Option<HashMap<String, String>>,
/// }
///
/// impl Builder {
/// pub fn new() -> Self {
/// Builder::default()
/// }
///
/// #[doc = "An optional field which may or may not be set when `.build()` is called."]
/// pub fn set_optional_field(&mut self, optional_field: Option<String>) -> &mut Self {
/// self.optional_field = optional_field;
/// self
/// }
///
/// #[doc = "An optional field which may or may not be set when `.build()` is called."]
/// pub fn optional_field(mut self, optional_field: String) -> Self {
/// self.optional_field = Some(optional_field);
/// self
/// }
///
/// #[doc = "An optional field that will default to `f64::MAX` if it's unset when `.build()` is called."]
/// pub fn set_optional_field_with_a_default(&mut self, optional_field_with_a_default: Option<f64>) -> &mut Self {
/// self.optional_field_with_a_default = optional_field_with_a_default;
/// self
/// }
///
/// #[doc = "An optional field that will default to `f64::MAX` if it's unset when `.build()` is called."]
/// pub fn optional_field_with_a_default(mut self, optional_field_with_a_default: f64) -> Self {
/// self.optional_field_with_a_default = Some(optional_field_with_a_default);
/// self
/// }
///
/// #[doc = "A required field that will cause the builder to panic if it's unset when `.build()` is called."]
/// pub fn set_required_field_with_no_default(&mut self, required_field_with_no_default: Option<HashMap<String, String>>) -> &mut Self {
/// self.required_field_with_no_default = required_field_with_no_default;
/// self
/// }
///
/// #[doc = "A required field that will cause the builder to panic if it's unset when `.build()` is called."]
/// pub fn required_field_with_no_default(mut self, required_field_with_no_default: HashMap<String, String>) -> Self {
/// self.required_field_with_no_default = Some(required_field_with_no_default);
/// self
/// }
/// }
/// ```
/// Define a new builder struct, its fields, and their docs. This macro is intended to be called
/// by the `builder!` macro and should not be called directly.
}
}
/// Define setter methods for a builder struct. Must be called from within an `impl` block. This
/// macro is intended to be called by the `builder!` macro and should not be called directly.