aws_smithy_runtime_api/client/interceptors/context/
wrappers.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use super::{Error, Input, InterceptorContext, Output};
7use crate::client::interceptors::context::{Request, Response};
8use crate::client::orchestrator::OrchestratorError;
9use std::fmt::Debug;
10
11macro_rules! impl_from_interceptor_context {
12    (ref $wrapper:ident) => {
13        impl<'a, I, O, E> From<&'a InterceptorContext<I, O, E>> for $wrapper<'a, I, O, E> {
14            fn from(inner: &'a InterceptorContext<I, O, E>) -> Self {
15                Self { inner }
16            }
17        }
18    };
19    (mut $wrapper:ident) => {
20        impl<'a, I, O, E> From<&'a mut InterceptorContext<I, O, E>> for $wrapper<'a, I, O, E> {
21            fn from(inner: &'a mut InterceptorContext<I, O, E>) -> Self {
22                Self { inner }
23            }
24        }
25    };
26}
27
28macro_rules! expect {
29    ($self:ident, $what:ident) => {
30        $self.inner.$what().expect(concat!(
31            "`",
32            stringify!($what),
33            "` wasn't set in the underlying interceptor context. This is a bug."
34        ))
35    };
36}
37
38//
39// BeforeSerializationInterceptorContextRef
40//
41
42/// Interceptor context for the `read_before_execution` and `read_before_serialization` hooks.
43///
44/// Only the input is available at this point in the operation.
45#[derive(Debug)]
46pub struct BeforeSerializationInterceptorContextRef<'a, I = Input, O = Output, E = Error> {
47    inner: &'a InterceptorContext<I, O, E>,
48}
49
50impl_from_interceptor_context!(ref BeforeSerializationInterceptorContextRef);
51
52impl<'a, I, O, E> BeforeSerializationInterceptorContextRef<'a, I, O, E> {
53    /// Returns a reference to the input.
54    pub fn input(&self) -> &I {
55        expect!(self, input)
56    }
57
58    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
59    ///
60    /// There's no good reason to use this unless you're writing tests or you have to
61    /// interact with an API that doesn't support the context wrapper structs.
62    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
63        self.inner
64    }
65}
66
67//
68// BeforeSerializationInterceptorContextMut
69//
70
71/// Interceptor context for the `modify_before_serialization` hook.
72///
73/// Only the input is available at this point in the operation.
74#[derive(Debug)]
75pub struct BeforeSerializationInterceptorContextMut<'a, I = Input, O = Output, E = Error> {
76    inner: &'a mut InterceptorContext<I, O, E>,
77}
78
79impl_from_interceptor_context!(mut BeforeSerializationInterceptorContextMut);
80
81impl<'a, I, O, E> BeforeSerializationInterceptorContextMut<'a, I, O, E> {
82    /// Returns a reference to the input.
83    pub fn input(&self) -> &I {
84        expect!(self, input)
85    }
86
87    /// Returns a mutable reference to the input.
88    pub fn input_mut(&mut self) -> &mut I {
89        expect!(self, input_mut)
90    }
91
92    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
93    ///
94    /// There's no good reason to use this unless you're writing tests or you have to
95    /// interact with an API that doesn't support the context wrapper structs.
96    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
97        self.inner
98    }
99
100    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
101    ///
102    /// There's no good reason to use this unless you're writing tests or you have to
103    /// interact with an API that doesn't support the context wrapper structs.
104    pub fn inner_mut(&mut self) -> &'_ mut InterceptorContext<I, O, E> {
105        self.inner
106    }
107}
108
109//
110// BeforeSerializationInterceptorContextRef
111//
112
113/// Interceptor context for several hooks in between serialization and transmission.
114///
115/// Only the request is available at this point in the operation.
116#[derive(Debug)]
117pub struct BeforeTransmitInterceptorContextRef<'a, I = Input, O = Output, E = Error> {
118    inner: &'a InterceptorContext<I, O, E>,
119}
120
121impl_from_interceptor_context!(ref BeforeTransmitInterceptorContextRef);
122
123impl<'a, I, O, E> BeforeTransmitInterceptorContextRef<'a, I, O, E> {
124    /// Returns a reference to the transmittable request for the operation being invoked.
125    pub fn request(&self) -> &Request {
126        expect!(self, request)
127    }
128
129    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
130    ///
131    /// There's no good reason to use this unless you're writing tests or you have to
132    /// interact with an API that doesn't support the context wrapper structs.
133    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
134        self.inner
135    }
136}
137
138//
139// BeforeSerializationInterceptorContextMut
140//
141
142/// Interceptor context for several hooks in between serialization and transmission.
143///
144/// Only the request is available at this point in the operation.
145#[derive(Debug)]
146pub struct BeforeTransmitInterceptorContextMut<'a, I = Input, O = Output, E = Error> {
147    inner: &'a mut InterceptorContext<I, O, E>,
148}
149
150impl_from_interceptor_context!(mut BeforeTransmitInterceptorContextMut);
151
152impl<'a, I, O, E> BeforeTransmitInterceptorContextMut<'a, I, O, E> {
153    /// Returns a reference to the transmittable request for the operation being invoked.
154    pub fn request(&self) -> &Request {
155        expect!(self, request)
156    }
157
158    /// Returns a mutable reference to the transmittable request for the operation being invoked.
159    pub fn request_mut(&mut self) -> &mut Request {
160        expect!(self, request_mut)
161    }
162
163    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
164    ///
165    /// There's no good reason to use this unless you're writing tests or you have to
166    /// interact with an API that doesn't support the context wrapper structs.
167    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
168        self.inner
169    }
170
171    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
172    ///
173    /// There's no good reason to use this unless you're writing tests or you have to
174    /// interact with an API that doesn't support the context wrapper structs.
175    pub fn inner_mut(&mut self) -> &'_ mut InterceptorContext<I, O, E> {
176        self.inner
177    }
178}
179
180//
181// BeforeDeserializationInterceptorContextRef
182//
183
184/// Interceptor context for hooks before deserializing the response.
185///
186/// Only the response is available at this point in the operation.
187#[derive(Debug)]
188pub struct BeforeDeserializationInterceptorContextRef<'a, I = Input, O = Output, E = Error> {
189    inner: &'a InterceptorContext<I, O, E>,
190}
191
192impl_from_interceptor_context!(ref BeforeDeserializationInterceptorContextRef);
193
194impl<'a, I, O, E> BeforeDeserializationInterceptorContextRef<'a, I, O, E> {
195    /// Returns a reference to the response.
196    pub fn response(&self) -> &Response {
197        expect!(self, response)
198    }
199
200    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
201    ///
202    /// There's no good reason to use this unless you're writing tests or you have to
203    /// interact with an API that doesn't support the context wrapper structs.
204    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
205        self.inner
206    }
207}
208
209//
210// BeforeDeserializationInterceptorContextMut
211//
212
213/// Interceptor context for hooks before deserializing the response.
214///
215/// Only the response is available at this point in the operation.
216pub struct BeforeDeserializationInterceptorContextMut<'a, I = Input, O = Output, E = Error> {
217    inner: &'a mut InterceptorContext<I, O, E>,
218}
219
220impl_from_interceptor_context!(mut BeforeDeserializationInterceptorContextMut);
221
222impl<'a, I, O, E> BeforeDeserializationInterceptorContextMut<'a, I, O, E> {
223    /// Returns a reference to the response.
224    pub fn response(&self) -> &Response {
225        expect!(self, response)
226    }
227
228    /// Returns a mutable reference to the response.
229    pub fn response_mut(&mut self) -> &mut Response {
230        expect!(self, response_mut)
231    }
232
233    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
234    ///
235    /// There's no good reason to use this unless you're writing tests or you have to
236    /// interact with an API that doesn't support the context wrapper structs.
237    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
238        self.inner
239    }
240
241    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
242    ///
243    /// There's no good reason to use this unless you're writing tests or you have to
244    /// interact with an API that doesn't support the context wrapper structs.
245    pub fn inner_mut(&mut self) -> &'_ mut InterceptorContext<I, O, E> {
246        self.inner
247    }
248}
249
250//
251// AfterDeserializationInterceptorContextRef
252//
253
254/// Interceptor context for hooks after deserializing the response.
255///
256/// The response and the deserialized output or error are available at this point in the operation.
257pub struct AfterDeserializationInterceptorContextRef<'a, I = Input, O = Output, E = Error> {
258    inner: &'a InterceptorContext<I, O, E>,
259}
260
261impl_from_interceptor_context!(ref AfterDeserializationInterceptorContextRef);
262
263impl<'a, I, O, E> AfterDeserializationInterceptorContextRef<'a, I, O, E> {
264    /// Returns a reference to the response.
265    pub fn response(&self) -> &Response {
266        expect!(self, response)
267    }
268
269    /// Returns a reference to the deserialized output or error.
270    pub fn output_or_error(&self) -> Result<&O, &OrchestratorError<E>> {
271        expect!(self, output_or_error)
272    }
273
274    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
275    ///
276    /// There's no good reason to use this unless you're writing tests or you have to
277    /// interact with an API that doesn't support the context wrapper structs.
278    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
279        self.inner
280    }
281}
282
283//
284// FinalizerInterceptorContextRef
285//
286
287/// Interceptor context for finalization hooks.
288///
289/// This context is used by the `read_after_attempt` and `read_after_execution` hooks
290/// that are all called upon both success and failure, and may have varying levels
291/// of context available depending on where a failure occurred if the operation failed.
292pub struct FinalizerInterceptorContextRef<'a, I = Input, O = Output, E = Error> {
293    inner: &'a InterceptorContext<I, O, E>,
294}
295
296impl_from_interceptor_context!(ref FinalizerInterceptorContextRef);
297
298impl<'a, I, O, E> FinalizerInterceptorContextRef<'a, I, O, E> {
299    /// Returns the operation input.
300    pub fn input(&self) -> Option<&I> {
301        self.inner.input.as_ref()
302    }
303
304    /// Returns the serialized request.
305    pub fn request(&self) -> Option<&Request> {
306        self.inner.request.as_ref()
307    }
308
309    /// Returns the raw response.
310    pub fn response(&self) -> Option<&Response> {
311        self.inner.response.as_ref()
312    }
313
314    /// Returns the deserialized operation output or error.
315    pub fn output_or_error(&self) -> Option<Result<&O, &OrchestratorError<E>>> {
316        self.inner.output_or_error.as_ref().map(|o| o.as_ref())
317    }
318
319    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
320    ///
321    /// There's no good reason to use this unless you're writing tests or you have to
322    /// interact with an API that doesn't support the context wrapper structs.
323    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
324        self.inner
325    }
326}
327
328//
329// FinalizerInterceptorContextMut
330//
331
332/// Interceptor context for finalization hooks.
333///
334/// This context is used by the `modify_before_attempt_completion` and `modify_before_completion` hooks
335/// that are all called upon both success and failure, and may have varying levels
336/// of context available depending on where a failure occurred if the operation failed.
337pub struct FinalizerInterceptorContextMut<'a, I = Input, O = Output, E = Error> {
338    inner: &'a mut InterceptorContext<I, O, E>,
339}
340
341impl_from_interceptor_context!(mut FinalizerInterceptorContextMut);
342
343impl<'a, I, O, E> FinalizerInterceptorContextMut<'a, I, O, E> {
344    /// Returns the operation input.
345    pub fn input(&self) -> Option<&I> {
346        self.inner.input.as_ref()
347    }
348
349    /// Returns the serialized request.
350    pub fn request(&self) -> Option<&Request> {
351        self.inner.request.as_ref()
352    }
353
354    /// Returns the raw response.
355    pub fn response(&self) -> Option<&Response> {
356        self.inner.response.as_ref()
357    }
358
359    /// Returns the deserialized operation output or error.
360    pub fn output_or_error(&self) -> Option<Result<&O, &OrchestratorError<E>>> {
361        self.inner.output_or_error.as_ref().map(|o| o.as_ref())
362    }
363
364    /// Mutably returns the operation input.
365    pub fn input_mut(&mut self) -> Option<&mut I> {
366        self.inner.input.as_mut()
367    }
368
369    /// Mutably returns the serialized request.
370    pub fn request_mut(&mut self) -> Option<&mut Request> {
371        self.inner.request.as_mut()
372    }
373
374    /// Mutably returns the raw response.
375    pub fn response_mut(&mut self) -> Option<&mut Response> {
376        self.inner.response.as_mut()
377    }
378
379    /// Mutably returns the deserialized operation output or error.
380    pub fn output_or_error_mut(&mut self) -> Option<&mut Result<O, OrchestratorError<E>>> {
381        self.inner.output_or_error.as_mut()
382    }
383
384    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
385    ///
386    /// There's no good reason to use this unless you're writing tests or you have to
387    /// interact with an API that doesn't support the context wrapper structs.
388    pub fn inner(&self) -> &'_ InterceptorContext<I, O, E> {
389        self.inner
390    }
391
392    /// Downgrade this wrapper struct, returning the underlying InterceptorContext.
393    ///
394    /// There's no good reason to use this unless you're writing tests or you have to
395    /// interact with an API that doesn't support the context wrapper structs.
396    pub fn inner_mut(&mut self) -> &'_ mut InterceptorContext<I, O, E> {
397        self.inner
398    }
399}