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
//! # pipe_macros
//! A small macro library that allows you to pipe functions
//! similar to the pipe operator in Elixir and F# (|>)

#![deny(missing_docs)]
#![deny(warnings)]

#[macro_export]
macro_rules! pipe_fun {
    (&, $ret:expr) => {
        &$ret;
    };
    ((as $typ:ty), $ret:expr) => {
        $ret as $typ;
    };
    ({$fun:expr}, $ret:expr) => {
        $fun($ret);
    };
    ([$fun:ident], $ret:expr) => {
        $ret.$fun();
    };
    (($fun:ident($($arg:expr),*)), $ret:expr) => {
        $fun($ret $(,$arg)*);
    };
    ($fun:ident, $ret:expr) => {
        $fun($ret);
    }
}

#[macro_export]
macro_rules! pipe {
    ( $expr:expr => $($funs:tt)=>+ ) => {
        {
            let ret = $expr;
            $(
                let ret = pipe_fun!($funs, ret);
            )*
            ret
        }
    };
}

#[macro_export]
macro_rules! pipe_res {
    ( $expr:expr => $($funs:tt)=>+ ) => {
        {
            let ret = Ok($expr);
            $(
                let ret = match ret {
                    Ok(x) => pipe_fun!($funs, x),
                    _ => ret
                };
            )*
            ret
        }
    };
}

#[macro_export]
macro_rules! pipe_opt {
    ( $expr:expr => $($funs:tt)=>+ ) => {
        {
            let ret = None;
            $(
                let ret = match ret {
                    None => pipe_fun!($funs, $expr),
                    _ => ret
                };
            )*
            ret
        }
    };
}

#[cfg(test)]
mod test_pipe_opt{
    fn times2(a: u32) -> Option<u32>{
        return Some(a * 2);
    }

    fn nope(_a: u32) -> Option<u32>{
        return None;
    }

    #[test]
    fn accepts_options() {
        let ret = pipe_opt!(
            4
            => times2
        );

        assert_eq!(ret, Some(8));
    }

    #[test]
    fn accepts_unwrap() {
        let ret = pipe_opt!(
            4
            => times2
        ).unwrap();

        assert_eq!(ret, 8);
    }


    #[test]
    fn exits_early() {
        let ret = pipe_opt!(
            4
            => times2
            => times2
            => times2
        );

        assert_eq!(ret, Some(8));
    }

    #[test]
    fn goes_until_some() {
        let ret = pipe_opt!(
            4
            => nope
            => nope
            => {|_i: u32| None}
            => times2
            => nope
        );

        assert_eq!(ret, Some(8));
    }

    #[test]
    fn ends_with_none() {
        let ret = pipe_opt!(
            4
            => nope
            => nope
            => {|_i: u32| None}
            => nope
        );

        assert_eq!(ret, None);
    }
}


#[cfg(test)]
mod test_pipe_res{
    fn times2(a: u32) -> Result<u32, String>{
        return Ok(a * 2);
    }

    fn fail_if_over_4(a: u32) -> Result<u32, String>{
        if a > 4 {
            return Err("This number is larger than four".to_string());
        }
        return Ok(a);
    }

    #[test]
    fn accepts_results() {
        let ret = pipe_res!(
            4
            => times2
        );

        assert_eq!(ret, Ok(8));
    }

    #[test]
    fn accepts_unwrap() {
        let ret = pipe_res!(
            4
            => times2
        ).unwrap();

        assert_eq!(ret, 8);
    }


    #[test]
    fn chains_result_values() {
        let ret = pipe_res!(
            4
            => times2
            => times2
            => times2
        );

        assert_eq!(ret, Ok(32));
    }

    #[test]
    fn exits_early() {
        let ret = pipe_res!(
            4
            => times2
            => fail_if_over_4
            => times2
            => times2
        );

        assert_eq!(ret, Err("This number is larger than four".to_string()));
    }
}

#[cfg(test)]
mod test_pipe{
    fn times2(a: u32) -> u32{
        return a * 2;
    }

    fn times(a: u32, b: u32, c: u32) -> u32{
        return a * b * c;
    }

    #[test]
    fn test_int() {
        let multiply = |i: u32| i * 2;
        let ret = pipe!(
            4
            => times2
            => {|i: u32| i * 2}
            => multiply
            => (times(100, 10))
        );

        assert_eq!(ret, 32000);
    }

    #[test]
    fn test_string() {
        let ret = pipe!(
            "abcd"
            => [len]
            => (as u32)
            => times2
            => (times(100, 10))
            => [to_string]
        );

        //let ret = "abcd";
        //let ret = ret.len();
        //let ret = ret as u32;
        //let ret = times2(ret);
        //let ret = times(ret, 100, 10);
        //let ret = ret.to_string();

        assert_eq!(ret, times(times2(("abcd".len() as u32)), 100, 10).to_string());
        assert_eq!(ret, "8000");
    }
}