aws_smithy_runtime_api/client/
ser_de.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Serialization/deserialization for the orchestrator.
7
8use crate::box_error::BoxError;
9use crate::client::interceptors::context::{Error, Input, Output};
10use crate::client::orchestrator::{HttpRequest, HttpResponse, OrchestratorError};
11use crate::impl_shared_conversions;
12use aws_smithy_types::config_bag::{ConfigBag, Storable, StoreReplace};
13use std::fmt;
14use std::sync::Arc;
15
16/// Serialization implementation that converts an [`Input`] into an [`HttpRequest`].
17pub trait SerializeRequest: Send + Sync + fmt::Debug {
18    /// Serializes the input into an HTTP request.
19    ///
20    /// The type of the [`Input`] must be known ahead of time by the request serializer
21    /// implementation, and must be downcasted to get access to the information necessary
22    /// for serialization.
23    ///
24    /// The request serializer is generally added to the [`ConfigBag`] by the operation's
25    /// code generated runtime plugin, which is aware of the correct input/output/error types.
26    fn serialize_input(&self, input: Input, cfg: &mut ConfigBag) -> Result<HttpRequest, BoxError>;
27}
28
29/// A shared request serializer.
30///
31/// This is a simple shared ownership wrapper type for the [`SerializeRequest`] trait.
32#[derive(Clone, Debug)]
33pub struct SharedRequestSerializer(Arc<dyn SerializeRequest>);
34
35impl SharedRequestSerializer {
36    /// Creates a new shared request serializer.
37    pub fn new(serializer: impl SerializeRequest + 'static) -> Self {
38        Self(Arc::new(serializer))
39    }
40}
41
42impl SerializeRequest for SharedRequestSerializer {
43    fn serialize_input(&self, input: Input, cfg: &mut ConfigBag) -> Result<HttpRequest, BoxError> {
44        self.0.serialize_input(input, cfg)
45    }
46}
47
48impl Storable for SharedRequestSerializer {
49    type Storer = StoreReplace<Self>;
50}
51
52impl_shared_conversions!(convert SharedRequestSerializer from SerializeRequest using SharedRequestSerializer::new);
53
54/// Deserialization implementation that converts an [`HttpResponse`] into an [`Output`] or [`Error`].
55pub trait DeserializeResponse: Send + Sync + fmt::Debug {
56    /// For streaming requests, deserializes the response headers.
57    ///
58    /// The orchestrator will call `deserialize_streaming` first, and if it returns `None`,
59    /// then it will continue onto `deserialize_nonstreaming`. This method should only be
60    /// implemented for streaming requests where the streaming response body needs to be a part
61    /// of the deserialized output.
62    fn deserialize_streaming(
63        &self,
64        response: &mut HttpResponse,
65    ) -> Option<Result<Output, OrchestratorError<Error>>> {
66        let _ = response;
67        None
68    }
69
70    /// Deserialize the entire response including its body into an output or error.
71    fn deserialize_nonstreaming(
72        &self,
73        response: &HttpResponse,
74    ) -> Result<Output, OrchestratorError<Error>>;
75}
76
77/// Shared response deserializer.
78///
79/// This is a simple shared ownership wrapper type for the [`DeserializeResponse`] trait.
80#[derive(Debug)]
81pub struct SharedResponseDeserializer(Arc<dyn DeserializeResponse>);
82
83impl SharedResponseDeserializer {
84    /// Creates a new [`SharedResponseDeserializer`].
85    pub fn new(serializer: impl DeserializeResponse + 'static) -> Self {
86        Self(Arc::new(serializer))
87    }
88}
89
90impl DeserializeResponse for SharedResponseDeserializer {
91    fn deserialize_nonstreaming(
92        &self,
93        response: &HttpResponse,
94    ) -> Result<Output, OrchestratorError<Error>> {
95        self.0.deserialize_nonstreaming(response)
96    }
97
98    fn deserialize_streaming(
99        &self,
100        response: &mut HttpResponse,
101    ) -> Option<Result<Output, OrchestratorError<Error>>> {
102        self.0.deserialize_streaming(response)
103    }
104}
105
106impl Storable for SharedResponseDeserializer {
107    type Storer = StoreReplace<Self>;
108}
109
110impl_shared_conversions!(convert SharedResponseDeserializer from DeserializeResponse using SharedResponseDeserializer::new);