aws_smithy_runtime_api/http/
extensions.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use crate::http::HttpError;
7
8#[derive(Default, Debug)]
9pub(crate) struct Extensions {
10    extensions_02x: http_02x::Extensions,
11    extensions_1x: http_1x::Extensions,
12}
13
14impl Extensions {
15    pub(crate) fn new() -> Self {
16        Self::default()
17    }
18
19    /// Adds an extension to the request extensions
20    pub(crate) fn insert<T: Send + Sync + Clone + 'static>(&mut self, extension: T) {
21        self.extensions_1x.insert(extension.clone());
22        self.extensions_02x.insert(extension);
23    }
24}
25
26impl From<http_02x::Extensions> for Extensions {
27    fn from(value: http_02x::Extensions) -> Self {
28        Self {
29            extensions_02x: value,
30            extensions_1x: Default::default(),
31        }
32    }
33}
34
35impl From<http_1x::Extensions> for Extensions {
36    fn from(value: http_1x::Extensions) -> Self {
37        Self {
38            extensions_02x: Default::default(),
39            extensions_1x: value,
40        }
41    }
42}
43
44impl TryFrom<Extensions> for http_02x::Extensions {
45    type Error = HttpError;
46
47    fn try_from(value: Extensions) -> Result<Self, Self::Error> {
48        if value.extensions_1x.len() > value.extensions_02x.len() {
49            Err(HttpError::invalid_extensions())
50        } else {
51            Ok(value.extensions_02x)
52        }
53    }
54}
55
56impl TryFrom<Extensions> for http_1x::Extensions {
57    type Error = HttpError;
58
59    fn try_from(value: Extensions) -> Result<Self, Self::Error> {
60        if value.extensions_02x.len() > value.extensions_1x.len() {
61            Err(HttpError::invalid_extensions())
62        } else {
63            Ok(value.extensions_1x)
64        }
65    }
66}