aws_smithy_types/
checksum_config.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Types that allow users to indicate their preferences for checksum calculation and validation
7
8// Note: These types would likely make more sense in `aws-smithy-checksums` and were originally
9// added there. But we have lints protecting against exporting non-stable types from stable crates
10// and the checksums crate is not yet 1.0, so these types cannot live there for now. In the future
11// if we do decide to 1.0 the checksums crate we can move these types there and re-export them here
12// to maintain the current behavior.
13
14use std::error::Error;
15use std::fmt;
16use std::str::FromStr;
17
18use crate::config_bag::{Storable, StoreReplace};
19
20// Valid names for RequestChecksumCalculation and ResponseChecksumValidation
21const WHEN_SUPPORTED: &str = "when_supported";
22const WHEN_REQUIRED: &str = "when_required";
23
24/// Determines when a checksum will be calculated for request payloads. Values are:
25/// * [RequestChecksumCalculation::WhenSupported] - (default) When set, a checksum will be
26///   calculated for all request payloads of operations modeled with the
27///   `httpChecksum` trait where `requestChecksumRequired` is `true` and/or a
28///   `requestAlgorithmMember` is modeled.
29/// * [RequestChecksumCalculation::WhenRequired] - When set, a checksum will only be calculated for
30///   request payloads of operations modeled with the  `httpChecksum` trait where
31///   `requestChecksumRequired` is `true` or where a requestAlgorithmMember
32///   is modeled and supplied.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
34#[non_exhaustive]
35pub enum RequestChecksumCalculation {
36    /// Calculate request checksums when they are supported.
37    #[default]
38    WhenSupported,
39    /// Caulculate request checksums only when they are required.
40    WhenRequired,
41}
42
43impl Storable for RequestChecksumCalculation {
44    type Storer = StoreReplace<Self>;
45}
46
47impl FromStr for RequestChecksumCalculation {
48    type Err = UnknownRequestChecksumCalculationError;
49
50    fn from_str(request_checksum_calculation: &str) -> Result<Self, Self::Err> {
51        if request_checksum_calculation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
52            Ok(Self::WhenSupported)
53        } else if request_checksum_calculation.eq_ignore_ascii_case(WHEN_REQUIRED) {
54            Ok(Self::WhenRequired)
55        } else {
56            Err(UnknownRequestChecksumCalculationError::new(
57                request_checksum_calculation,
58            ))
59        }
60    }
61}
62
63/// Determines when checksum validation will be performed on response payloads. Values are:
64/// * [ResponseChecksumValidation::WhenSupported] - (default) When set, checksum validation is performed on all
65///   response payloads of operations modeled with the `httpChecksum` trait where
66///   `responseAlgorithms` is modeled, except when no modeled checksum algorithms
67///   are supported.
68/// * [ResponseChecksumValidation::WhenRequired] - When set, checksum validation is not performed on
69///   response payloads of operations unless the checksum algorithm is supported and
70///   the `requestValidationModeMember` member is set to `ENABLED`.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
72#[non_exhaustive]
73pub enum ResponseChecksumValidation {
74    /// Validate response checksums when they are supported.
75    #[default]
76    WhenSupported,
77    /// Validate response checksums only when they are required.
78    WhenRequired,
79}
80
81impl Storable for ResponseChecksumValidation {
82    type Storer = StoreReplace<Self>;
83}
84
85impl FromStr for ResponseChecksumValidation {
86    type Err = UnknownResponseChecksumValidationError;
87
88    fn from_str(response_checksum_validation: &str) -> Result<Self, Self::Err> {
89        if response_checksum_validation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
90            Ok(Self::WhenSupported)
91        } else if response_checksum_validation.eq_ignore_ascii_case(WHEN_REQUIRED) {
92            Ok(Self::WhenRequired)
93        } else {
94            Err(UnknownResponseChecksumValidationError::new(
95                response_checksum_validation,
96            ))
97        }
98    }
99}
100
101/// Unknown setting for `request_checksum_calculation`
102#[derive(Debug)]
103#[non_exhaustive]
104pub struct UnknownRequestChecksumCalculationError {
105    request_checksum_calculation: String,
106}
107
108impl UnknownRequestChecksumCalculationError {
109    pub(crate) fn new(request_checksum_calculation: impl Into<String>) -> Self {
110        Self {
111            request_checksum_calculation: request_checksum_calculation.into(),
112        }
113    }
114
115    /// The unknown value
116    pub fn request_checksum_calculation(&self) -> &str {
117        &self.request_checksum_calculation
118    }
119}
120
121impl fmt::Display for UnknownRequestChecksumCalculationError {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        write!(
124            f,
125            r#"unknown request_checksum_calculation value "{}", please pass a known name ("when_supported", "when_required")"#,
126            self.request_checksum_calculation
127        )
128    }
129}
130
131impl Error for UnknownRequestChecksumCalculationError {}
132
133/// Unknown setting for `response_checksum_validation`
134#[derive(Debug)]
135#[non_exhaustive]
136pub struct UnknownResponseChecksumValidationError {
137    response_checksum_validation: String,
138}
139
140impl UnknownResponseChecksumValidationError {
141    pub(crate) fn new(response_checksum_validation: impl Into<String>) -> Self {
142        Self {
143            response_checksum_validation: response_checksum_validation.into(),
144        }
145    }
146
147    /// The unknown value
148    pub fn response_checksum_validation(&self) -> &str {
149        &self.response_checksum_validation
150    }
151}
152
153impl fmt::Display for UnknownResponseChecksumValidationError {
154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155        write!(
156            f,
157            r#"unknown response_checksum_validation value "{}", please pass a known name ("when_supported", "when_required")"#,
158            self.response_checksum_validation
159        )
160    }
161}
162
163impl Error for UnknownResponseChecksumValidationError {}