AndThenExt

Trait AndThenExt 

Source
pub trait AndThenExt<I, O, E, R> {
    // Provided methods
    fn and_then<O2, F>(self, f: F) -> AndThen<O, Self, F>
       where Self: Sized,
             F: FunMut<O, Output = Result<O2, FatalError<E>>>,
             O: HList,
             O2: HList { ... }
    fn and_thenf<O2, F>(self, f: F) -> AndThenF<O, Self, F>
       where Self: Sized,
             F: FnMut(O) -> Result<O2, FatalError<E>> { ... }
    fn ok_and_then<O2, R2, F>(self, other: F) -> OkAndThen<O, R, Self, F>
       where Self: Sized,
             F: FunMut<(R, O), Output = Result<(R2, O2), FatalError<E>>> { ... }
    fn ok_then<O2, R2, P>(self, other: P) -> OkThen<O, R, Self, P>
       where Self: Pipe<I, O, E, R> + Sized,
             P: Pipe<(R, O), O2, E, R2> + Sized { ... }
}
Expand description

Combinator that applies a faillible function on the output of a pipe

Provided Methods§

Source

fn and_then<O2, F>(self, f: F) -> AndThen<O, Self, F>
where Self: Sized, F: FunMut<O, Output = Result<O2, FatalError<E>>>, O: HList, O2: HList,

Applies the given closure on the output of the pipe

the output of both Pipe and closure and must be an HList

fn yolo(_: &str, _: &str) -> Result<(u8, u8), FatalError<Error>> { Ok((1, 2)) }

assert_eq!(
    tag::<Error, _, _>("1").and(tag("2")).and_then(yolo).apply("12"),
    Ok(("", (1u8, 2u8)))
)
Source

fn and_thenf<O2, F>(self, f: F) -> AndThenF<O, Self, F>
where Self: Sized, F: FnMut(O) -> Result<O2, FatalError<E>>,

Applies the given closure on the output of a pipe

assert_eq!(
    tag::<Error, _, _>("1")
        .and(tag("2"))
        .and_thenf(|(first, second)| Ok(12))
        .apply("12"),
    Ok(("", 12))
)
Source

fn ok_and_then<O2, R2, F>(self, other: F) -> OkAndThen<O, R, Self, F>
where Self: Sized, F: FunMut<(R, O), Output = Result<(R2, O2), FatalError<E>>>,

Applies the given closure to the output and remaining input of a pipe


#[derive(Debug, PartialEq, Eq)]
enum FooBar {
    Foo,
    Bar,
}

fn true_or_false(input: &str) -> Result<&str, (bool,), Error> {
    tag("true").map1(|_| true).or(tag("false").map1(|_| false)).apply(input)
}

fn foobar(input: &str) -> Result<&str, (FooBar,), Error> {
    true_or_false
        .ok_and_then(|x, (y,)| {
            if y {
                tag("bar").map1(|_| FooBar::Bar).apply(x)
            } else {
                tag("foo").map1(|_| FooBar::Foo).apply(x)
            }
        })
        .apply(input)
}

assert_eq!(foobar("truebar"), Ok(("", (FooBar::Bar,))));
assert_eq!(foobar("falsefoo"), Ok(("", (FooBar::Foo,))));
assert_eq!(
    tag::<Error, _, _>("1")
        .and(tag("2"))
        .ok_and_then(|r, (first, second)| Ok(("yolo", (12,))))
        .apply("12"),
    Ok(("yolo", (12,)))
)
Source

fn ok_then<O2, R2, P>(self, other: P) -> OkThen<O, R, Self, P>
where Self: Pipe<I, O, E, R> + Sized, P: Pipe<(R, O), O2, E, R2> + Sized,

Applies the pipes consecutively

the output of both Pipe and closure and must be an HList

#[derive(Debug, PartialEq, Eq)]
enum FooBar {
    Foo,
    Bar,
}

fn true_or_false(input: &str) -> Result<&str, (bool,), Error> {
    tag("true").map1(|_| true).or(tag("false").map1(|_| false)).apply(input)
}

fn foobar(input: &str) -> Result<&str, (FooBar,), Error> {
    true_or_false
        .ok_then(|(x, (y,))| {
            if y {
                tag("bar").map1(|_| FooBar::Bar).apply(x)
            } else {
                tag("foo").map1(|_| FooBar::Foo).apply(x)
            }
        })
        .apply(input)
}

assert_eq!(
    tag::<Error, _, _>("1")
        .and(tag("2"))
        .ok_then(|(r, (first, second))| Ok(("yolo", (12,))))
        .apply("12"),
    Ok(("yolo", (12,)))
);

assert_eq!(foobar("truebar"), Ok(("", (FooBar::Bar,))));
assert_eq!(foobar("falsefoo"), Ok(("", (FooBar::Foo,))));

Implementors§

Source§

impl<I, O, E, R, P> AndThenExt<I, O, E, R> for P
where P: Pipe<I, O, E, R>,