slack_messaging/
macros.rs1macro_rules! pipe {
2 ($val:expr => $f:path) => {{
3 $f($val)
4 }};
5 ($val:expr => $f:path | $($g:path)|*) => {{
6 pipe!($f($val) => $($g)|*)
7 }};
8}
9
10#[macro_export]
34macro_rules! plain_text {
35 ($fmt:expr) => {
36 $crate::composition_objects::PlainText::builder()
37 .text(format!($fmt))
38 .build()
39 };
40 ($fmt:expr, $($arg:tt)+) => {
41 $crate::composition_objects::PlainText::builder()
42 .text(format!($fmt, $($arg)+))
43 .build()
44 };
45}
46
47#[macro_export]
71macro_rules! mrkdwn {
72 ($fmt:expr) => {
73 $crate::composition_objects::MrkdwnText::builder()
74 .text(format!($fmt))
75 .build()
76 };
77 ($fmt:expr, $($arg:tt)+) => {
78 $crate::composition_objects::MrkdwnText::builder()
79 .text(format!($fmt, $($arg)+))
80 .build()
81 };
82}
83
84#[cfg(test)]
85mod tests {
86 use crate::composition_objects::{PlainText, MrkdwnText};
87
88 #[test]
89 fn pipe_chains_multiple_functions() {
90 fn add_one(v: usize) -> usize {
91 v + 1
92 }
93
94 fn times_two(v: usize) -> usize {
95 v * 2
96 }
97
98 fn divide_five(v: usize) -> usize {
99 v / 5
100 }
101
102 let v = pipe!(4 => add_one | times_two);
103 assert_eq!(v, 10);
104
105 let v = pipe!(4 => add_one | times_two | divide_five);
106 assert_eq!(v, 2);
107 }
108
109 #[test]
110 fn it_works_macro_plain_text_given_expression() {
111 let text = plain_text!("Hello, Tanaka!");
112 let expected = PlainText::builder().text("Hello, Tanaka!").build();
113 assert_eq!(text, expected);
114 }
115
116 #[test]
117 fn it_works_macro_plain_text_given_expression_and_tokens() {
118 let name = "Tanaka";
119 let text = plain_text!("Hello, {name}!");
120 let expected = PlainText::builder().text("Hello, Tanaka!").build();
121 assert_eq!(text, expected);
122 }
123
124 #[test]
125 fn it_works_macro_mrkdwn_given_expression() {
126 let text = mrkdwn!("Hello, Tanaka!");
127 let expected = MrkdwnText::builder().text("Hello, Tanaka!").build();
128 assert_eq!(text, expected);
129 }
130
131 #[test]
132 fn it_works_macro_mrkdwn_given_expression_and_tokens() {
133 let name = "Tanaka";
134 let text = mrkdwn!("Hello, {name}!");
135 let expected = MrkdwnText::builder().text("Hello, Tanaka!").build();
136 assert_eq!(text, expected);
137 }
138}