Macro timrs_pipe_macro::pipe

source ·
pipe!() { /* proc-macro */ }
Expand description

Macro providing the custom piping binary operators before (|>) and after (<|), operations can be parenthesized and are left-associative.

§Examples

§After Operator (<|):

use timrs_pipe_macro::pipe;

fn increment(x: i32) -> i32 { x + 1 }
fn to_string(x: i32) -> String { x.to_string() }
fn greet(x: String) -> String { format!("HELLO {x}!") }

assert_eq!(
    pipe! { 1 ->> greet <| to_string <| increment },
    "HELLO 2!"
);

§Before Operator (|>):

use timrs_pipe_macro::pipe;

fn increment(x: i32) -> i32 { x + 1 }
fn to_string(x: i32) -> String { x.to_string() }
fn greet(x: String) -> String { format!("HELLO {x}!") }

assert_eq!(
    pipe! { 1 ->> increment |> to_string |> greet },
    "HELLO 2!"
);

§Grammar

The grammar rules for the pipe! macro are the following:

§Tokens

e_e        := (end of input)
e_function := (any valid callable rust expression, i.e. can be called with the following syntax `f ()`)
e_input    := (any valid rust expression that can be used as a function input)
s_insert   := "->>"
s_p_left   := "("
s_p_right  := ")"
s_o_after  := "<|"
s_o_before := "|>"

§Rules

pipe                    := e_input s_insert operation e_e
operation               := parenthesized_operation | operand operator operand
parenthesized_operation := s_p_left operation s_p_right
operator                := s_o_after | s_o_before
operand                 := parenthesized_operand | operation | e_function
parenthesized_operand   := s_p_left operand s_p_right