pipeline_macro/macros.rs
1/// Helper to pass return of functions as parameter of other functions
2#[doc(hidden)]
3#[macro_export(local_inner_macros)]
4macro_rules! __pipeline_fn {
5    (($fn:ident($($args:expr),*)), $ret:expr) => {
6        $fn($ret $(,$arg)*);
7    };
8    ($fn:expr, $ret:expr) => {
9        $fn($ret)
10    };
11}
12
13
14/// Return new instance of pipeline struct
15///
16/// # Syntax:
17/// ```
18/// # use pipeline_macro::*;
19/// # struct InputType {}
20/// # struct OutputType {}
21/// # fn function1(i: InputType) -> InputType { i }
22/// # fn function2(i: InputType) -> OutputType { OutputType {} }
23/// pipeline! {
24///      InputType => function1 => function2 ;-> OutputType
25/// };
26/// ```
27#[macro_export]
28macro_rules! pipeline {
29    ($in:ty => $($fns:expr) => * ;-> $out:ty) => {
30        {{
31            Pipeline { fun : (| input: $in | {
32                let result = input;
33
34                $(
35                    let result = $crate::__pipeline_fn!($fns, result);
36                )*
37                    return result;
38            })}
39        }}
40    };
41}
42
43