pipe_chain/
lib.rs

1#![feature(round_char_boundary)]
2#![warn(missing_docs)]
3#![doc = include_str!("../readme.md")]
4
5pub mod byte;
6pub mod parser;
7pub mod pipe;
8pub mod str;
9mod tag;
10mod take;
11
12pub use self::{pipe::*, tag::*, take::*};
13use fatal_error::{FatalError, NeverErr};
14
15/// Convenience type to simplify the signature of pipe result types
16///
17/// `R`: remaining input
18///
19/// `O`: Output
20///
21/// `E`: an error wrapped into a FatalError
22pub type Result<R, O, E> = std::result::Result<(R, O), FatalError<E>>;
23
24/// Combinator trait
25pub trait Pipe<I, O, E = NeverErr, R = I> {
26    /// Produces a [Result] from an input
27    ///
28    /// from an input `I` produces either:
29    ///
30    /// - an output `O` and what remains from the input: `R`
31    /// - an error `E`
32    fn apply(&mut self, input: I) -> Result<R, O, E>;
33}
34
35/// a pipe is implemented for all closures with the same signature
36impl<I, O, E, R, F> Pipe<I, O, E, R> for F
37where
38    F: FnMut(I) -> Result<R, O, E>,
39{
40    fn apply(&mut self, input: I) -> Result<R, O, E> { (self)(input) }
41}
42
43impl<I, E> Pipe<I, (), E> for () {
44    fn apply(&mut self, input: I) -> Result<I, (), E> { Ok((input, ())) }
45}