1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! Use `use enum_macro::em;` to import `em!` macro.
//!
//! Notice: DO NOT import by `extern crate`,
//! there's are some helper macro in the crate.
//!
//! For more information about usage, see [em]

#[doc(hidden)]
#[macro_export]
macro_rules! em_if {
    ( $i:expr, $p:pat, $t:expr, $f:expr ) => {
        if let $p = $i {
            $t
        } else {
            $f
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! get {
    ( $i:expr, $p:path, $pp:tt, $r:expr ) => {
        $crate::em_if!($i, $p$pp, Some($r), None)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! is {
    ( $i:expr, $p:path, $pp:tt, $r:expr ) => {
        $crate::em_if!($i, $p$pp, true, false)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! map {
    ( $i:ident, $p:path, $pp:tt, $r:expr, $f:expr ) => {
        $crate::em_if!($i, $p$pp, $p($f($r)), $i)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! and_then {
    ( $i:ident, $p:path, $pp:tt, ($($r:ident,)+), $f:expr ) => {
        $crate::em_if!($i, $p$pp, $f($($r,)+), $i)
    };
    ( $i:ident, $p:path, $pp:tt, $r:ident, $f:expr ) => {
        $crate::em_if!($i, $p$pp, $f($r), $i)
    };
}

/// enum helper macro
///
/// ```
/// use enum_macro::em;
///
/// #[derive(Clone, Debug, PartialEq)]
/// enum Enum {
///     A(i32),
///     B(i32, i32),
///     C { x: i32, y: i32 },
/// }
///
/// let a = Enum::A(1);
/// let b = Enum::B(2, 3);
/// let c = Enum::C { x: 4, y: 5 };
///
/// // Get Inner Value
///
/// assert_eq!(em!(a.get Enum::A), Some(1));
/// assert_eq!(em!(Enum::A(1) => get Enum::A), Some(1));
/// assert_eq!(em!(b.get Enum::B[i, j]), Some((2, 3)));
/// assert_eq!(em!(c.get Enum::C{x, y}), Some((4, 5)));
/// assert_eq!(em!(c.get Enum::C{y, x}), Some((5, 4)));
/// assert_eq!(em!(c.get Enum::C{x, ..}), Some((4,)));
///
/// // Variants Detect
///
/// assert_eq!(em!(a.is Enum::A), true);
/// assert_eq!(em!(Enum::A(1) => is Enum::A), true);
///
/// assert_eq!(em!(b.is Enum::C{x, y}), false);
/// // or
/// assert_eq!(em!(b.is Enum::C{..}), false);
/// // or
/// assert_eq!(em!(b.is Enum::C{}), false);
///
/// assert_eq!(em!(c.is Enum::B[i, j]), false);
/// // or
/// assert_eq!(em!(c.is Enum::B[..]), false);
/// // or
/// assert_eq!(em!(c.is Enum::B[]), false);
///
/// // Map
///
/// let a2 = a.clone();
/// let b2 = b.clone();
/// assert_eq!(em!(a2.map Enum::A > |x| x + 1), Enum::A(2));
/// assert_eq!(em!(b2.map Enum::A > |x| x + 1), Enum::B(2, 3));
///
/// // And Then
///
/// let a2 = a.clone();
/// assert_eq!(em!(a2.and_then Enum::A > |x| Enum::B(x + 1, 0)), Enum::B(2, 0));
///
/// let b2 = b.clone();
/// assert_eq!(em!(b2.and_then Enum::B[x, y] > |x, y| Enum::C {x, y}), Enum::C { x: 2, y: 3 });
///
/// let b2 = b.clone();
/// // only care order
/// assert_eq!(em!(b2.and_then Enum::B[x, y] > |i, j| Enum::C {x: i, y: j}), Enum::C { x: 2, y: 3 });
/// ```
#[macro_export]
macro_rules! em {
    ( $i:ident.$m:ident $p:path $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, (_x), _x $(, $($a),+)?)
    };
    ( $i:ident.$m:ident $p:path[$(..)?] $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, (..), () $(, $($a),+)?)
    };
    ( $i:ident.$m:ident $p:path[$($x:ident),+] $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, ($($x,)+), ($($x,)+) $(, $($a),+)?)
    };
    ( $i:ident.$m:ident $p:path{$($($x:ident),+$(,)?)? $(..)?} $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, { $($($x,)+)? .. }, ($($($x,)+)?) $(, $($a),+)?)
    };
    ( $i:expr => $m:ident $p:path $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, (_x), _x $(, $($a),+)?)
    };
    ( $i:expr => $m:ident $p:path[$(..)?] $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, (..), () $(, $($a),+)?)
    };
    ( $i:expr => $m:ident $p:path[$($x:ident),+] $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, ($($x,)+), ($($x,)+) $(, $($a),+)?)
    };
    ( $i:expr => $m:ident $p:path{$($($x:ident),+$(,)?)? $(..)?} $(> $($a:expr),+)? ) => {
        $crate::$m!($i, $p, { $($($x,)+)? .. }, ($($($x,)+)?) $(, $($a),+)?)
    };
}

#[cfg(test)]
mod tests {
    #[derive(Debug, PartialEq)]
    enum Enum {
        A(i32),
        B(i32, i32),
        C { x: i32, y: i32 },
    }

    fn init() -> (Enum, Enum, Enum) {
        let a = Enum::A(1);
        let b = Enum::B(2, 3);
        let c = Enum::C { x: 4, y: 5 };
        (a, b, c)
    }

    #[test]
    fn get() {
        let (a, b, c) = init();
        assert_eq!(em!(a.get Enum::A), Some(1));
        assert_eq!(em!(Enum::A(1) => get Enum::A), Some(1));
        assert_eq!(em!(b.get Enum::B[i, j]), Some((2, 3)));
        assert_eq!(em!(c.get Enum::C{x, y}), Some((4, 5)));
        assert_eq!(em!(c.get Enum::C{x, ..}), Some((4,)));
    }

    #[test]
    fn not_get() {
        let (a, b, c) = init();
        assert_eq!(em!(a.get Enum::C{x, y}), None);
        assert_eq!(em!(b.get Enum::A), None);
        assert_eq!(em!(c.get Enum::B[i, j]), None);
    }

    #[test]
    fn is() {
        let (a, b, c) = init();
        assert_eq!(em!(a.is Enum::A), true);
        assert_eq!(em!(b.is Enum::B[]), true);
        assert_eq!(em!(c.is Enum::C{}), true);
    }

    #[test]
    fn isnt() {
        let (a, b, c) = init();
        assert_eq!(em!(a.is Enum::C{}), false);
        assert_eq!(em!(b.is Enum::A), false);
        assert_eq!(em!(c.is Enum::B[]), false);
    }

    #[test]
    fn map() {
        let (a, b, _c) = init();
        assert_eq!(em!(a.map Enum::A > |x| x + 1), Enum::A(2));
        assert_eq!(em!(b.map Enum::A > |x| x + 1), Enum::B(2, 3));
    }

    #[test]
    fn and_then() {
        let (a, b, c) = init();
        assert_eq!(
            em!(a.and_then Enum::A > |x| Enum::B(x + 1, 0)),
            Enum::B(2, 0)
        );
        assert_eq!(
            em!(b.and_then Enum::B[x, y] > |x, y| Enum::C {x, y}),
            Enum::C { x: 2, y: 3 }
        );
        assert_eq!(
            em!(c.and_then Enum::A > |x| Enum::B(x - 1, 0)),
            Enum::C { x: 4, y: 5 }
        );
    }
}