Coverage Report

Created: 2026-04-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/combine-4.6.7/src/stream/decoder.rs
Line
Count
Source
1
use crate::{
2
    error::ParseError,
3
    stream::buf_reader::{Buffer, Bufferless, CombineBuffer},
4
};
5
6
use std::{
7
    fmt,
8
    io::{self, Read},
9
};
10
11
#[cfg(feature = "pin-project-lite")]
12
use std::pin::Pin;
13
14
#[derive(Debug)]
15
pub enum Error<E, P> {
16
    Parse(E),
17
    Io { position: P, error: io::Error },
18
}
19
20
impl<'a, P> From<Error<crate::easy::Errors<u8, &'a [u8], P>, P>>
21
    for crate::easy::Errors<u8, &'a [u8], P>
22
where
23
    P: Ord + Clone,
24
{
25
0
    fn from(e: Error<crate::easy::Errors<u8, &'a [u8], P>, P>) -> Self {
26
0
        match e {
27
0
            Error::Parse(e) => e,
28
0
            Error::Io { position, error } => {
29
0
                crate::easy::Errors::from_error(position, crate::easy::Error::Other(error.into()))
30
            }
31
        }
32
0
    }
33
}
34
35
impl<E, P> std::error::Error for Error<E, P>
36
where
37
    E: std::error::Error,
38
    P: fmt::Display + fmt::Debug,
39
{
40
}
41
42
impl<E: fmt::Display, P: fmt::Display> fmt::Display for Error<E, P> {
43
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44
0
        match self {
45
0
            Error::Parse(e) => e.fmt(f),
46
0
            Error::Io { position: _, error } => error.fmt(f),
47
        }
48
0
    }
49
}
50
51
#[derive(Default)]
52
/// Used together with the `decode!` macro
53
pub struct Decoder<S, P, C = Buffer> {
54
    position: P,
55
    state: S,
56
    buffer: C,
57
    end_of_input: bool,
58
}
59
60
impl<S, P> Decoder<S, P, Buffer>
61
where
62
    P: Default,
63
    S: Default,
64
{
65
    /// Constructs a new [`Decoder`] with an internal buffer. Allows any `AsyncRead/Read` instance to
66
    /// be used when decoding but there may be data left in the internal buffer after decoding
67
    /// (accessible with [`Decoder::buffer`])
68
8.12k
    pub fn new() -> Self {
69
8.12k
        Decoder::default()
70
8.12k
    }
<combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::new
Line
Count
Source
68
8.12k
    pub fn new() -> Self {
69
8.12k
        Decoder::default()
70
8.12k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _>>::new
71
72
    /// Constructs a new [`Decoder`] with an internal buffer. Allows any `AsyncRead/Read` instance to
73
    /// be used when decoding but there may be data left in the internal buffer after decoding
74
    /// (accessible with [`Decoder::buffer`])
75
0
    pub fn new_buffer() -> Self {
76
0
        Decoder::new()
77
0
    }
78
}
79
80
impl<S, P> Decoder<S, P, Bufferless>
81
where
82
    P: Default,
83
    S: Default,
84
{
85
    /// Constructs a new `Decoder` without an internal buffer. Requires the read instance to be
86
    /// wrapped with combine's [`BufReader`] instance to
87
    ///
88
    /// [`BufReader`]: super::buf_reader::BufReader
89
0
    pub fn new_bufferless() -> Self {
90
0
        Decoder::default()
91
0
    }
92
}
93
94
impl<S, P> Decoder<S, P> {
95
1.56k
    pub fn buffer(&self) -> &[u8] {
96
1.56k
        &self.buffer.0
97
1.56k
    }
<combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::buffer
Line
Count
Source
95
1.56k
    pub fn buffer(&self) -> &[u8] {
96
1.56k
        &self.buffer.0
97
1.56k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _>>::buffer
98
}
99
100
impl<S, P, C> Decoder<S, P, C> {
101
    #[doc(hidden)]
102
155k
    pub fn advance<R>(&mut self, read: &mut R, removed: usize)
103
155k
    where
104
155k
        C: CombineBuffer<R>,
105
    {
106
        // Remove the data we have parsed and adjust `removed` to be the amount of data we
107
        // committed from `self.reader`
108
155k
        self.buffer.advance(read, removed)
109
155k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::advance::<&mut std::net::tcp::TcpStream>
Unexecuted instantiation: <combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::advance::<&mut std::os::unix::net::stream::UnixStream>
<combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::advance::<&[u8]>
Line
Count
Source
102
155k
    pub fn advance<R>(&mut self, read: &mut R, removed: usize)
103
155k
    where
104
155k
        C: CombineBuffer<R>,
105
    {
106
        // Remove the data we have parsed and adjust `removed` to be the amount of data we
107
        // committed from `self.reader`
108
155k
        self.buffer.advance(read, removed)
109
155k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _, _>>::advance::<_>
110
111
    #[doc(hidden)]
112
    #[cfg(feature = "pin-project-lite")]
113
    pub fn advance_pin<R>(&mut self, read: Pin<&mut R>, removed: usize)
114
    where
115
        C: CombineBuffer<R>,
116
    {
117
        // Remove the data we have parsed and adjust `removed` to be the amount of data we
118
        // committed from `self.reader`
119
        self.buffer.advance_pin(read, removed);
120
    }
121
122
0
    pub fn position(&self) -> &P {
123
0
        &self.position
124
0
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::position
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _, _>>::position
125
126
    #[doc(hidden)]
127
161k
    pub fn __inner(&mut self) -> (&mut S, &mut P, &C, bool) {
128
161k
        (
129
161k
            &mut self.state,
130
161k
            &mut self.position,
131
161k
            &self.buffer,
132
161k
            self.end_of_input,
133
161k
        )
134
161k
    }
<combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::__inner
Line
Count
Source
127
161k
    pub fn __inner(&mut self) -> (&mut S, &mut P, &C, bool) {
128
161k
        (
129
161k
            &mut self.state,
130
161k
            &mut self.position,
131
161k
            &self.buffer,
132
161k
            self.end_of_input,
133
161k
        )
134
161k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _, _>>::__inner
135
}
136
137
impl<S, P, C> Decoder<S, P, C>
138
where
139
    C: ,
140
{
141
    #[doc(hidden)]
142
153k
    pub fn __before_parse<R>(&mut self, mut reader: R) -> io::Result<()>
143
153k
    where
144
153k
        R: Read,
145
153k
        C: crate::stream::buf_reader::CombineSyncRead<R>,
146
    {
147
153k
        if self.buffer.extend_buf_sync(&mut reader)? == 0 {
148
4.42k
            self.end_of_input = true;
149
148k
        }
150
151
153k
        Ok(())
152
153k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::__before_parse::<&mut &mut std::net::tcp::TcpStream>
Unexecuted instantiation: <combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::__before_parse::<&mut &mut std::os::unix::net::stream::UnixStream>
<combine::stream::decoder::Decoder<combine::parser::combinator::AnySendSyncPartialState, combine::stream::PointerOffset<[u8]>>>::__before_parse::<&mut &[u8]>
Line
Count
Source
142
153k
    pub fn __before_parse<R>(&mut self, mut reader: R) -> io::Result<()>
143
153k
    where
144
153k
        R: Read,
145
153k
        C: crate::stream::buf_reader::CombineSyncRead<R>,
146
    {
147
153k
        if self.buffer.extend_buf_sync(&mut reader)? == 0 {
148
4.42k
            self.end_of_input = true;
149
148k
        }
150
151
153k
        Ok(())
152
153k
    }
Unexecuted instantiation: <combine::stream::decoder::Decoder<_, _, _>>::__before_parse::<_>
153
}
154
155
#[cfg(feature = "tokio-02")]
156
impl<S, P, C> Decoder<S, P, C> {
157
    #[doc(hidden)]
158
    pub async fn __before_parse_tokio_02<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
159
    where
160
        R: tokio_02_dep::io::AsyncRead,
161
        C: crate::stream::buf_reader::CombineRead<R, dyn tokio_02_dep::io::AsyncRead>,
162
    {
163
        let copied =
164
            crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
165
                .await?;
166
        if copied == 0 {
167
            self.end_of_input = true;
168
        }
169
170
        Ok(())
171
    }
172
}
173
174
#[cfg(feature = "tokio-03")]
175
impl<S, P, C> Decoder<S, P, C> {
176
    #[doc(hidden)]
177
    pub async fn __before_parse_tokio_03<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
178
    where
179
        R: tokio_03_dep::io::AsyncRead,
180
        C: crate::stream::buf_reader::CombineRead<R, dyn tokio_03_dep::io::AsyncRead>,
181
    {
182
        let copied =
183
            crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
184
                .await?;
185
        if copied == 0 {
186
            self.end_of_input = true;
187
        }
188
189
        Ok(())
190
    }
191
}
192
193
#[cfg(feature = "tokio")]
194
impl<S, P, C> Decoder<S, P, C> {
195
    #[doc(hidden)]
196
    pub async fn __before_parse_tokio<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
197
    where
198
        R: tokio_dep::io::AsyncRead,
199
        C: crate::stream::buf_reader::CombineRead<R, dyn tokio_dep::io::AsyncRead>,
200
    {
201
        let copied =
202
            crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
203
                .await?;
204
        if copied == 0 {
205
            self.end_of_input = true;
206
        }
207
208
        Ok(())
209
    }
210
}
211
212
#[cfg(feature = "futures-03")]
213
impl<S, P, C> Decoder<S, P, C> {
214
    #[doc(hidden)]
215
    pub async fn __before_parse_async<R>(&mut self, reader: Pin<&mut R>) -> io::Result<()>
216
    where
217
        R: futures_io_03::AsyncRead,
218
        C: crate::stream::buf_reader::CombineAsyncRead<R>,
219
    {
220
        let copied = self.buffer.extend_buf(reader).await?;
221
222
        if copied == 0 {
223
            self.end_of_input = true;
224
        }
225
        Ok(())
226
    }
227
}