Skip to main content

rust_elm/
macros.rs

1//! Declarative macros for update dispatch and test message enums.
2
3/// Match all variants of an action enum in `update` (exhaustive dispatch helper).
4#[macro_export]
5macro_rules! total_update {
6    ($state:expr, $action:expr, {
7        $($variant:pat => $body:expr,)*
8        _ => $default:expr
9    }) => {{
10        match $action {
11            $($variant => $body,)*
12            _ => $default,
13        }
14    }};
15}
16
17/// Generate a trivial action for property tests.
18#[macro_export]
19macro_rules! arbitrary_msg {
20    ($name:ident { $($variant:ident),* $(,)? }) => {
21        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
22        pub enum $name {
23            $($variant,)*
24        }
25
26        impl $name {
27            pub fn all() -> &'static [$name] {
28                &[$(Self::$variant,)*]
29            }
30        }
31    };
32}
33
34/// Build a [`CombineReducers`] tuple from reducer fn pointers.
35#[macro_export]
36macro_rules! reducers {
37    ($($r:expr),+ $(,)?) => {
38        $crate::reducer::CombineReducers((
39            $( $crate::reducer::coerce_fn($r) ),+
40        ))
41    };
42}