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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use super::{Error, Input, InterceptorContext, Output};
use crate::client::interceptors::context::{Request, Response};
use crate::client::orchestrator::OrchestratorError;
use std::fmt::Debug;

macro_rules! output {
    (&Option<Result<$o_ty:ty, $e_ty:ty>>) => {
        Option<Result<&$o_ty, &$e_ty>>
    };
    (&Option<$ty:ty>) => {
        Option<&$ty>
    };
    (&mut Option<$ty:ty>) => {
        Option<&mut $ty>
    };
    (&Result<$o_ty:ty, $e_ty:ty>) => {
        Result<&$o_ty, &$e_ty>
    };
    (&$($tt:tt)+) => {
        &$($tt)+
    };
    (&mut $($tt:tt)+) => {
        &mut $($tt)+
    };
}

macro_rules! declare_method {
    (&mut $name:ident, $inner_name:ident, $doc:literal, Option<$ty:ty>) => {
        #[doc=$doc]
        pub fn $name(&mut self) -> Option<&mut $ty> {
            self.inner.$inner_name.as_ref()
        }
    };
    (&$name:ident, $inner_name:ident, $doc:literal, Option<$ty:ty>) => {
        #[doc=$doc]
        pub fn $name(&self) -> Option<$ty> {
            self.inner.$inner_name.as_mut()
        }
    };
    (&mut $name:ident, $doc:literal, $($tt:tt)+) => {
        #[doc=$doc]
        pub fn $name(&mut self) -> output!(&mut $($tt)+) {
            self.inner.$name()
        }
    };
    (&$name:ident, $doc:literal, $($tt:tt)+) => {
        #[doc=$doc]
        pub fn $name(&self) -> output!(&$($tt)+) {
            self.inner.$name()
        }
    };
}

macro_rules! declare_known_method {
    (output_or_error: &mut $($tt:tt)+) => {
        declare_method!(&mut output_or_error_mut, "Returns a mutable reference to the deserialized output or error.", $($tt)+);
    };
    (output_or_error: &$($tt:tt)+) => {
        declare_method!(&output_or_error, "Returns a reference to the deserialized output or error.", $($tt)+);
    };
    (input: &mut $($tt:tt)+) => {
        declare_method!(&mut input_mut, "Returns a mutable reference to the input.", $($tt)+);
    };
    (input: &$($tt:tt)+) => {
        declare_method!(&input, "Returns a reference to the input.", $($tt)+);
    };
    (request: &mut $($tt:tt)+) => {
        declare_method!(&mut request_mut, "Returns a mutable reference to the transmittable request for the operation being invoked.", $($tt)+);
    };
    (request: &$($tt:tt)+) => {
        declare_method!(&request, "Returns a reference to the transmittable request for the operation being invoked.", $($tt)+);
    };
    (response: &mut $($tt:tt)+) => {
        declare_method!(&mut response_mut, "Returns a mutable reference to the response.", $($tt)+);
    };
    (response: &$($tt:tt)+) => {
        declare_method!(&response, "Returns a reference to the response.", $($tt)+);
    };
}

macro_rules! declare_wrapper {
    (($ref_struct_name:ident $mut_struct_name:ident)$($tt:tt)+) => {
        pub struct $ref_struct_name<'a, I = Input, O = Output, E = Error>
        where E: Debug {
            inner: &'a InterceptorContext<I, O, E>,
        }

        impl<'a, I, O, E: Debug> From<&'a InterceptorContext<I, O, E>> for $ref_struct_name<'a, I, O, E>
        {
            fn from(inner: &'a InterceptorContext<I, O, E>) -> Self {
                Self { inner }
            }
        }

        impl<'a, I, O, E: Debug> $ref_struct_name<'a, I, O, E> {
            declare_ref_wrapper_methods!($($tt)+);
        }

        pub struct $mut_struct_name<'a, I = Input, O = Output, E = Error>
        where E: Debug {
            inner: &'a mut InterceptorContext<I, O, E>,
        }

        impl<'a, I, O, E: Debug> From<&'a mut InterceptorContext<I, O, E>> for $mut_struct_name<'a, I, O, E>
        {
            fn from(inner: &'a mut InterceptorContext<I, O, E>) -> Self {
                Self { inner }
            }
        }

        impl<'a, I, O, E: Debug> $mut_struct_name<'a, I, O, E> {
            declare_ref_wrapper_methods!($($tt)+);
            declare_mut_wrapper_methods!($($tt)+);
        }
    };
}

macro_rules! declare_ref_wrapper_methods {
    (($field:ident: $($head:tt)+)$($tail:tt)+) => {
        declare_known_method!($field: &$($head)+);
        declare_ref_wrapper_methods!($($tail)+);
    };
    (($field:ident: $($tt:tt)+)) => {
        declare_known_method!($field: &$($tt)+);
    };
}

macro_rules! declare_mut_wrapper_methods {
    (($field:ident: $($head:tt)+)$($tail:tt)+) => {
        declare_known_method!($field: &mut $($head)+);
        declare_mut_wrapper_methods!($($tail)+);
    };
    (($field:ident: $($tt:tt)+)) => {
        declare_known_method!($field: &mut $($tt)+);
    };
}

declare_wrapper!(
    (BeforeSerializationInterceptorContextRef BeforeSerializationInterceptorContextMut)
    (input: I)
);

declare_wrapper!(
    (BeforeTransmitInterceptorContextRef BeforeTransmitInterceptorContextMut)
    (input: I)
    (request: Request)
);

declare_wrapper!(
    (BeforeDeserializationInterceptorContextRef BeforeDeserializationInterceptorContextMut)
    (input: I)
    (request: Request)
    (response: Response)
);

declare_wrapper!(
    (AfterDeserializationInterceptorContextRef AfterDeserializationInterceptorContextMut)
    (input: I)
    (request: Request)
    (response: Response)
    (output_or_error: Result<O, OrchestratorError<E>>)
);

// Why are all the rest of these defined with a macro but these last two aren't? I simply ran out of
// time. Consider updating the macros to support these last two if you're looking for a challenge.
// - Zelda

pub struct FinalizerInterceptorContextRef<'a, I = Input, O = Output, E = Error>
where
    E: Debug,
{
    inner: &'a InterceptorContext<I, O, E>,
}

impl<'a, I, O, E: Debug> From<&'a InterceptorContext<I, O, E>>
    for FinalizerInterceptorContextRef<'a, I, O, E>
{
    fn from(inner: &'a InterceptorContext<I, O, E>) -> Self {
        Self { inner }
    }
}

impl<'a, I, O, E: Debug> FinalizerInterceptorContextRef<'a, I, O, E> {
    pub fn input(&self) -> Option<&I> {
        self.inner.input.as_ref()
    }

    pub fn request(&self) -> Option<&Request> {
        self.inner.request.as_ref()
    }

    pub fn response(&self) -> Option<&Response> {
        self.inner.response.as_ref()
    }

    pub fn output_or_error(&self) -> Option<Result<&O, &OrchestratorError<E>>> {
        self.inner.output_or_error.as_ref().map(|o| o.as_ref())
    }
}

pub struct FinalizerInterceptorContextMut<'a, I = Input, O = Output, E = Error>
where
    E: Debug,
{
    inner: &'a mut InterceptorContext<I, O, E>,
}

impl<'a, I, O, E: Debug> From<&'a mut InterceptorContext<I, O, E>>
    for FinalizerInterceptorContextMut<'a, I, O, E>
{
    fn from(inner: &'a mut InterceptorContext<I, O, E>) -> Self {
        Self { inner }
    }
}

impl<'a, I, O, E: Debug> FinalizerInterceptorContextMut<'a, I, O, E> {
    pub fn input(&self) -> Option<&I> {
        self.inner.input.as_ref()
    }

    pub fn request(&self) -> Option<&Request> {
        self.inner.request.as_ref()
    }

    pub fn response(&self) -> Option<&Response> {
        self.inner.response.as_ref()
    }

    pub fn output_or_error(&self) -> Option<Result<&O, &OrchestratorError<E>>> {
        self.inner.output_or_error.as_ref().map(|o| o.as_ref())
    }

    pub fn input_mut(&mut self) -> Option<&mut I> {
        self.inner.input.as_mut()
    }

    pub fn request_mut(&mut self) -> Option<&mut Request> {
        self.inner.request.as_mut()
    }

    pub fn response_mut(&mut self) -> Option<&mut Response> {
        self.inner.response.as_mut()
    }

    pub fn output_or_error_mut(&mut self) -> Option<&mut Result<O, OrchestratorError<E>>> {
        self.inner.output_or_error.as_mut()
    }
}