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
//! _("kiam" is "when" in Esperanto)_
//!
//! This crate entroduces [`when!`] macro which provides better syntax for
//! `if`/`else if`/`else` chains. The syntax is similar to `match`.
//!
//! (idea is borrowed from [kotlin][kt-when-expr])
//!
//! [kt-when-expr]: https://kotlinlang.org/docs/reference/control-flow.html#when-expression
#![no_std]
#![forbid(unsafe_code)]
#![deny(missing_docs, broken_intra_doc_links)]

/// Better syntax for `if`/`else if`/`else` similar to `match` syntax
///
/// ## Usage
///
/// Usage is similar to the usage of `match`, but instead of patterns, branches are guarded by boolean expression:
///
/// ```rust
/// kiam::when! {
///     false => (),
///     true => (),
///     // ...
/// }
/// ```
///
/// `_` can be used as a default branch (it's also required to use `when!` in expression possition):
///
/// ```rust
/// let x = kiam::when! {
///     false => 0,
///     _ => 1,
/// };
///
/// assert_eq!(x, 1);
/// ```
///
/// You can also use `let <pat> =` to match a pattern, but in difference with `match` you'll have to provide an expression for every pattern:
///
/// ```rust
/// let a = None;
/// let b = Some(17);
/// let fallible = || Err(());
///
/// let x = kiam::when! {
///     let Some(x) = a => x,
///     let Ok(x) = fallible() => x,
///     let Some(x) = b => (x as u32) + 1,
///     _ => 1,
/// };
///
/// assert_eq!(x, 18);
/// ```
///
/// Last notes:
/// - You can also compare structure litetals without brackets (you can't do thif with `if`/`else if`/`else` chain)
/// - You can mixup boolean-braches with pattern matching
/// - Only one branch is executed (not to be confused with `switch` in C-like languages)
///
/// ```rust
/// let mut x = 0;
///
/// kiam::when! {
///     let Ok(_) = Err::<(), _>(()) => x = 1,
///     true => x = 2,
///     true => x = 3,
///     let Some(n) = Some(42) => x = n,
/// };
///
/// assert_eq!(x, 2);
/// ```
///
/// ```compile_fail
/// #[derive(PartialEq)]
/// struct Struct { a: i32 }
///
/// // This does not compile because of the ambiguity
/// if Struct { a: 0 } == Struct { a: 0 } {
///     // ...
/// }
/// ```
///
/// ```rust
/// #[derive(PartialEq)]
/// struct Struct { a: i32 }
///
/// kiam::when! {
///     // This, on the other hand, compiles fine
///     Struct { a: 0 } == Struct { a: 0 } => {
///         // ...
///     },
/// }
/// ```
///
/// ## Grammar
///
/// ```text
/// grammar:
///                   ╭───────────────>────────────────╮  ╭────>────╮
///                   │                                │  │         │
/// │├──╭── line ──╮──╯── "," ── "_" ── "=>" ── expr ──╰──╯── "," ──╰──┤│
///     │          │
///     ╰── "," ───╯
///
/// line:
///     ╭─────────────>─────────────╮
///     │                           │
/// │├──╯── "let"/i ── pat ── "=" ──╰── expr ── "=>" ── expr ──┤│
/// ```
#[macro_export]
macro_rules! when {
    (
        $(
            $(let $pat:pat = )? $cond:expr => $branch:expr
        ),+
        $(, _ => $def_branch:expr)?
        $(,)?
    ) => {
        $(
            if $(let $pat = )? $cond {
                $branch
            } else
        )+
        {
            $(
                $def_branch
            )?
        }
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let r = when! {
            false => 0,
            true => 1,
            true => 2,
            _ => 42,
        };

        assert_eq!(r, 1);
    }

    #[test]
    fn pattern() {
        let r = when! {
            let Some(x) = None => x,
            let Some(y) = Some(12) => y + 1,
            _ => 0,
        };

        assert_eq!(r, 13);
    }

    #[test]
    fn mixed() {
        let r = when! {
            false => 0,
            let Some(_) = None::<bool> => 0,
            let Some(y) = Some(12) => y + 1,
            true => 1,
            _ => 0,
        };

        assert_eq!(r, 13);
    }

    #[test]
    fn r#struct() {
        #[derive(PartialEq)]
        struct Struct {
            x: i32,
        }

        // won't work with if/elseif/else
        #[allow(clippy::eq_op)]
        let r = when! {
            Struct { x: 0 } == Struct { x: 1 } => 0,
            Struct { x: 22 } == Struct { x: 22 } => 1,
            _ => 2,
        };

        assert_eq!(r, 1);
    }

    #[test]
    fn no_def() {
        let mut x = 0;

        when! {
            false => x = 18,
            true => x += 1,
        }

        assert_eq!(x, 1);
    }
}