office_hours/macro.rs
1/// Creates an [`OfficeHours`] that takes in a list of statements to execute
2/// when the current time is between office hours.
3///
4/// - Only execute code between the default hours of 9am and 5pm.
5///
6/// ```
7/// use office_hours::office_hours;
8/// office_hours!({ println!("Between 9am and 5pm") });
9/// ```
10///
11/// - Only execute code between the custom hours of 5pm and 10pm.
12///
13/// ```
14/// use office_hours::office_hours;
15/// office_hours!(Clock::FivePm, Clock::TenPm, {
16/// println!("Between 5pm and 10pm")
17/// });
18/// ```
19/// [`OfficeHours`]: crate::OfficeHours
20#[macro_export]
21macro_rules! office_hours {
22 ({ $($code:stmt)* }) => {{
23 use office_hours::Clock;
24 office_hours!(Clock::NineAm, Clock::FivePm, { $($code)* })
25 }};
26 ($start:expr, $finish:expr, { $($code:stmt)* }) => {{
27 use office_hours::{Clock, OfficeHours};
28 let office_hours = OfficeHours::new($start, $finish);
29 if office_hours.now() {
30 $($code)*
31 }
32 }};
33 ($start_var:expr, $finish_var:expr, | $start:pat_param, $finish:pat_param | { $($code:stmt)* }) => {{
34 use office_hours::{Clock, OfficeHours};
35 let office_hours = OfficeHours::new($start_var, $finish_var);
36 let $start = &office_hours.start;
37 let $finish = &office_hours.finish;
38 if office_hours.now() {
39 $($code)*
40 }
41 }};
42}