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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

pub mod iter;
#[cfg(feature = "nom-adapters")]
pub mod nom;
pub mod read;
pub mod utf8;

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum Streaming<T> {
    Item(T),
    Incomplete,
}

pub type Parsed<T> = (Option<T>, usize);

pub trait Buffer {
    type Output: ?Sized;

    fn buffer(&mut self) -> (&Self::Output, &mut usize);
    fn exhausted(&self) -> bool;
}

pub trait Advance {
    type Error;

    fn advance(&mut self) -> Result<(), Self::Error>;
}

pub trait AdvanceAsync {
    type Error;

    fn poll_advance(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>>;
}

pub trait Parse<'i> {
    type Input: 'i;
    type Output;
    type Error;

    fn parse(&mut self, input: Self::Input)
        -> Result<Streaming<Parsed<Self::Output>>, Self::Error>;

    fn parse_eof(&mut self, input: Self::Input) -> Result<Parsed<Self::Output>, Self::Error>;
}

#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
struct AdvanceAsyncFuture<'a, B: ?Sized>(&'a mut B);

impl<B> Future for AdvanceAsyncFuture<'_, B>
where
    B: ?Sized + AdvanceAsync + Unpin,
{
    type Output = Result<(), B::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = &mut *self;
        Pin::new(&mut *this.0).poll_advance(cx)
    }
}

pub(crate) fn advance_async<B>(src: &mut B) -> impl Future<Output = Result<(), B::Error>> + '_
where
    B: ?Sized + AdvanceAsync + Unpin,
{
    AdvanceAsyncFuture(src)
}

pub struct StreamParser<P, S> {
    parser: P,
    source: S,
}

impl<P, S> StreamParser<P, S> {
    pub fn new(parser: P, source: S) -> Self {
        StreamParser { parser, source }
    }

    pub fn advance(&mut self) -> Result<(), S::Error>
    where
        S: Advance,
    {
        self.source.advance()
    }

    pub fn advance_async(&mut self) -> impl Future<Output = Result<(), S::Error>> + '_
    where
        S: AdvanceAsync + Unpin,
    {
        advance_async(&mut self.source)
    }

    pub fn get(&mut self) -> Result<Option<Streaming<<P as Parse>::Output>>, <P as Parse>::Error>
    where
        S: Buffer,
        for<'i> P: Parse<'i, Input = &'i S::Output>,
    {
        let exhausted = self.source.exhausted();
        let (input, pos) = self.source.buffer();
        match self.parser.parse(input) {
            Ok(Streaming::Item((output, parsed))) => {
                *pos += parsed;
                Ok(output.map(Streaming::Item))
            }
            Ok(Streaming::Incomplete) => {
                if exhausted {
                    match self.parser.parse_eof(input) {
                        Ok((output, parsed)) => {
                            *pos += parsed;
                            Ok(output.map(Streaming::Item))
                        }
                        Err(err) => Err(err),
                    }
                } else {
                    Ok(Some(Streaming::Incomplete))
                }
            }
            Err(err) => Err(err),
        }
    }
}

#[cfg(all(doctest, feature = "std"))]
doc_comment::doctest!("../README.md");

#[cfg(test)]
mod tests;