Skip to main content

cond

Function cond 

Source
pub fn cond<I, O, F, P, ArgsF, ArgsP>(
    predicate: F,
    next: P,
) -> Cond<Pipe<F, ArgsF>, Pipe<P, ArgsP>, I, O>
where F: Handler<I, bool, ArgsF>, P: Handler<I, O, ArgsP>, I: Clone + Send + Sync + 'static, O: Send + 'static, ArgsF: Send + Sync + 'static, ArgsP: Send + Sync + 'static,
Expand description

Creates a conditional pipeline. If the predicate returns true, the next pipeline is executed. If false, it returns a PipelineError::Failure.

ยงExample

use pipe_it::{cond, Context, Pipeline, Input, ext::HandlerExt};

async fn is_even(n: Input<i32>) -> bool { *n % 2 == 0 }
async fn process(n: Input<i32>) -> String { "Even".to_string() }

let pipe = cond(is_even, process);

// Success case
let result = pipe.apply(Context::empty(2)).await;
assert_eq!(result.unwrap(), "Even");

// Failure case
let result = pipe.apply(Context::empty(1)).await;
assert!(result.is_err());