#![no_std]
#[macro_export]
macro_rules! switch {
($input:expr; $first:expr$(, $($conditions:expr),*)? => $execute:expr$(,)?) => {
if $input == $first $(&& $($conditions)&&*)? { $execute }
};
($input:expr; $first:expr$(, $($conditions:expr),*)? => $execute:expr, _ => $execute_last:expr$(,)?) => {
if $input == $first $(&& $($conditions)&&*)? { $execute }
else { $execute_last }
};
($input:expr; $first:expr$(, $($conditions:expr),*)? => $execute:expr, $($rest:expr$(, $($conditions_rest:expr),*)? => $exec:expr),+$(,)?) => {
if $input == $first $(&& $($conditions)&&*)? { $execute }
$(else if $input == $rest $(&& $($conditions_rest)&&*)? { $exec })*
};
($input:expr; $first:expr$(, $($conditions:expr),*)? => $execute:expr, $($rest:expr$(, $($conditions_rest:expr),*)? => $exec:expr),+, _ => $execute_last:expr$(,)?) => {
if $input == $first $(&& $($conditions)&&*)? { $execute }
$(else if $input == $rest $(&& $($conditions_rest)&&*)? { $exec })*
else { $execute_last }
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn single_case() {
let x = 3;
switch! { x;
3 => (),
}
}
#[test]
fn single_case_with_else() {
let x = 3;
switch! { x;
3 => (),
_ => (),
}
}
#[test]
fn multi_case() {
let x = 3;
switch! { x;
3 => (),
3 => (),
3 => (),
3 => (),
}
}
#[test]
fn multi_case_with_else() {
let x = 3;
switch! { x;
3 => (),
3 => (),
3 => (),
3 => (),
_ => (),
}
}
}