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§
Sourcefn and_thenf<O2, F>(self, f: F) -> AndThenF<O, Self, F>
fn and_thenf<O2, F>(self, f: F) -> AndThenF<O, Self, F>
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))
)Sourcefn ok_and_then<O2, R2, F>(self, other: F) -> OkAndThen<O, R, Self, F>
fn ok_and_then<O2, R2, F>(self, other: F) -> OkAndThen<O, R, Self, F>
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,)))
)Sourcefn ok_then<O2, R2, P>(self, other: P) -> OkThen<O, R, Self, P>
fn ok_then<O2, R2, P>(self, other: P) -> OkThen<O, R, Self, P>
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,))));