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
// devela::ops::iif
//
//! Miscellaneous items.
//
/// *`i`nline `if`*.
///
/// A inline alternative for `if` and `if let`.
///
/// # Examples
///
/// Replacing `if`:
/// ```
/// use devela::ops::iif;
///
/// // This
/// let s = iif![1 > 0; true; false];
///
/// // Would be equivalent to
/// let s = if 1 > 0 {
/// true
/// } else {
/// false
/// };
/// ```
///
/// Replacing `if let`:
/// ```
/// use devela::ops::iif;
///
/// let num = Some(123);
///
/// // This
/// iif![let Some(n) = num ; println!("num:{n}") ; { dbg![num]; }];
///
/// // Would be equivalent to
/// if let Some(n) = num {
/// println!("num:{n}")
/// } else {
/// dbg![num];
/// }
/// ```
///
/// Nested:
/// ```
/// use devela::ops::iif;
///
/// let mut s = String::new();
/// let is_premium = Some(true);
///
/// // This
/// iif![let Some(b) = is_premium; iif![b; s += " [premium]"]];
///
/// // Would be equivalent to
/// if let Some(b) = is_premium {
/// if b {
/// s += " [premium]";
/// }
/// }
/// ```
#[macro_export]
macro_rules! iif {
($if:expr; $true:expr) => {
if $if {
$true
}
};
($if:expr ; $true:expr ; $($false:expr)?) => {
if $if {
$true
} else {
$( $false )?
}
};
(let $pat:pat = $if:expr ; $true:expr) => {
if let $pat = $if {
$true
}
};
(let $pat:pat = $if:expr ; $true:expr ; $($false:expr)? ) => {
if let $pat = $if {
$true
} else {
$( $false )?
}
};
}
pub use iif;
#[cfg(test)]
mod test_iif {
use crate::ops::iif;
#[test]
fn iif() {
assert_eq!('a', iif!(true ; 'a' ; 'b'));
assert_eq!('b', iif!(false ; 'a' ; 'b'));
}
#[test]
fn iif_let() {
let somea = Some('a');
let noa: Option<char> = None;
assert_eq!('a', iif!(let Some(a) = somea; a ; 'b'));
assert_eq!('b', iif!(let Some(a) = noa; a ; 'b'));
}
}
#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
#[test]
fn iif() {
assert_eq!('a', iif!(true ; 'a' ; 'b'));
assert_eq!('b', iif!(false ; 'a' ; 'b'));
}
#[test]
fn iif_let() {
let somea = Some('a');
let noa: Option<char> = None;
assert_eq!('a', iif!(let Some(a) = somea; a ; 'b'));
assert_eq!('b', iif!(let Some(a) = noa; a ; 'b'));
}
}