1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{Pipe, Result};
use std::marker::PhantomData;
use tuplify::Unpack;

/// Combinator that calls [tuplify::Unpack::unpack] on it's output
pub trait UnpackExt<I, O, E, R> {
    /// Unpacks the output of a pipe
    ///
    /// Example:
    /// ```rust
    /// # use fatal_error::FatalError;
    /// # use pipe_chain::{Pipe, AndExt, UnpackExt, tag, Incomplete, str::TagStrError};
    /// # use std::error::Error as StdError;
    /// # #[derive(Debug, PartialEq, Eq)]
    /// # enum Error {
    /// #     Incomplete(Incomplete),
    /// #     Tag(TagStrError),
    /// # }
    /// #
    /// # impl std::fmt::Display for Error {
    /// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    /// #        write!(f, "{self:?}")
    /// #     }
    /// # }
    /// # impl StdError for Error {}
    /// # impl From<Incomplete> for Error {
    /// #     fn from(value: Incomplete) -> Error { Error::Incomplete(value) }
    /// # }
    /// #
    /// # impl From<TagStrError> for Error {
    /// #     fn from(value: TagStrError) -> Error { Error::Tag(value) }
    /// # }
    /// // unpacking many elements does nothing
    /// assert_eq!(
    ///     tag::<Error, _, _>("foo").and(tag("bar")).unpack().apply("foobar"),
    ///     Ok(("", ("foo", "bar")))
    /// );
    ///
    /// // single element without unpacking
    /// assert_eq!(tag::<Error, _, _>("foo").apply("foo"), Ok(("", ("foo",))));
    ///
    /// // single element with unpacking
    /// assert_eq!(tag::<Error, _, _>("foo").unpack().apply("foo"), Ok(("", "foo")));
    /// ```
    fn unpack(self) -> UnpackPipe<Self, O>
    where
        O: Unpack,
        Self: Sized,
    {
        UnpackPipe::new(self)
    }
}

impl<I, O, E, R, P> UnpackExt<I, O, E, R> for P where P: Pipe<I, O, E, R> {}

/// [UnpackExt::unpack] implementation
pub struct UnpackPipe<P, O> {
    p: P,
    o: PhantomData<O>,
}

impl<P, O> UnpackPipe<P, O> {
    fn new(p: P) -> Self { Self { p, o: PhantomData } }
}

impl<I, O, E, R, P> Pipe<I, O::Output, E, R> for UnpackPipe<P, O>
where
    O: Unpack,
    P: Pipe<I, O, E, R>,
{
    fn apply(&mut self, input: I) -> Result<R, O::Output, E> {
        self.p.apply(input).map(|(i, o)| (i, o.unpack()))
    }
}