aws_types/
endpoint_config.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Newtypes for endpoint-related parameters
7//!
8//! Parameters require newtypes so they have distinct types when stored in layers in config bag.
9
10use std::fmt;
11use std::str::FromStr;
12
13use aws_smithy_types::config_bag::{Storable, StoreReplace};
14
15/// Newtype for `use_fips`
16#[derive(Clone, Debug)]
17pub struct UseFips(pub bool);
18impl Storable for UseFips {
19    type Storer = StoreReplace<UseFips>;
20}
21
22/// Newtype for `use_dual_stack`
23#[derive(Clone, Debug)]
24pub struct UseDualStack(pub bool);
25impl Storable for UseDualStack {
26    type Storer = StoreReplace<UseDualStack>;
27}
28
29/// Newtype for `endpoint_url`
30#[derive(Clone, Debug)]
31pub struct EndpointUrl(pub String);
32impl Storable for EndpointUrl {
33    type Storer = StoreReplace<EndpointUrl>;
34}
35
36const PREFERRED: &str = "preferred";
37const DISABLED: &str = "disabled";
38const REQUIRED: &str = "required";
39
40/// Setting to control the account ID-based routing behavior.
41#[non_exhaustive]
42#[derive(Debug, Clone, PartialEq, Eq, Default)]
43pub enum AccountIdEndpointMode {
44    /// The endpoint should include account ID if available.
45    #[default]
46    Preferred,
47    /// A resolved endpoint does not include account ID.
48    Disabled,
49    /// The endpoint must include account ID. If the account ID isn't available, the SDK throws an error.
50    Required,
51}
52
53impl AccountIdEndpointMode {
54    fn all_variants() -> [AccountIdEndpointMode; 3] {
55        use AccountIdEndpointMode::*;
56        [Preferred, Disabled, Required]
57    }
58}
59
60impl Storable for AccountIdEndpointMode {
61    type Storer = StoreReplace<Self>;
62}
63
64impl fmt::Display for AccountIdEndpointMode {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
66        use AccountIdEndpointMode::*;
67        write!(
68            f,
69            "{}",
70            match self {
71                Preferred => "preferred",
72                Disabled => "disabled",
73                Required => "required",
74            }
75        )
76    }
77}
78
79impl FromStr for AccountIdEndpointMode {
80    type Err = AccountIdEndpointModeParseError;
81
82    fn from_str(mode_str: &str) -> Result<Self, Self::Err> {
83        if mode_str.eq_ignore_ascii_case(PREFERRED) {
84            Ok(Self::Preferred)
85        } else if mode_str.eq_ignore_ascii_case(DISABLED) {
86            Ok(Self::Disabled)
87        } else if mode_str.eq_ignore_ascii_case(REQUIRED) {
88            Ok(Self::Required)
89        } else {
90            Err(AccountIdEndpointModeParseError::new(mode_str))
91        }
92    }
93}
94
95/// Error encountered when failing to parse a string into [`AccountIdEndpointMode`].
96#[derive(Debug)]
97pub struct AccountIdEndpointModeParseError {
98    mode_string: String,
99}
100
101impl AccountIdEndpointModeParseError {
102    fn new(mode_string: impl Into<String>) -> Self {
103        Self {
104            mode_string: mode_string.into(),
105        }
106    }
107}
108
109impl fmt::Display for AccountIdEndpointModeParseError {
110    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111        write!(
112            f,
113            "error parsing string `{}` as `AccountIdEndpointMode`, valid options are: {:#?}",
114            self.mode_string,
115            AccountIdEndpointMode::all_variants().map(|mode| mode.to_string())
116        )
117    }
118}
119
120impl std::error::Error for AccountIdEndpointModeParseError {}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125
126    #[test]
127    fn parse_ok_account_id_endpoint_mode() {
128        assert_eq!(
129            AccountIdEndpointMode::Preferred,
130            AccountIdEndpointMode::from_str("preferred").unwrap()
131        );
132        assert_eq!(
133            AccountIdEndpointMode::Disabled,
134            AccountIdEndpointMode::from_str("disabled").unwrap()
135        );
136        assert_eq!(
137            AccountIdEndpointMode::Required,
138            AccountIdEndpointMode::from_str("required").unwrap()
139        );
140    }
141
142    #[test]
143    fn parse_err_account_id_endpoint_mode() {
144        let err = AccountIdEndpointMode::from_str("invalid").err().unwrap();
145        assert_eq!(
146            r#"error parsing string `invalid` as `AccountIdEndpointMode`, valid options are: [
147    "preferred",
148    "disabled",
149    "required",
150]"#,
151            format!("{err}")
152        );
153    }
154}