1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
56//! Types that allow users to indicate their preferences for checksum calculation and validation
78// 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.
1314use std::error::Error;
15use std::fmt;
16use std::str::FromStr;
1718use crate::config_bag::{Storable, StoreReplace};
1920// Valid names for RequestChecksumCalculation and ResponseChecksumValidation
21const WHEN_SUPPORTED: &str = "when_supported";
22const WHEN_REQUIRED: &str = "when_required";
2324/// 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]
38WhenSupported,
39/// Caulculate request checksums only when they are required.
40WhenRequired,
41}
4243impl Storable for RequestChecksumCalculation {
44type Storer = StoreReplace<Self>;
45}
4647impl FromStr for RequestChecksumCalculation {
48type Err = UnknownRequestChecksumCalculationError;
4950fn from_str(request_checksum_calculation: &str) -> Result<Self, Self::Err> {
51if request_checksum_calculation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
52Ok(Self::WhenSupported)
53 } else if request_checksum_calculation.eq_ignore_ascii_case(WHEN_REQUIRED) {
54Ok(Self::WhenRequired)
55 } else {
56Err(UnknownRequestChecksumCalculationError::new(
57 request_checksum_calculation,
58 ))
59 }
60 }
61}
6263/// 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]
76WhenSupported,
77/// Validate response checksums only when they are required.
78WhenRequired,
79}
8081impl Storable for ResponseChecksumValidation {
82type Storer = StoreReplace<Self>;
83}
8485impl FromStr for ResponseChecksumValidation {
86type Err = UnknownResponseChecksumValidationError;
8788fn from_str(response_checksum_validation: &str) -> Result<Self, Self::Err> {
89if response_checksum_validation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
90Ok(Self::WhenSupported)
91 } else if response_checksum_validation.eq_ignore_ascii_case(WHEN_REQUIRED) {
92Ok(Self::WhenRequired)
93 } else {
94Err(UnknownResponseChecksumValidationError::new(
95 response_checksum_validation,
96 ))
97 }
98 }
99}
100101/// Unknown setting for `request_checksum_calculation`
102#[derive(Debug)]
103#[non_exhaustive]
104pub struct UnknownRequestChecksumCalculationError {
105 request_checksum_calculation: String,
106}
107108impl UnknownRequestChecksumCalculationError {
109pub(crate) fn new(request_checksum_calculation: impl Into<String>) -> Self {
110Self {
111 request_checksum_calculation: request_checksum_calculation.into(),
112 }
113 }
114115/// The unknown value
116pub fn request_checksum_calculation(&self) -> &str {
117&self.request_checksum_calculation
118 }
119}
120121impl fmt::Display for UnknownRequestChecksumCalculationError {
122fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123write!(
124 f,
125r#"unknown request_checksum_calculation value "{}", please pass a known name ("when_supported", "when_required")"#,
126self.request_checksum_calculation
127 )
128 }
129}
130131impl Error for UnknownRequestChecksumCalculationError {}
132133/// Unknown setting for `response_checksum_validation`
134#[derive(Debug)]
135#[non_exhaustive]
136pub struct UnknownResponseChecksumValidationError {
137 response_checksum_validation: String,
138}
139140impl UnknownResponseChecksumValidationError {
141pub(crate) fn new(response_checksum_validation: impl Into<String>) -> Self {
142Self {
143 response_checksum_validation: response_checksum_validation.into(),
144 }
145 }
146147/// The unknown value
148pub fn response_checksum_validation(&self) -> &str {
149&self.response_checksum_validation
150 }
151}
152153impl fmt::Display for UnknownResponseChecksumValidationError {
154fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155write!(
156 f,
157r#"unknown response_checksum_validation value "{}", please pass a known name ("when_supported", "when_required")"#,
158self.response_checksum_validation
159 )
160 }
161}
162163impl Error for UnknownResponseChecksumValidationError {}