4
4
use error_stack:: ResultExt ;
5
5
use masking:: { ExposeInterface , PeekInterface , Secret , Strategy } ;
6
6
use quick_xml:: de;
7
+ #[ cfg( all( feature = "logs" , feature = "async_ext" ) ) ]
8
+ use router_env:: logger;
7
9
use serde:: { Deserialize , Serialize } ;
8
10
9
11
use crate :: {
@@ -295,28 +297,34 @@ impl<T> StringExt<T> for String {
295
297
/// Extending functionalities of Wrapper types for idiomatic
296
298
#[ cfg( feature = "async_ext" ) ]
297
299
#[ cfg_attr( feature = "async_ext" , async_trait:: async_trait) ]
298
- pub trait AsyncExt < A , B > {
300
+ pub trait AsyncExt < A > {
299
301
/// Output type of the map function
300
302
type WrappedSelf < T > ;
301
303
302
304
/// Extending map by allowing functions which are async
303
- async fn async_map < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
305
+ async fn async_map < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
304
306
where
305
307
F : FnOnce ( A ) -> Fut + Send ,
306
308
Fut : futures:: Future < Output = B > + Send ;
307
309
308
310
/// Extending the `and_then` by allowing functions which are async
309
- async fn async_and_then < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
311
+ async fn async_and_then < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
310
312
where
311
313
F : FnOnce ( A ) -> Fut + Send ,
312
314
Fut : futures:: Future < Output = Self :: WrappedSelf < B > > + Send ;
315
+
316
+ /// Extending `unwrap_or_else` to allow async fallback
317
+ async fn async_unwrap_or_else < F , Fut > ( self , func : F ) -> A
318
+ where
319
+ F : FnOnce ( ) -> Fut + Send ,
320
+ Fut : futures:: Future < Output = A > + Send ;
313
321
}
314
322
315
323
#[ cfg( feature = "async_ext" ) ]
316
324
#[ cfg_attr( feature = "async_ext" , async_trait:: async_trait) ]
317
- impl < A : Send , B , E : Send > AsyncExt < A , B > for Result < A , E > {
325
+ impl < A : Send , E : Send + std :: fmt :: Debug > AsyncExt < A > for Result < A , E > {
318
326
type WrappedSelf < T > = Result < T , E > ;
319
- async fn async_and_then < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
327
+ async fn async_and_then < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
320
328
where
321
329
F : FnOnce ( A ) -> Fut + Send ,
322
330
Fut : futures:: Future < Output = Self :: WrappedSelf < B > > + Send ,
@@ -327,7 +335,7 @@ impl<A: Send, B, E: Send> AsyncExt<A, B> for Result<A, E> {
327
335
}
328
336
}
329
337
330
- async fn async_map < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
338
+ async fn async_map < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
331
339
where
332
340
F : FnOnce ( A ) -> Fut + Send ,
333
341
Fut : futures:: Future < Output = B > + Send ,
@@ -337,13 +345,28 @@ impl<A: Send, B, E: Send> AsyncExt<A, B> for Result<A, E> {
337
345
Err ( err) => Err ( err) ,
338
346
}
339
347
}
348
+
349
+ async fn async_unwrap_or_else < F , Fut > ( self , func : F ) -> A
350
+ where
351
+ F : FnOnce ( ) -> Fut + Send ,
352
+ Fut : futures:: Future < Output = A > + Send ,
353
+ {
354
+ match self {
355
+ Ok ( a) => a,
356
+ Err ( _err) => {
357
+ #[ cfg( feature = "logs" ) ]
358
+ logger:: error!( "Error: {:?}" , _err) ;
359
+ func ( ) . await
360
+ }
361
+ }
362
+ }
340
363
}
341
364
342
365
#[ cfg( feature = "async_ext" ) ]
343
366
#[ cfg_attr( feature = "async_ext" , async_trait:: async_trait) ]
344
- impl < A : Send , B > AsyncExt < A , B > for Option < A > {
367
+ impl < A : Send > AsyncExt < A > for Option < A > {
345
368
type WrappedSelf < T > = Option < T > ;
346
- async fn async_and_then < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
369
+ async fn async_and_then < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
347
370
where
348
371
F : FnOnce ( A ) -> Fut + Send ,
349
372
Fut : futures:: Future < Output = Self :: WrappedSelf < B > > + Send ,
@@ -354,7 +377,7 @@ impl<A: Send, B> AsyncExt<A, B> for Option<A> {
354
377
}
355
378
}
356
379
357
- async fn async_map < F , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
380
+ async fn async_map < F , B , Fut > ( self , func : F ) -> Self :: WrappedSelf < B >
358
381
where
359
382
F : FnOnce ( A ) -> Fut + Send ,
360
383
Fut : futures:: Future < Output = B > + Send ,
@@ -364,6 +387,17 @@ impl<A: Send, B> AsyncExt<A, B> for Option<A> {
364
387
None => None ,
365
388
}
366
389
}
390
+
391
+ async fn async_unwrap_or_else < F , Fut > ( self , func : F ) -> A
392
+ where
393
+ F : FnOnce ( ) -> Fut + Send ,
394
+ Fut : futures:: Future < Output = A > + Send ,
395
+ {
396
+ match self {
397
+ Some ( a) => a,
398
+ None => func ( ) . await ,
399
+ }
400
+ }
367
401
}
368
402
369
403
/// Extension trait for validating application configuration. This trait provides utilities to
0 commit comments