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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
/// Temporary shim to allow new and old endpoint resolvers to co-exist
///
/// This enables converting from the actual parameters type to the placeholder parameters type that
/// contains a region
#[doc(hidden)]
impl From<crate::endpoint::Params> for aws_endpoint::Params {
    fn from(params: crate::endpoint::Params) -> Self {
        Self::new(
            params
                .region()
                .map(|r| aws_types::region::Region::new(r.to_string())),
        )
    }
}

/// Generated endpoint tests
#[cfg(test)]
mod test {

    /// For region aws-cn-global with FIPS disabled and DualStack disabled
    #[test]
    fn test_1() {
        use aws_smithy_http::endpoint::ResolveEndpoint;
        let params = crate::endpoint::Params::builder()
            .use_fips(false)
            .region("aws-cn-global".to_string())
            .use_dual_stack(false)
            .build()
            .expect("invalid params");
        let resolver = crate::endpoint::DefaultResolver::new();
        let endpoint = resolver.resolve_endpoint(&params);
        let endpoint = endpoint
            .expect("Expected valid endpoint: https://cloudfront.cn-northwest-1.amazonaws.com.cn");
        assert_eq!(
            endpoint,
            aws_smithy_types::endpoint::Endpoint::builder()
                .url("https://cloudfront.cn-northwest-1.amazonaws.com.cn")
                .property(
                    "authSchemes",
                    vec![aws_smithy_types::Document::from({
                        let mut out =
                            std::collections::HashMap::<String, aws_smithy_types::Document>::new();
                        out.insert("name".to_string(), "sigv4".to_string().into());
                        out.insert("signingName".to_string(), "cloudfront".to_string().into());
                        out.insert(
                            "signingRegion".to_string(),
                            "cn-northwest-1".to_string().into(),
                        );
                        out
                    })]
                )
                .build()
        );
    }

    /// For region aws-global with FIPS disabled and DualStack disabled
    #[test]
    fn test_2() {
        use aws_smithy_http::endpoint::ResolveEndpoint;
        let params = crate::endpoint::Params::builder()
            .use_fips(false)
            .region("aws-global".to_string())
            .use_dual_stack(false)
            .build()
            .expect("invalid params");
        let resolver = crate::endpoint::DefaultResolver::new();
        let endpoint = resolver.resolve_endpoint(&params);
        let endpoint = endpoint.expect("Expected valid endpoint: https://cloudfront.amazonaws.com");
        assert_eq!(
            endpoint,
            aws_smithy_types::endpoint::Endpoint::builder()
                .url("https://cloudfront.amazonaws.com")
                .property(
                    "authSchemes",
                    vec![aws_smithy_types::Document::from({
                        let mut out =
                            std::collections::HashMap::<String, aws_smithy_types::Document>::new();
                        out.insert("name".to_string(), "sigv4".to_string().into());
                        out.insert("signingName".to_string(), "cloudfront".to_string().into());
                        out.insert("signingRegion".to_string(), "us-east-1".to_string().into());
                        out
                    })]
                )
                .build()
        );
    }

    /// For custom endpoint with fips disabled and dualstack disabled
    #[test]
    fn test_3() {
        use aws_smithy_http::endpoint::ResolveEndpoint;
        let params = crate::endpoint::Params::builder()
            .use_fips(false)
            .region("us-east-1".to_string())
            .use_dual_stack(false)
            .endpoint("https://example.com".to_string())
            .build()
            .expect("invalid params");
        let resolver = crate::endpoint::DefaultResolver::new();
        let endpoint = resolver.resolve_endpoint(&params);
        let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
        assert_eq!(
            endpoint,
            aws_smithy_types::endpoint::Endpoint::builder()
                .url("https://example.com")
                .build()
        );
    }

    /// For custom endpoint with fips enabled and dualstack disabled
    #[test]
    fn test_4() {
        use aws_smithy_http::endpoint::ResolveEndpoint;
        let params = crate::endpoint::Params::builder()
            .use_fips(true)
            .region("us-east-1".to_string())
            .use_dual_stack(false)
            .endpoint("https://example.com".to_string())
            .build()
            .expect("invalid params");
        let resolver = crate::endpoint::DefaultResolver::new();
        let endpoint = resolver.resolve_endpoint(&params);
        let error = endpoint.expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled and dualstack disabled]");
        assert_eq!(
            format!("{}", error),
            "Invalid Configuration: FIPS and custom endpoint are not supported"
        )
    }

    /// For custom endpoint with fips disabled and dualstack enabled
    #[test]
    fn test_5() {
        use aws_smithy_http::endpoint::ResolveEndpoint;
        let params = crate::endpoint::Params::builder()
            .use_fips(false)
            .region("us-east-1".to_string())
            .use_dual_stack(true)
            .endpoint("https://example.com".to_string())
            .build()
            .expect("invalid params");
        let resolver = crate::endpoint::DefaultResolver::new();
        let endpoint = resolver.resolve_endpoint(&params);
        let error = endpoint.expect_err("expected error: Invalid Configuration: Dualstack and custom endpoint are not supported [For custom endpoint with fips disabled and dualstack enabled]");
        assert_eq!(
            format!("{}", error),
            "Invalid Configuration: Dualstack and custom endpoint are not supported"
        )
    }
}

#[non_exhaustive]
#[derive(std::clone::Clone, std::cmp::PartialEq, std::fmt::Debug)]
/// Configuration parameters for resolving the correct endpoint
pub struct Params {
    /// The AWS region used to dispatch the request.
    pub(crate) region: std::string::String,
    /// When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.
    pub(crate) use_dual_stack: bool,
    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
    pub(crate) use_fips: bool,
    /// Override the endpoint used to send this request
    pub(crate) endpoint: std::option::Option<std::string::String>,
}
impl Params {
    /// Create a builder for [`Params`]
    pub fn builder() -> crate::endpoint::ParamsBuilder {
        crate::endpoint::ParamsBuilder::default()
    }
    /// The AWS region used to dispatch the request.
    pub fn region(&self) -> std::option::Option<&str> {
        Some(&self.region)
    }
    /// When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.
    pub fn use_dual_stack(&self) -> std::option::Option<bool> {
        Some(self.use_dual_stack)
    }
    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
    pub fn use_fips(&self) -> std::option::Option<bool> {
        Some(self.use_fips)
    }
    /// Override the endpoint used to send this request
    pub fn endpoint(&self) -> std::option::Option<&str> {
        self.endpoint.as_deref()
    }
}

/// The default endpoint resolver
#[derive(Default)]
pub struct DefaultResolver {
    partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
}

impl DefaultResolver {
    /// Create a new endpoint resolver with default settings
    pub fn new() -> Self {
        Self { partition_resolver: crate::endpoint_lib::partition::PartitionResolver::new_from_json(b"{\"version\":\"1.1\",\"partitions\":[{\"id\":\"aws\",\"regionRegex\":\"^(us|eu|ap|sa|ca|me|af)-\\\\w+-\\\\d+$\",\"regions\":{\"af-south-1\":{},\"ap-east-1\":{},\"ap-northeast-1\":{},\"ap-northeast-2\":{},\"ap-northeast-3\":{},\"ap-south-1\":{},\"ap-southeast-1\":{},\"ap-southeast-2\":{},\"ap-southeast-3\":{},\"ca-central-1\":{},\"eu-central-1\":{},\"eu-north-1\":{},\"eu-south-1\":{},\"eu-west-1\":{},\"eu-west-2\":{},\"eu-west-3\":{},\"me-central-1\":{},\"me-south-1\":{},\"sa-east-1\":{},\"us-east-1\":{},\"us-east-2\":{},\"us-west-1\":{},\"us-west-2\":{},\"aws-global\":{}},\"outputs\":{\"name\":\"aws\",\"dnsSuffix\":\"amazonaws.com\",\"dualStackDnsSuffix\":\"api.aws\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-us-gov\",\"regionRegex\":\"^us\\\\-gov\\\\-\\\\w+\\\\-\\\\d+$\",\"regions\":{\"us-gov-west-1\":{},\"us-gov-east-1\":{},\"aws-us-gov-global\":{}},\"outputs\":{\"name\":\"aws-us-gov\",\"dnsSuffix\":\"amazonaws.com\",\"dualStackDnsSuffix\":\"api.aws\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-cn\",\"regionRegex\":\"^cn\\\\-\\\\w+\\\\-\\\\d+$\",\"regions\":{\"cn-north-1\":{},\"cn-northwest-1\":{},\"aws-cn-global\":{}},\"outputs\":{\"name\":\"aws-cn\",\"dnsSuffix\":\"amazonaws.com.cn\",\"dualStackDnsSuffix\":\"api.amazonwebservices.com.cn\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-iso\",\"regionRegex\":\"^us\\\\-iso\\\\-\\\\w+\\\\-\\\\d+$\",\"outputs\":{\"name\":\"aws-iso\",\"dnsSuffix\":\"c2s.ic.gov\",\"supportsFIPS\":true,\"supportsDualStack\":false,\"dualStackDnsSuffix\":\"c2s.ic.gov\"},\"regions\":{\"us-iso-east-1\":{},\"us-iso-west-1\":{},\"aws-iso-global\":{}}},{\"id\":\"aws-iso-b\",\"regionRegex\":\"^us\\\\-isob\\\\-\\\\w+\\\\-\\\\d+$\",\"outputs\":{\"name\":\"aws-iso-b\",\"dnsSuffix\":\"sc2s.sgov.gov\",\"supportsFIPS\":true,\"supportsDualStack\":false,\"dualStackDnsSuffix\":\"sc2s.sgov.gov\"},\"regions\":{\"us-isob-east-1\":{},\"aws-iso-b-global\":{}}}]}").expect("valid JSON") }
    }
}

impl aws_smithy_http::endpoint::ResolveEndpoint<crate::endpoint::Params> for DefaultResolver {
    fn resolve_endpoint(&self, params: &Params) -> aws_smithy_http::endpoint::Result {
        let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
        crate::endpoint::internals::resolve_endpoint(
            params,
            &mut diagnostic_collector,
            &self.partition_resolver,
        )
        .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))
    }
}

/// Builder for [`Params`]
#[derive(std::clone::Clone, std::cmp::PartialEq, std::default::Default, std::fmt::Debug)]
pub struct ParamsBuilder {
    region: std::option::Option<std::string::String>,
    use_dual_stack: std::option::Option<bool>,
    use_fips: std::option::Option<bool>,
    endpoint: std::option::Option<std::string::String>,
}
impl ParamsBuilder {
    /// Consume this builder, creating [`Params`].
    pub fn build(self) -> Result<crate::endpoint::Params, crate::endpoint::InvalidParams> {
        Ok(
            #[allow(clippy::unnecessary_lazy_evaluations)]
            crate::endpoint::Params {
                region: self
                    .region
                    .ok_or_else(|| crate::endpoint::InvalidParams::missing("region"))?,
                use_dual_stack: self
                    .use_dual_stack
                    .or_else(|| Some(false))
                    .ok_or_else(|| crate::endpoint::InvalidParams::missing("use_dual_stack"))?,
                use_fips: self
                    .use_fips
                    .or_else(|| Some(false))
                    .ok_or_else(|| crate::endpoint::InvalidParams::missing("use_fips"))?,
                endpoint: self.endpoint,
            },
        )
    }
    /// Sets the value for region
    ///
    /// The AWS region used to dispatch the request.
    pub fn region(mut self, value: impl Into<std::string::String>) -> Self {
        self.region = Some(value.into());
        self
    }

    /// Sets the value for region
    ///
    /// The AWS region used to dispatch the request.
    pub fn set_region(mut self, param: Option<std::string::String>) -> Self {
        self.region = param;
        self
    }
    /// Sets the value for use_dual_stack
    ///
    /// When unset, this parameter has a default value of `false`.
    /// When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.
    pub fn use_dual_stack(mut self, value: impl Into<bool>) -> Self {
        self.use_dual_stack = Some(value.into());
        self
    }

    /// Sets the value for use_dual_stack
    ///
    /// When unset, this parameter has a default value of `false`.
    /// When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.
    pub fn set_use_dual_stack(mut self, param: Option<bool>) -> Self {
        self.use_dual_stack = param;
        self
    }
    /// Sets the value for use_fips
    ///
    /// When unset, this parameter has a default value of `false`.
    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
    pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
        self.use_fips = Some(value.into());
        self
    }

    /// Sets the value for use_fips
    ///
    /// When unset, this parameter has a default value of `false`.
    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
    pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
        self.use_fips = param;
        self
    }
    /// Sets the value for endpoint
    ///
    /// Override the endpoint used to send this request
    pub fn endpoint(mut self, value: impl Into<std::string::String>) -> Self {
        self.endpoint = Some(value.into());
        self
    }

    /// Sets the value for endpoint
    ///
    /// Override the endpoint used to send this request
    pub fn set_endpoint(mut self, param: Option<std::string::String>) -> Self {
        self.endpoint = param;
        self
    }
}

/// An error that occurred during endpoint resolution
#[derive(Debug)]
pub struct InvalidParams {
    field: std::borrow::Cow<'static, str>,
}

impl InvalidParams {
    #[allow(dead_code)]
    fn missing(field: &'static str) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl std::fmt::Display for InvalidParams {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "a required field was missing: `{}`", self.field)
    }
}

impl std::error::Error for InvalidParams {}

/// Endpoints internals
mod internals;