pipeline-0.5.0/.gitignore000064400007650000024000000001331257176146700135440ustar0000000000000000# Compiled files *.o *.so *.rlib *.dll # Executables *.exe # Generated by Cargo /target/ pipeline-0.5.0/.travis.yml000064400007650000024000000000601257214410500136450ustar0000000000000000language: rust rust: - stable - beta - nightly pipeline-0.5.0/Cargo.toml000064400007650000024000000015531257412622000134740ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "pipeline" version = "0.5.0" authors = ["Johann Hofmann "] description = "A macro collection to pipe |> your functions calls, like in F# or Elixir." readme = "README.md" keywords = ["pipe", "function", "elixir", "macro", "composition"] license = "MIT" repository = "https://github.com/johannhof/pipeline.rs" pipeline-0.5.0/Cargo.toml.orig000064400007650000024000000005361257412622000144330ustar0000000000000000[package] name = "pipeline" version = "0.5.0" authors = ["Johann Hofmann "] description = "A macro collection to pipe |> your functions calls, like in F# or Elixir." repository = "https://github.com/johannhof/pipeline.rs" readme = "README.md" keywords = ["pipe", "function", "elixir", "macro", "composition"] license = "MIT" pipeline-0.5.0/LICENSE000064400007650000024000000020721257176146700125650ustar0000000000000000The MIT License (MIT) Copyright (c) 2015 Johann Hofmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pipeline-0.5.0/README.md000064400007650000024000000052641257412616500130360ustar0000000000000000# pipeline.rs [![](https://travis-ci.org/johannhof/pipeline.rs.svg)](https://travis-ci.org/johannhof/pipeline.rs) [![](https://img.shields.io/crates/v/pipeline.svg)](https://crates.io/crates/pipeline) Pipeline is a macro collection to pipe your functions calls, like in F# or Elixir. Instead of the nice `|>` operator it uses `=>` as a pipe character, due to limitations in the Rust macro system. ## Usage Put this in your Cargo.toml ```toml [dependencies] pipeline = "0.4.1" ``` Then you can import the macros with extern crate and macro_use ```rust #[macro_use] extern crate pipeline; ``` ## Examples ```rust // pipe_res exits the pipeline early if a function returns an Err() let result = pipe_res!("http://rust-lang.org" => download => parse => get_links) ``` ```rust fn times(a: u32, b: u32) -> u32{ return a * b; } let num = pipe!( 4 => (times(10)) => {|i: u32| i * 2} => (times(4)) ); // takes a string length, doubles it and converts it back into a string let length = pipe!( "abcd" => [len] => (as u32) => times(2) => [to_string] ); ``` ## Macros - `pipe!` is the "standard" pipe macro - `pipe_res!` works like `pipe!` but takes only functions that return a `Result` (of the same type) and returns early if that result is an Err. Useful for combining multiple IO transformations like opening a file, reading the contents and making an HTTP request. - `pipe_opt!` works like `pipe!` but takes only functions that return an `Option` (of the same type). The pipeline will continue to operate on the initial value as long as `None` is returned from all functions. If a function in the pipeline returns `Some`, the macro will exit early and return that value. This can be useful if you want to try out several functions to see which can make use of that value in a specified order. ## Syntax Features Any `pipe` starts with an expression as initial value and requires you to specify a function to transform that initial value. ```rust let result = pipe!(2 => times2); ``` You can get more fancy with functions, too, if you add parentheses like in a normal function call, the passed parameters will be applied to that function after the transformed value. > You have to put it in parentheses because the Rust macro system can be very restrictive. If you figure out a way to do it without please make a PR. ```rust let result = pipe!(2 => (times(2))); ``` You can pass closures \o/! A closure must be wrapped in curly brackets (`{}`) ```rust let result = pipe!( 2 => (times(2)) => {|i: u32| i * 2} ); ``` If you want a function to be called as a method on the transform value, put it in square brackets (`[]`). ```rust let result = pipe!( "abcd" => [len] ); ``` pipeline-0.5.0/src/lib.rs000064400007650000024000000111501257412534200134450ustar0000000000000000//! # 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{ return Some(a * 2); } fn nope(_a: u32) -> Option{ 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{ return Ok(a * 2); } fn fail_if_over_4(a: u32) -> Result{ 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"); } }