Skip to main content

adventure_rusoto_sns/
lib.rs

1//! A proof-of-concept crate to provides trait implementations of types in [`rusoto_sns`],
2//! to work with [`adventure`].
3//!
4//! Examples
5//! --------
6//!
7//! ```ignore
8//! ```
9use std::ops::Deref;
10use std::pin::Pin;
11use std::rc::Rc;
12use std::sync::Arc;
13use std::time::Duration;
14
15use adventure::{
16    response::Future01Response, BaseRequest, OneshotRequest, PagedRequest, Request,
17    RetriableRequest,
18};
19use rusoto_core::{RusotoError, RusotoFuture};
20use rusoto_sns::*;
21
22pub type RusotoResponse<T, E> = Future01Response<RusotoFuture<T, E>>;
23
24/// A wrapper for various type of requests in [`rusoto_sns`].
25#[derive(Clone, Debug)]
26pub struct AwsSns<T> {
27    inner: T,
28}
29
30pub trait AsSns {
31    type Output: Sns + ?Sized;
32    fn as_sns(&self) -> &Self::Output;
33}
34
35impl AsSns for SnsClient {
36    type Output = SnsClient;
37    fn as_sns(&self) -> &SnsClient {
38        self
39    }
40}
41
42impl<T: ?Sized> AsSns for &T
43where
44    T: Sns,
45{
46    type Output = T;
47    fn as_sns(&self) -> &Self::Output {
48        &**self
49    }
50}
51
52impl<T: ?Sized> AsSns for Box<T>
53where
54    T: Sns,
55{
56    type Output = T;
57    fn as_sns(&self) -> &Self::Output {
58        &**self
59    }
60}
61
62impl<T: ?Sized> AsSns for Arc<T>
63where
64    T: Sns,
65{
66    type Output = T;
67    fn as_sns(&self) -> &Self::Output {
68        &**self
69    }
70}
71
72impl<T: ?Sized> AsSns for Rc<T>
73where
74    T: Sns,
75{
76    type Output = T;
77    fn as_sns(&self) -> &Self::Output {
78        &**self
79    }
80}
81
82impl<P> AsSns for Pin<P>
83where
84    P: Deref,
85    <P as Deref>::Target: Sns,
86{
87    type Output = <P as Deref>::Target;
88    fn as_sns(&self) -> &Self::Output {
89        &**self
90    }
91}
92
93macro_rules! impl_adventure {
94    {
95        wrapper: $wrapper:ident;
96        client: $client:ident;
97        $( impl $name:ident { $($body:tt)* } )*
98    } => {
99        $(
100            impl From<$name> for $wrapper<$name> {
101                fn from(req: $name) -> Self {
102                    $wrapper { inner: req }
103                }
104            }
105
106            impl_adventure!(@ $wrapper, $name, $client; $($body)*);
107        )*
108    };
109
110    (@ $wrapper:ident, $name:ident, $client:ident; type Ok = $ok:ty; type Error = $error:ident; $($rest:tt)*) => {
111        impl BaseRequest for $wrapper<$name> {
112            type Ok = $ok;
113            type Error = RusotoError<$error>;
114        }
115
116        impl_adventure!(@@ $wrapper, $name, $client, $error; $($rest)*);
117    };
118    (@@ $wrapper:ident, $name:ident, $client:ident, $error:ident; ) => { };
119    (@@ $wrapper:ident, $name:ident, $client:ident, $error:ident; retry: $variant:ident; $($rest:tt)*) => {
120        impl RetriableRequest for $wrapper<$name> {
121            fn should_retry(&self, err: &Self::Error, _next_interval: Duration) -> bool {
122                if let RusotoError::HttpDispatch(_) = err {
123                    true
124                } else {
125                    false
126                }
127            }
128        }
129
130        impl_adventure!(@@ $wrapper, $name, $client, $error; $($rest)*);
131    };
132    (@@ $wrapper:ident, $name:ident, $client:ident, $error:ident; send: $method:ident; $($rest:tt)*) => {
133        impl<C> OneshotRequest<C> for $wrapper<$name> where C: AsSns {
134            type Response = RusotoResponse<Self::Ok, $error>;
135
136            fn send_once(self, client: C) -> Self::Response {
137                Future01Response::new(client.as_sns().$method(self.inner))
138            }
139        }
140
141        impl<C> Request<C> for $wrapper<$name> where C: AsSns {
142            type Response = <Self as OneshotRequest<C>>::Response;
143
144            #[inline]
145            fn send(self: Pin<&mut Self>, client: C) -> Self::Response {
146                self.clone().send_once(client)
147            }
148        }
149
150        impl_adventure!(@@ $wrapper, $name, $client, $error; $($rest)*);
151    };
152    (@@ $wrapper:ident, $name:ident, $client:ident, $error:ident; advance: $token:ident; $($rest:tt)*) => {
153        impl PagedRequest for $wrapper<$name> {
154            fn advance(&mut self, response: &Self::Ok) -> bool {
155                if let Some(next) = response.$token.clone() {
156                    self.inner.$token = Some(next);
157                    true
158                } else {
159                    false
160                }
161            }
162        }
163
164        impl_adventure!(@@ $wrapper, $name, $client, $error; $($rest)*);
165    }
166}
167
168impl_adventure! {
169    wrapper: AwsSns;
170    client: Sns;
171
172    impl AddPermissionInput {
173        type Ok = ();
174        type Error = AddPermissionError;
175        retry: Server;
176        send: add_permission;
177    }
178
179    impl CheckIfPhoneNumberIsOptedOutInput {
180        type Ok = CheckIfPhoneNumberIsOptedOutResponse;
181        type Error = CheckIfPhoneNumberIsOptedOutError;
182        retry: Server;
183        send: check_if_phone_number_is_opted_out;
184    }
185
186    impl ConfirmSubscriptionInput {
187        type Ok = ConfirmSubscriptionResponse;
188        type Error = ConfirmSubscriptionError;
189        retry: Server;
190        send: confirm_subscription;
191    }
192
193    impl CreatePlatformApplicationInput {
194        type Ok = CreatePlatformApplicationResponse;
195        type Error = CreatePlatformApplicationError;
196        retry: Server;
197        send: create_platform_application;
198    }
199
200    impl CreatePlatformEndpointInput {
201        type Ok = CreateEndpointResponse;
202        type Error = CreatePlatformEndpointError;
203        retry: Server;
204        send: create_platform_endpoint;
205    }
206
207    impl CreateTopicInput {
208        type Ok = CreateTopicResponse;
209        type Error = CreateTopicError;
210        retry: Server;
211        send: create_topic;
212    }
213
214    impl DeleteEndpointInput {
215        type Ok = ();
216        type Error = DeleteEndpointError;
217        retry: Server;
218        send: delete_endpoint;
219    }
220
221    impl DeletePlatformApplicationInput {
222        type Ok = ();
223        type Error = DeletePlatformApplicationError;
224        retry: Server;
225        send: delete_platform_application;
226    }
227
228    impl DeleteTopicInput {
229        type Ok = ();
230        type Error = DeleteTopicError;
231        retry: Server;
232        send: delete_topic;
233    }
234
235    impl GetEndpointAttributesInput {
236        type Ok = GetEndpointAttributesResponse;
237        type Error = GetEndpointAttributesError;
238        retry: Server;
239        send: get_endpoint_attributes;
240    }
241
242    impl GetPlatformApplicationAttributesInput {
243        type Ok = GetPlatformApplicationAttributesResponse;
244        type Error = GetPlatformApplicationAttributesError;
245        retry: Server;
246        send: get_platform_application_attributes;
247    }
248
249    impl GetSMSAttributesInput {
250        type Ok = GetSMSAttributesResponse;
251        type Error = GetSMSAttributesError;
252        retry: Server;
253        send: get_sms_attributes;
254    }
255
256    impl GetSubscriptionAttributesInput {
257        type Ok = GetSubscriptionAttributesResponse;
258        type Error = GetSubscriptionAttributesError;
259        retry: Server;
260        send: get_subscription_attributes;
261    }
262
263    impl GetTopicAttributesInput {
264        type Ok = GetTopicAttributesResponse;
265        type Error = GetTopicAttributesError;
266        retry: Server;
267        send: get_topic_attributes;
268    }
269
270    impl ListEndpointsByPlatformApplicationInput {
271        type Ok = ListEndpointsByPlatformApplicationResponse;
272        type Error = ListEndpointsByPlatformApplicationError;
273        retry: Server;
274        send: list_endpoints_by_platform_application;
275        advance: next_token;
276    }
277
278    impl ListPhoneNumbersOptedOutInput {
279        type Ok = ListPhoneNumbersOptedOutResponse;
280        type Error = ListPhoneNumbersOptedOutError;
281        retry: Server;
282        send: list_phone_numbers_opted_out;
283        advance: next_token;
284    }
285
286    impl ListPlatformApplicationsInput {
287        type Ok = ListPlatformApplicationsResponse;
288        type Error = ListPlatformApplicationsError;
289        retry: Server;
290        send: list_platform_applications;
291        advance: next_token;
292    }
293
294    impl ListSubscriptionsInput {
295        type Ok = ListSubscriptionsResponse;
296        type Error = ListSubscriptionsError;
297        retry: Server;
298        send: list_subscriptions;
299        advance: next_token;
300    }
301
302    impl ListSubscriptionsByTopicInput {
303        type Ok = ListSubscriptionsByTopicResponse;
304        type Error = ListSubscriptionsByTopicError;
305        retry: Server;
306        send: list_subscriptions_by_topic;
307        advance: next_token;
308    }
309
310    impl ListTagsForResourceRequest {
311        type Ok = ListTagsForResourceResponse;
312        type Error = ListTagsForResourceError;
313        retry: Server;
314        send: list_tags_for_resource;
315    }
316
317    impl ListTopicsInput {
318        type Ok = ListTopicsResponse;
319        type Error = ListTopicsError;
320        retry: Server;
321        send: list_topics;
322        advance: next_token;
323    }
324
325    impl OptInPhoneNumberInput {
326        type Ok = OptInPhoneNumberResponse;
327        type Error = OptInPhoneNumberError;
328        retry: Server;
329        send: opt_in_phone_number;
330    }
331
332    impl PublishInput {
333        type Ok = PublishResponse;
334        type Error = PublishError;
335        retry: Server;
336        send: publish;
337    }
338
339    impl RemovePermissionInput {
340        type Ok = ();
341        type Error = RemovePermissionError;
342        retry: Server;
343        send: remove_permission;
344    }
345
346    impl SetEndpointAttributesInput {
347        type Ok = ();
348        type Error = SetEndpointAttributesError;
349        retry: Server;
350        send: set_endpoint_attributes;
351    }
352
353    impl SetPlatformApplicationAttributesInput {
354        type Ok = ();
355        type Error = SetPlatformApplicationAttributesError;
356        retry: Server;
357        send: set_platform_application_attributes;
358    }
359
360    impl SetSMSAttributesInput {
361        type Ok = SetSMSAttributesResponse;
362        type Error = SetSMSAttributesError;
363        retry: Server;
364        send: set_sms_attributes;
365    }
366
367    impl SetSubscriptionAttributesInput {
368        type Ok = ();
369        type Error = SetSubscriptionAttributesError;
370        retry: Server;
371        send: set_subscription_attributes;
372    }
373
374    impl SetTopicAttributesInput {
375        type Ok = ();
376        type Error = SetTopicAttributesError;
377        retry: Server;
378        send: set_topic_attributes;
379    }
380
381    impl SubscribeInput {
382        type Ok = SubscribeResponse;
383        type Error = SubscribeError;
384        retry: Server;
385        send: subscribe;
386    }
387
388    impl TagResourceRequest {
389        type Ok = TagResourceResponse;
390        type Error = TagResourceError;
391        retry: Server;
392        send: tag_resource;
393    }
394
395    impl UnsubscribeInput {
396        type Ok = ();
397        type Error = UnsubscribeError;
398        retry: Server;
399        send: unsubscribe;
400    }
401
402    impl UntagResourceRequest {
403        type Ok = UntagResourceResponse;
404        type Error = UntagResourceError;
405        retry: Server;
406        send: untag_resource;
407    }
408
409}