conditional

Macro conditional 

Source
macro_rules! conditional {
    ($enum:ident, $( $variant:ident => $op:expr ),+ $(,)?) => { ... };
}
Expand description

Creates an Op that conditionally dispatches to one of multiple sub-ops based on the variant of the input enum.

Important Requirements:

  1. The enum must be defined as a single-type-parameter wrapper, e.g.
    enum MyEnum<T> {
        VariantA(T),
        VariantB(T),
    }
    This allows all variants to share the same inner type (T).
  2. All sub-ops must have the same Input type (this T) and the same Output. That is, for each variant, the corresponding op must implement Op<Input = T, Output = Out>.

§Example

use rig::pipeline::*;
use rig::conditional;
use tokio;

#[tokio::main]
async fn main() {
    #[derive(Debug)]
    enum ExampleEnum<T> {
        Variant1(T),
        Variant2(T),
    }

    // Creates a pipeline Op that adds 1 if it’s Variant1, or doubles if it’s Variant2
    let op1 = map(|x: i32| x + 1);
    let op2 = map(|x: i32| x * 2);

    let conditional = conditional!(ExampleEnum,
        Variant1 => op1,
        Variant2 => op2,
    );

    let result1 = conditional.call(ExampleEnum::Variant1(2)).await;
    assert_eq!(result1, 3);

    let result2 = conditional.call(ExampleEnum::Variant2(3)).await;
    assert_eq!(result2, 6);
}