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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//un comment below lines to debug macros

/*#![feature(trace_macros)]
trace_macros!(true);*/
//! Crate `function-compose` provides utilities for composing functions and way to inject arguments to functions
//! The composeable functions should return rust Result type with FnError as Err type
//!
//!
//! ### Usage
//! ```rust
//! use function_compose::composeable;
//! #[composeable()]
//! pub fn add_10(a: i32) -> Result<i32, FnError> {
//!     Ok(a + 10)
//! }
//! 
//! ```
//! 
//! ##### The async function should return BoxFuture and the error type should be FnError.
//! 
//! ```rust
//! use function_compose::composeable;
//! use futures::{future::BoxFuture, FutureExt};
//! #[composeable()]
//! pub fn add_async(a: i32, b: i32) -> BoxFuture<'static, Result<i32, FnError>> {
//!     async move {
//!         let r = a + b;
//!         Ok(r)
//!     }.boxed()
//! }
//! ```
//! 
//! ##### Composing async and sync functions usage
//!
//!```ignore
//! use function_compose::compose;
//! use fn_macros::composeable;
//! use futures::{future::BoxFuture, FutureExt};
//! #[composeable()]
//! pub fn add_async(a: i32, b: i32) -> BoxFuture<'static, Result<i32, FnError>> {
//!     async move {
//!         let r = a + b;
//!         Ok(r)
//!     }.boxed()
//! }
//! #[composeable()]
//! pub fn add_10(a: i32) -> Result<i32, FnError> {
//!     Ok(a + 10)
//! }
//! async fn test(){
//!    let result = compose!(add_async.provide(10) -> add_100 -> with_args(10)).await;
//!    assert_eq!(210, result.unwrap());
//! }
//! ```
//! ##### Function argument injection usage
//!```rust
//! use function_compose::composeable;
//! use futures::{future::BoxFuture, FutureExt};
//! #[composeable()]
//! pub fn add_3_arg_async(a: i32,b: i32, c:i32) -> BoxFuture<'static, Result<i32, FnError>>{
//!     async move{
//!         let  r =   a + b + c;
//!         Ok(r)
//!     }.boxed()
//! }
//! ```
//! ##### Example of multiple injection to async function
//!
//!```ignore
//! use crate::compose;
//! let result = compose!(add_3_arg_async.provide(100).provide(200) -> add_10 -> with_args(10)).await;
//! assert_eq!(220, result.unwrap());
//!```


use std::{error::Error, fmt::Display, ops::Deref};
use std::env::Args;


use futures::{future::BoxFuture, FutureExt};
//use paste::paste;


//pub type FnError = Box<dyn Error>;

#[derive(Debug)]
pub struct FnError{
    pub underlying_error: Option<Box<dyn Error + Send>>,
    pub error_code:Option<String>,
    pub description: Option<String>
}

pub use function_compose_proc_macros::*;
pub use paste::*;
pub use concat_idents::concat_idents;

macro_rules! composer_generator {
    ($arg1:ident, $return_type1:ident, $return_type2:ident) => {
        paste!{
            #[doc = concat!("Then implementation for composing sync function (BoxedFn1) with another sync function(BoxedFn1) ")]
            impl<'a, $arg1: 'a + Send, $return_type1: 'a + Send, $return_type2: 'a>
                Then<'a, $arg1, $return_type1, $return_type2, BoxedFn1<'a, $return_type1, $return_type2>, BoxedFn1<'a, $arg1, $return_type2>> for BoxedFn1<'a, $arg1, $return_type1>{

                fn then(self, f: BoxedFn1<'a, $return_type1, $return_type2>) -> BoxedFn1<'a, $arg1, $return_type2> {
                    let r1 = move |x: $arg1| {
                        let b = self(x)?;
                        let r = f(b)?;
                        Ok(r)
                    };
                    Box::new(r1)
                }
            }

            #[doc = concat!("Then implementation for composing sync function(BoxedFn1) with another async function(BoxedAsyncFn1) ")]
            impl<'a, $arg1: 'a + Send, $return_type1: 'a + Send, $return_type2: 'a>
                Then<'a, $arg1, $return_type1, $return_type2, BoxedAsyncFn1<'a, $return_type1, $return_type2>, BoxedAsyncFn1<'a, $arg1, $return_type2>> for BoxedFn1<'a, $arg1, $return_type1>{

                fn then(self, f: BoxedAsyncFn1<'a, $return_type1, $return_type2>) -> BoxedAsyncFn1<'a, $arg1, $return_type2> {
                    let r1 =  |x: $arg1| {
                        async move{
                            let b = self(x)?;
                            f(b).await
                        }.boxed()
                    };
                    Box::new(r1)
                }
            }

            #[doc = concat!("Then implementation for composing async function(BoxedAsyncFn1) with another sync function(BoxedFn1) ")]

            impl<'a, $arg1: 'a + Send, $return_type1: 'a + Send, $return_type2: 'a>
                Then<'a, $arg1, $return_type1, $return_type2, BoxedFn1<'a, $return_type1, $return_type2>, BoxedAsyncFn1<'a, $arg1, $return_type2>> for BoxedAsyncFn1<'a, $arg1, $return_type1>{

                fn then(self, f: BoxedFn1<'a, $return_type1, $return_type2>) -> BoxedAsyncFn1<'a, $arg1, $return_type2> {
                    let r1 = |a: $arg1| {
                        async move {
                            let gResult = self(a).await?;
                            f(gResult)
                        }.boxed()
                    };
                    let r: BoxedAsyncFn1<'a,$arg1, $return_type2> = Box::new(r1);
                    r
                }
            }


            #[doc = concat!("Then implementation for composing async function(BoxedAsyncFn1) with another async function(BoxedAsyncFn1) ")]
            impl<'a, $arg1: 'a + Send, $return_type1: 'a + Send, $return_type2: 'a>
                Then<'a, $arg1, $return_type1, $return_type2, BoxedAsyncFn1<'a, $return_type1, $return_type2>, BoxedAsyncFn1<'a, $arg1, $return_type2>> for BoxedAsyncFn1<'a, $arg1, $return_type1>{

                fn then(self, f: BoxedAsyncFn1<'a, $return_type1, $return_type2>) -> BoxedAsyncFn1<'a, $arg1, $return_type2> {
                    let r1 = |a: $arg1| {
                        async move {
                            let gResult = self(a).await?;
                            f(gResult).await
                        }.boxed()
                    };
                    let r: BoxedAsyncFn1<'a,$arg1, $return_type2> = Box::new(r1);
                    r
                }
            }
        }
    }
}
macro_rules! impl_injector {
    ([$($args:ident),*], $provided:ident, $return_type:ident,  $arg_size:literal, $return_fn_arg_size:literal) => {

        paste!  {
            #[doc = concat!("dependency injection function provide_f", stringify!($arg_size), " for injecting the last argument of a given sync function")]
            pub fn [<provider_f $arg_size>]<'a, $($args),*, $provided, $return_type>(fn1: [<BoxedFn $arg_size>]<'a, $($args),*, $provided, $return_type>,provided_data: $provided,) -> [<BoxedFn $return_fn_arg_size>]<'a, $($args),* , $return_type> where $( $args: 'a ),*, $provided: Send + Sync + 'a, $return_type: 'a{
                    Box::new(move |$( [<$args:lower>]:$args ),*| fn1($( [<$args:lower>]),*,  provided_data))
            }

            #[doc = concat!("dependency injection function provider_async_f", stringify!($arg_size), " for injecting the last argument of a given async function")]
            pub fn [<provider_async_f $arg_size>]<'a, $($args),*, $provided, $return_type>(fn1: [<BoxedAsyncFn $arg_size>]<'a, $($args),*, $provided, $return_type>,provided_data: $provided,) -> [<BoxedAsyncFn $return_fn_arg_size>]<'a, $($args),* , $return_type> where $( $args: 'a ),*, $provided: Send + Sync + 'a, $return_type: 'a{
                    Box::new(move |$( [<$args:lower>]:$args ),*| fn1($( [<$args:lower>]),*,  provided_data))
            }

        }
        paste!{

            #[doc = concat!("Injector implementation for a given sync function that accepts " , stringify!($return_fn_arg_size+1), " arguments and returns a function with ", stringify!($return_fn_arg_size), " arguments")]
            impl<'a, $($args),*, $provided, $return_type> Injector<$provided, [<BoxedFn $return_fn_arg_size>]<'a, $($args),*, $return_type>> for [<BoxedFn $arg_size>] <'a, $($args),*, $provided, $return_type>
            where $( $args: 'a ),*, $provided: Send + Sync +'a, $return_type: 'a
            {
                fn provide(self, a: $provided) -> [<BoxedFn $return_fn_arg_size>]<'a, $($args),*, $return_type> {
                    let r = [<provider_f $arg_size>](self, a);
                    r
                }
            }

            #[doc = concat!("Injector implementation for a given async function that accepts " , stringify!($return_fn_arg_size+1), " arguments  and returns a function with ", stringify!($return_fn_arg_size), " arguments")]
            impl<'a, $($args),*, $provided, $return_type> Injector<$provided, [<BoxedAsyncFn $return_fn_arg_size>]<'a, $($args),*, $return_type>> for [<BoxedAsyncFn $arg_size>] <'a, $($args),*, $provided, $return_type>
            where $( $args: 'a ),*, $provided: Send + Sync +'a, $return_type: 'a
            {
                fn provide(self, a: $provided) -> [<BoxedAsyncFn $return_fn_arg_size>]<'a, $($args),*, $return_type> {
                    let r = [<provider_async_f $arg_size>](self, a);
                    r
                }
            }
        }
    };
}

macro_rules! generate_boxed_fn {
    ( [$($args:ident),*], $return_type:ident, $arg_size:expr ) => {

            //let x = count!($($args),*);
            crate::concat_idents!(boxed_fn_name = BoxedFn,$arg_size  {
                #[doc = concat!("Type alias  BoxedFn", stringify!($arg_size), "  for Boxed FnOnce sync function with ", stringify!($arg_size), " arguments")]
                pub type boxed_fn_name<'a, $($args),*, $return_type,> = Box<dyn FnOnce($($args),*) -> Result<$return_type, FnError> + Send + Sync + 'a>;
            });

            crate::concat_idents!(boxed_fn_name = BoxedAsyncFn,$arg_size  {
                #[doc = concat!("Type alias  BoxedAsyncFn", stringify!($arg_size), "  for Boxed FnOnce async function" , stringify!($arg_size), " arguments")]
                    pub type boxed_fn_name<'a, $($args),*, $return_type,> = Box<dyn FnOnce($($args),*) -> BoxFuture<'a, Result<$return_type, FnError>> + Send + Sync + 'a>;
                });

            paste!{
                #[doc = concat!("Function to box FnOnce sync function with ", stringify!($arg_size), " aguments and coerce it to BoxedFn",stringify!($arg_size))]
                pub fn [<lift_sync_fn $arg_size>]<'a, $($args),*, $return_type, F: FnOnce($($args),*) -> Result<$return_type, FnError> + Send + Sync + 'a>(f: F,) -> [<BoxedFn $arg_size>]<'a, $($args),*, $return_type> {
                    Box::new(f)
                }

                #[doc = concat!("Function to box  FnOnce sync function with ", stringify!($arg_size), " aguments and coerce it to BoxedAsyncFn",stringify!($arg_size))]
                pub fn [<lift_async_fn $arg_size>]<'a, $($args),*, $return_type, F: FnOnce($($args),*) -> BoxFuture<'a,Result<$return_type, FnError>> + Send + Sync + 'a>(f: F,) -> [<BoxedAsyncFn $arg_size>]<'a, $($args),*, $return_type> {
                    Box::new(f)
                }
            }
    }
}

generate_boxed_fn! {[T1], T2, 1}

generate_boxed_fn!([T1, T2], T3, 2);
impl_injector! {[T1],T2, T3, 2, 1}

generate_boxed_fn!([T1, T2, T3], T4, 3);
impl_injector!([T1, T2], T3, T4, 3, 2);

generate_boxed_fn!([T1, T2, T3, T4], T5, 4);
impl_injector!([T1, T2, T3], T4, T5, 4, 3);

generate_boxed_fn!([T1, T2, T3, T4, T5], T6, 5);
impl_injector!([T1, T2, T3, T4], T5, T6, 5, 4);

generate_boxed_fn!([T1, T2, T3, T4, T5, T6], T7, 6);
impl_injector!([T1, T2, T3, T4, T5], T6, T7, 6, 5);

generate_boxed_fn!([T1, T2, T3, T4, T5, T6, T7], T8, 7);
impl_injector!([T1, T2, T3, T4, T5, T6], T7, T8, 7, 6);

generate_boxed_fn!([T1, T2, T3, T4, T5, T6, T7, T8], T9, 8);
impl_injector!([T1, T2, T3, T4, T5, T6, T7], T8, T9, 8, 7);

//Generates a function composition for BoxedFn1 as below. The below is example of composing sync with sync function.
//Similar code is generated for composing sync with async function, async with sync function and async with async function.
// impl<'a, T1: 'a + Send, T2: 'a + Send, T3: 'a>
// Then<'a, T1, T2, T3, BoxedFn1<'a, T2, T3>, BoxedFn1<'a, T1, T3>> for BoxedFn1<'a, T1, T2> {
//     fn then(self, f: BoxedFn1<'a, T2, T3>) -> BoxedFn1<'a, T1, T3> {
//         let r1 = move |x: T1| {
//             let b = self(x)?;
//             let r = f(b)?;
//             Ok(r)
//         };
//         Box::new(r1)
//     }
// }
composer_generator!(T1, T2, T3);

#[derive(Debug)]
pub enum ErrorType {
    //User Id
    UserNotFound(String),
    AuthError(String),
    // Role ID
    RoleNotFound(String),
    Unknown(String),
    DBInitError,
    DBError(String),
    InvalidInput(String),
    EmailAlreadyTaken(String),
}




pub trait Injector<I, O> {
    fn provide(self, a: I) -> O;
}

///trait Then allows you to compose functions.
/// Type param A represents the function arg of Self
///
/// Type param B represents the return type of Self
///
///Type B also acts as the input arg of function f
///
/// Type C represents the return type of  function f

pub trait Then<'a, A, B, C, F, R> {

    /// Compose self with function f
    ///
    /// self:function(A)->B
    ///
    /// f:function(B)->C
    ///
    /// returns function(A)->C
    fn then(self, f: F) -> R;
}

#[macro_use]
pub mod macros {
    #[macro_export]
    macro_rules! c1 {
        ($fLeft:ident, $current_f:ident, true, true) => {
            $fLeft.then_async(current_f)
        };

        ($fLeft:ident, $current_f:ident, true, false) => {
            $fLeft.then_sync(current_f)
        };

        ($fLeft:ident, $current_f:ident, false, false) => {
            $fLeft.then_sync(current_f)
        };

        ($fLeft:ident, $current_f:ident, false, true) => {
            $fLeft.then_async(current_f)
        };
    }

    #[macro_export]
    macro_rules! compose {
        ($fnLeft:ident,$isLeftFnAsync:ident,-> with_args($args:expr) $($others:tt)*) => {
            {
            let r = $fnLeft($args);
            r
            }
        };

        ($fnLeft:ident,$isLeftFnAsync:ident,.provide($p1:expr) $($others:tt)*) => {
            {
                use crate::Injector;
                let p = $fnLeft.provide($p1);
                let p1 = compose!(p,$isLeftFnAsync,$($others)*);
                p1
            }
        };

        ($fLeft:ident,$isLeftFnAsync:ident,$fRight:ident, $isRightAsync:ident,  .provide($p:expr) $($others:tt)*) =>{
            {
                let fRight = $fRight.provide($p);
                let f3 = compose!($fLeft,$isLeftFnAsync,fRight,$isRightAsync,$($others)*);
                f3
            }
        };

        ($fLeft:ident,$isLeftFnAsync:ident,$fRight:ident, $isRightAsync:ident,  ->  $($others:tt)*) =>{
            {
                let fLeft = $fLeft.then($fRight);
                let isLeftFnAsync = $isRightAsync || $isLeftFnAsync;
                let f3 = compose!(fLeft,isLeftFnAsync, -> $($others)*);
                f3
            }
        };

        ($fLeft:ident,$isLeftFnAsync:ident,-> $fn:ident.provide($p:expr) $($others:tt)*) =>{
            {
                let f4;
                
                crate::concat_idents!(lifted_fn_name = fn_composer__lifted_fn,_, $fn {
                    paste!{
                    let is_retryable = [< fn_composer__is_retryable_ $fn >]();
                    let current_f = if !is_retryable{
                        [<fn_composer__lifted_fn_ $fn>]($fn)
                    }else {
                        [<fn_composer__lifted_fn_ $fn>]([< fn_composer__ retry_ $fn>])
                    };
                    }
                    crate::concat_idents!(asynCheckFn = fn_composer__is_async_, $fn {
                        let isRightAsync = asynCheckFn();
                        let fRight = current_f.provide($p);
                        let f3 = compose!($fLeft,$isLeftFnAsync,fRight,isRightAsync,$($others)*);
                        f4 = f3;
                    });
                });
                f4
            }
        };

        ($fLeft:ident,$isLeftFnAsync:ident,-> $fn:ident.provide($p:expr) $($others:tt)*) =>{
            {
                let f4;
                crate::concat_idents!(lifted_fn_name = fn_composer__lifted_fn,_, $fn {
                    let is_retryable = [<fn_composer__is_retryable $fn>]();
                    let current_f = if !is_retryable{
                        [<lifted_fn_name $fn>]($fn)
                    }else {
                        [<lifted_fn_name $fn>]([<fn_composer__ retry_ $fn>])
                    };
                    crate::concat_idents!(asynCheckFn = fn_composer__is_async_, $fn {
                        let isRightAsync = asynCheckFn();
                        let fRight = current_f.provide($p);
                        let f3 = compose!($fLeft,$isLeftFnAsync,fRight,isRightAsync,$($others)*);
                        f4 = f3;
                    });
                });
                f4
            }
        };


        ($fLeft:ident,$isLeftFnAsync:ident,-> $fn:ident $($others:tt)*) =>{
            {
                let f4;
                paste!{

                    let asynCheckFn = [<fn_composer__is_async_ $fn>];
                    let currentAsync = asynCheckFn();
                    let _isResultAsync = currentAsync || $isLeftFnAsync;
                    let is_retryable = [<fn_composer__is_retryable_ $fn>]();
                    let current_f = if !is_retryable{
                        [<fn_composer__lifted_fn_ $fn>]($fn)
                    }else {
                        [<fn_composer__lifted_fn_ $fn>]([<fn_composer__ retry_ $fn>])
                    };
                    let f3 = $fLeft.then(current_f);
                    let f3 = compose!(f3,_isResultAsync,$($others)*);
                    f4 = f3;
                }
                f4
            }
        };



        ($fn:ident $($others:tt)*) => {
            {

                use crate::Then;
                let f2;
                crate::paste!{
                    let f = [<fn_composer__lifted_fn_ $fn>]($fn);
                    let isAsync = [<fn_composer__is_async_ $fn>]();
                    let is_retryable = [<fn_composer__is_retryable_ $fn>]();
                    let f = if !is_retryable{
                        [<fn_composer__lifted_fn_ $fn>]($fn)
                    }else {
                        [<fn_composer__lifted_fn_ $fn>]([<fn_composer__ retry_ $fn>])
                    };
                    let f1 = compose!(f,isAsync,$($others)*);
                    f2 = f1;
                };
                f2
            }
        };


    }
}