aws_smithy_runtime_api/http/
extensions.rs

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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use crate::http::HttpError;
use http as http0;

#[derive(Default, Debug)]
pub(crate) struct Extensions {
    extensions_02x: http0::Extensions,
    extensions_1x: http1::Extensions,
}

impl Extensions {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    /// Adds an extension to the request extensions
    pub(crate) fn insert<T: Send + Sync + Clone + 'static>(&mut self, extension: T) {
        self.extensions_1x.insert(extension.clone());
        self.extensions_02x.insert(extension);
    }
}

impl From<http0::Extensions> for Extensions {
    fn from(value: http0::Extensions) -> Self {
        Self {
            extensions_02x: value,
            extensions_1x: Default::default(),
        }
    }
}

impl From<http1::Extensions> for Extensions {
    fn from(value: http1::Extensions) -> Self {
        Self {
            extensions_02x: Default::default(),
            extensions_1x: value,
        }
    }
}

impl TryFrom<Extensions> for http0::Extensions {
    type Error = HttpError;

    fn try_from(value: Extensions) -> Result<Self, Self::Error> {
        if value.extensions_1x.len() > value.extensions_02x.len() {
            Err(HttpError::invalid_extensions())
        } else {
            Ok(value.extensions_02x)
        }
    }
}

impl TryFrom<Extensions> for http1::Extensions {
    type Error = HttpError;

    fn try_from(value: Extensions) -> Result<Self, Self::Error> {
        if value.extensions_02x.len() > value.extensions_1x.len() {
            Err(HttpError::invalid_extensions())
        } else {
            Ok(value.extensions_1x)
        }
    }
}