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:
- The enum must be defined as a single-type-parameter wrapper, e.g.
This allows all variants to share the same inner type (
enum MyEnum<T> { VariantA(T), VariantB(T), }
T
). - All sub-ops must have the same
Input
type (thisT
) and the sameOutput
. That is, for each variant, the corresponding op must implementOp<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);
}