slack_messaging/
macros.rs

1macro_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/// Shorthand to build [`Text`](crate::composition_objects::Text) object with `type` set to
11/// `plain_text`.
12///
13/// ```
14/// # use slack_messaging::plain_text;
15/// # use slack_messaging::composition_objects::{Text, Plain};
16/// let text = plain_text!("Hello, World!");
17/// let expected = Text::<Plain>::builder()
18///     .text("Hello, World!")
19///     .build();
20///
21/// assert_eq!(text, expected);
22///
23/// // You can use this like format! macro.
24/// let greet = "Hi";
25/// let name = "Tanaka";
26///
27/// let text = plain_text!("{greet}, {name}!");
28/// let expected = Text::<Plain>::builder()
29///     .text("Hi, Tanaka!")
30///     .build();
31///
32/// assert_eq!(text, expected);
33/// ```
34#[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/// Shorthand to build [`Text`](crate::composition_objects::Text) object with `type` set to
49/// `mrkdwn`.
50///
51/// ```
52/// # use slack_messaging::mrkdwn;
53/// # use slack_messaging::composition_objects::{Text, Mrkdwn};
54/// let text = mrkdwn!("Hello, World!");
55/// let expected = Text::<Mrkdwn>::builder()
56///     .text("Hello, World!")
57///     .build();
58///
59/// assert_eq!(text, expected);
60///
61/// // You can use this like format! macro.
62/// let greet = "Hi";
63/// let name = "Tanaka";
64///
65/// let text = mrkdwn!("{greet}, {name}!");
66/// let expected = Text::<Mrkdwn>::builder()
67///     .text("Hi, Tanaka!")
68///     .build();
69///
70/// assert_eq!(text, expected);
71/// ```
72#[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}