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