Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

942
vendor/combine/src/stream/buf_reader.rs vendored Normal file
View File

@@ -0,0 +1,942 @@
use std::io::{self, BufRead, Read};
#[cfg(any(
feature = "futures-03",
feature = "tokio-02",
feature = "tokio-03",
feature = "tokio"
))]
use std::pin::Pin;
#[cfg(any(feature = "futures-03", feature = "tokio-02", feature = "tokio-03"))]
use std::mem::MaybeUninit;
#[cfg(feature = "futures-core-03")]
use std::task::{Context, Poll};
#[cfg(feature = "futures-03")]
use std::future::Future;
use bytes::{Buf, BufMut, BytesMut};
#[cfg(feature = "pin-project-lite")]
use pin_project_lite::pin_project;
#[cfg(feature = "tokio-03")]
use tokio_03_dep::io::AsyncBufRead as _;
#[cfg(feature = "tokio")]
use tokio_dep::io::AsyncBufRead as _;
#[cfg(feature = "futures-core-03")]
use futures_core_03::ready;
#[cfg(feature = "pin-project-lite")]
pin_project! {
/// `BufReader` used by `Decoder` when it is constructed with [`Decoder::new_bufferless`][]
///
/// [`Decoder::new_bufferless`]: ../decoder/struct.Decoder.html#method.new_bufferless
#[derive(Debug)]
pub struct BufReader<R> {
#[pin]
inner: R,
buf: BytesMut
}
}
#[cfg(not(feature = "pin-project-lite"))]
/// `BufReader` used by `Decoder` when it is constructed with [`Decoder::new_bufferless`][]
///
/// [`Decoder::new_bufferless`]: ../decoder/struct.Decoder.html#method.new_bufferless
#[derive(Debug)]
pub struct BufReader<R> {
inner: R,
buf: BytesMut,
}
impl<R> BufReader<R> {
/// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB,
/// but may change in the future.
pub fn new(inner: R) -> Self {
Self::with_capacity(8096, inner)
}
/// Creates a new `BufReader` with the specified buffer capacity.
pub fn with_capacity(capacity: usize, inner: R) -> Self {
let buf = BytesMut::with_capacity(capacity);
Self { inner, buf }
}
/// Gets a reference to the underlying reader.
///
/// It is inadvisable to directly read from the underlying reader.
pub fn get_ref(&self) -> &R {
&self.inner
}
/// Gets a mutable reference to the underlying reader.
///
/// It is inadvisable to directly read from the underlying reader.
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
#[cfg(feature = "pin-project-lite")]
/// Gets a pinned mutable reference to the underlying reader.
///
/// It is inadvisable to directly read from the underlying reader.
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
self.project().inner
}
/// Consumes this `BufWriter`, returning the underlying reader.
///
/// Note that any leftover data in the internal buffer is lost.
pub fn into_inner(self) -> R {
self.inner
}
/// Returns a reference to the internally buffered data.
///
/// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty.
pub fn buffer(&self) -> &[u8] {
&self.buf
}
/// Invalidates all data in the internal buffer.
#[inline]
#[cfg(any(feature = "tokio-02", feature = "tokio-03", feature = "tokio"))]
fn discard_buffer(self: Pin<&mut Self>) {
let me = self.project();
me.buf.clear();
}
}
mod sealed {
pub trait Sealed {}
}
#[doc(hidden)]
pub trait CombineBuffer<R>: sealed::Sealed {
fn buffer<'a>(&'a self, read: &'a R) -> &'a [u8];
fn advance(&mut self, read: &mut R, len: usize);
#[cfg(feature = "pin-project-lite")]
fn advance_pin(&mut self, read: Pin<&mut R>, len: usize);
}
#[doc(hidden)]
pub trait CombineSyncRead<R>: CombineBuffer<R> {
fn extend_buf_sync(&mut self, read: &mut R) -> io::Result<usize>;
}
#[cfg(any(feature = "tokio-02", feature = "tokio-03", feature = "tokio"))]
#[doc(hidden)]
pub trait CombineRead<R, T: ?Sized>: CombineBuffer<R> {
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>>;
}
#[cfg(feature = "futures-03")]
#[doc(hidden)]
pub trait CombineAsyncRead<R>: CombineBuffer<R> {
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>>;
fn extend_buf<'a>(&'a mut self, read: Pin<&'a mut R>) -> ExtendBuf<'a, Self, R>
where
Self: Sized;
}
#[cfg(feature = "futures-03")]
pin_project_lite::pin_project! {
#[doc(hidden)]
pub struct ExtendBuf<'a, C, R> {
buffer: &'a mut C,
read: Pin<&'a mut R>
}
}
#[cfg(feature = "futures-03")]
impl<'a, C, R> Future for ExtendBuf<'a, C, R>
where
C: CombineAsyncRead<R>,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let me = self.project();
me.buffer.poll_extend_buf(cx, me.read.as_mut())
}
}
/// Marker used by `Decoder` for an internal buffer
#[derive(Default)]
pub struct Buffer(pub(crate) BytesMut);
impl sealed::Sealed for Buffer {}
impl<R> CombineBuffer<R> for Buffer {
fn buffer<'a>(&'a self, _read: &'a R) -> &'a [u8] {
&self.0
}
fn advance(&mut self, _read: &mut R, len: usize) {
self.0.advance(len);
}
#[cfg(feature = "pin-project-lite")]
fn advance_pin(&mut self, _read: Pin<&mut R>, len: usize) {
self.0.advance(len);
}
}
impl<R> CombineSyncRead<R> for Buffer
where
R: Read,
{
fn extend_buf_sync(&mut self, read: &mut R) -> io::Result<usize> {
extend_buf_sync(&mut self.0, read)
}
}
#[cfg(feature = "futures-03")]
impl<R> CombineAsyncRead<R> for Buffer
where
R: futures_io_03::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>> {
poll_extend_buf(&mut self.0, cx, read)
}
fn extend_buf<'a>(&'a mut self, read: Pin<&'a mut R>) -> ExtendBuf<'a, Self, R> {
if !self.0.has_remaining_mut() {
self.0.reserve(8 * 1024);
}
// Copy of tokio's read_buf method (but it has to force initialize the buffer)
let bs = self.0.chunk_mut();
for i in 0..bs.len() {
bs.write_byte(i, 0);
}
ExtendBuf { buffer: self, read }
}
}
#[cfg(feature = "tokio-02")]
impl<R> CombineRead<R, dyn tokio_02_dep::io::AsyncRead> for Buffer
where
R: tokio_02_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>> {
if !self.0.has_remaining_mut() {
self.0.reserve(8 * 1024);
}
read.poll_read_buf(cx, &mut Bytes05(&mut self.0))
}
}
#[cfg(feature = "tokio-03")]
fn tokio_03_to_read_buf(bs: &mut BytesMut) -> tokio_03_dep::io::ReadBuf<'_> {
let uninit = bs.chunk_mut();
unsafe {
tokio_03_dep::io::ReadBuf::uninit(std::slice::from_raw_parts_mut(
uninit.as_mut_ptr() as *mut MaybeUninit<u8>,
uninit.len(),
))
}
}
#[cfg(feature = "tokio-03")]
impl<R> CombineRead<R, dyn tokio_03_dep::io::AsyncRead> for Buffer
where
R: tokio_03_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>> {
tokio_03_read_buf(cx, read, &mut self.0)
}
}
#[cfg(feature = "tokio-03")]
fn tokio_03_read_buf(
cx: &mut Context<'_>,
read: Pin<&mut impl tokio_03_dep::io::AsyncRead>,
bs: &mut bytes::BytesMut,
) -> Poll<io::Result<usize>> {
if !bs.has_remaining_mut() {
bs.reserve(8 * 1024);
}
let mut buf = tokio_03_to_read_buf(bs);
ready!(read.poll_read(cx, &mut buf))?;
unsafe {
let n = buf.filled().len();
bs.advance_mut(n);
Poll::Ready(Ok(n))
}
}
#[cfg(feature = "tokio")]
impl<R> CombineRead<R, dyn tokio_dep::io::AsyncRead> for Buffer
where
R: tokio_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>> {
tokio_read_buf(read, cx, &mut self.0)
}
}
#[cfg(feature = "tokio")]
fn tokio_read_buf(
read: Pin<&mut impl tokio_dep::io::AsyncRead>,
cx: &mut Context<'_>,
bs: &mut bytes::BytesMut,
) -> Poll<io::Result<usize>> {
if !bs.has_remaining_mut() {
bs.reserve(8 * 1024);
}
tokio_util::io::poll_read_buf(read, cx, bs)
}
/// Marker used by `Decoder` for an external buffer
#[derive(Default)]
pub struct Bufferless;
impl sealed::Sealed for Bufferless {}
impl<R> CombineBuffer<BufReader<R>> for Bufferless {
fn buffer<'a>(&'a self, read: &'a BufReader<R>) -> &'a [u8] {
&read.buf
}
fn advance(&mut self, read: &mut BufReader<R>, len: usize) {
read.buf.advance(len);
}
#[cfg(feature = "pin-project-lite")]
fn advance_pin(&mut self, read: Pin<&mut BufReader<R>>, len: usize) {
read.project().buf.advance(len);
}
}
impl<R> CombineSyncRead<BufReader<R>> for Bufferless
where
R: Read,
{
fn extend_buf_sync(&mut self, read: &mut BufReader<R>) -> io::Result<usize> {
extend_buf_sync(&mut read.buf, &mut read.inner)
}
}
fn extend_buf_sync<R>(buf: &mut BytesMut, read: &mut R) -> io::Result<usize>
where
R: Read,
{
let size = 8 * 1024;
if !buf.has_remaining_mut() {
buf.reserve(size);
}
// Copy of tokio's poll_read_buf method (but it has to force initialize the buffer)
let n = {
let bs = buf.chunk_mut();
let initial_size = bs.len().min(size);
let bs = &mut bs[..initial_size];
for i in 0..bs.len() {
bs.write_byte(i, 0);
}
// Convert to `&mut [u8]`
// SAFETY: the entire buffer is preinitialized above
let bs = unsafe { &mut *(bs as *mut _ as *mut [u8]) };
let n = read.read(bs)?;
assert!(
n <= bs.len(),
"AsyncRead reported that it initialized more than the number of bytes in the buffer"
);
n
};
// SAFETY: the entire buffer has been preinitialized
unsafe { buf.advance_mut(n) };
Ok(n)
}
#[cfg(feature = "tokio-02")]
struct Bytes05<'a>(&'a mut BytesMut);
#[cfg(feature = "tokio-02")]
impl bytes_05::BufMut for Bytes05<'_> {
fn remaining_mut(&self) -> usize {
self.0.remaining_mut()
}
unsafe fn advance_mut(&mut self, cnt: usize) {
self.0.advance_mut(cnt)
}
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
unsafe { &mut *(self.0.chunk_mut() as *mut _ as *mut [MaybeUninit<u8>]) }
}
}
#[cfg(feature = "tokio-02")]
impl<R> CombineRead<BufReader<R>, dyn tokio_02_dep::io::AsyncRead> for Bufferless
where
R: tokio_02_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut BufReader<R>>,
) -> Poll<io::Result<usize>> {
let me = read.project();
if !me.buf.has_remaining_mut() {
me.buf.reserve(8 * 1024);
}
tokio_02_dep::io::AsyncRead::poll_read_buf(me.inner, cx, &mut Bytes05(me.buf))
}
}
#[cfg(feature = "tokio-03")]
impl<R> CombineRead<BufReader<R>, dyn tokio_03_dep::io::AsyncRead> for Bufferless
where
R: tokio_03_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut BufReader<R>>,
) -> Poll<io::Result<usize>> {
let me = read.project();
tokio_03_read_buf(cx, me.inner, me.buf)
}
}
#[cfg(feature = "tokio")]
impl<R> CombineRead<BufReader<R>, dyn tokio_dep::io::AsyncRead> for Bufferless
where
R: tokio_dep::io::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut BufReader<R>>,
) -> Poll<io::Result<usize>> {
let me = read.project();
tokio_read_buf(me.inner, cx, me.buf)
}
}
#[cfg(feature = "futures-03")]
impl<R> CombineAsyncRead<BufReader<R>> for Bufferless
where
R: futures_io_03::AsyncRead,
{
fn poll_extend_buf(
&mut self,
cx: &mut Context<'_>,
read: Pin<&mut BufReader<R>>,
) -> Poll<io::Result<usize>> {
let me = read.project();
poll_extend_buf(me.buf, cx, me.inner)
}
fn extend_buf<'a>(
&'a mut self,
mut read: Pin<&'a mut BufReader<R>>,
) -> ExtendBuf<'a, Self, BufReader<R>> {
let me = read.as_mut().project();
if !me.buf.has_remaining_mut() {
me.buf.reserve(8 * 1024);
}
// Copy of tokio's read_buf method (but it has to force initialize the buffer)
let bs = me.buf.chunk_mut();
for i in 0..bs.len() {
bs.write_byte(i, 0);
}
ExtendBuf { buffer: self, read }
}
}
#[cfg(feature = "futures-03")]
fn poll_extend_buf<R>(
buf: &mut BytesMut,
cx: &mut Context<'_>,
read: Pin<&mut R>,
) -> Poll<io::Result<usize>>
where
R: futures_io_03::AsyncRead,
{
// Copy of tokio's read_buf method (but it has to force initialize the buffer)
let n = {
let bs = buf.chunk_mut();
// preinit the buffer
for i in 0..bs.len() {
bs.write_byte(i, 0);
}
// Convert to `&mut [u8]`
// SAFETY: preinitialize the buffer
let bs = unsafe { &mut *(bs as *mut _ as *mut [u8]) };
let n = ready!(read.poll_read(cx, bs))?;
assert!(
n <= bs.len(),
"AsyncRead reported that it initialized more than the number of bytes in the buffer"
);
n
};
// SAFETY: the buffer was preinitialized
unsafe { buf.advance_mut(n) };
Poll::Ready(Ok(n))
}
#[cfg(feature = "tokio-02")]
impl<R: tokio_02_dep::io::AsyncRead> tokio_02_dep::io::AsyncRead for BufReader<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
use tokio_02_dep::io::AsyncBufRead;
// If we don't have any buffered data and we're doing a massive read
// (larger than our internal buffer), bypass our internal buffer
// entirely.
if !self.buf.has_remaining_mut() && buf.len() >= self.buf.len() {
let res = ready!(self.as_mut().get_pin_mut().poll_read(cx, buf));
self.discard_buffer();
return Poll::Ready(res);
}
let mut rem = ready!(self.as_mut().poll_fill_buf(cx))?;
let nread = rem.read(buf)?;
self.consume(nread);
Poll::Ready(Ok(nread))
}
// we can't skip unconditionally because of the large buffer case in read.
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
}
#[cfg(feature = "tokio-02")]
impl<R: tokio_02_dep::io::AsyncRead> tokio_02_dep::io::AsyncBufRead for BufReader<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
let me = self.project();
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the underlying reader.
// Branch using `>=` instead of the more correct `==`
// to tell the compiler that the pos..cap slice is always valid.
if me.buf.is_empty() {
ready!(me.inner.poll_read_buf(cx, &mut Bytes05(me.buf)))?;
}
Poll::Ready(Ok(&me.buf[..]))
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let me = self.project();
me.buf.advance(amt);
}
}
#[cfg(feature = "tokio-02")]
impl<R: tokio_02_dep::io::AsyncRead + tokio_02_dep::io::AsyncWrite> tokio_02_dep::io::AsyncWrite
for BufReader<R>
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.get_pin_mut().poll_write(cx, buf)
}
fn poll_write_buf<B: bytes_05::Buf>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>> {
self.get_pin_mut().poll_write_buf(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_shutdown(cx)
}
}
#[cfg(feature = "tokio-03")]
impl<R: tokio_03_dep::io::AsyncRead> tokio_03_dep::io::AsyncRead for BufReader<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut tokio_03_dep::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
// If we don't have any buffered data and we're doing a massive read
// (larger than our internal buffer), bypass our internal buffer
// entirely.
if !self.buf.has_remaining_mut() && buf.remaining() >= self.buf.len() {
let res = ready!(self.as_mut().get_pin_mut().poll_read(cx, buf));
self.discard_buffer();
return Poll::Ready(res);
}
let rem = ready!(self.as_mut().poll_fill_buf(cx))?;
let amt = std::cmp::min(rem.len(), buf.remaining());
buf.put_slice(&rem[..amt]);
self.consume(amt);
Poll::Ready(Ok(()))
}
}
#[cfg(feature = "tokio-03")]
impl<R: tokio_03_dep::io::AsyncRead> tokio_03_dep::io::AsyncBufRead for BufReader<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
let me = self.project();
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the underlying reader.
if me.buf.is_empty() {
ready!(tokio_03_read_buf(cx, me.inner, me.buf))?;
}
Poll::Ready(Ok(&me.buf[..]))
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let me = self.project();
me.buf.advance(amt);
}
}
#[cfg(feature = "tokio-03")]
impl<R: tokio_03_dep::io::AsyncRead + tokio_03_dep::io::AsyncWrite> tokio_03_dep::io::AsyncWrite
for BufReader<R>
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.get_pin_mut().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_shutdown(cx)
}
}
#[cfg(feature = "tokio")]
impl<R: tokio_dep::io::AsyncRead> tokio_dep::io::AsyncRead for BufReader<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut tokio_dep::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
// If we don't have any buffered data and we're doing a massive read
// (larger than our internal buffer), bypass our internal buffer
// entirely.
if !self.buf.has_remaining_mut() && buf.remaining() >= self.buf.len() {
let res = ready!(self.as_mut().get_pin_mut().poll_read(cx, buf));
self.discard_buffer();
return Poll::Ready(res);
}
let rem = ready!(self.as_mut().poll_fill_buf(cx))?;
let amt = std::cmp::min(rem.len(), buf.remaining());
buf.put_slice(&rem[..amt]);
self.consume(amt);
Poll::Ready(Ok(()))
}
}
#[cfg(feature = "tokio")]
impl<R: tokio_dep::io::AsyncRead> tokio_dep::io::AsyncBufRead for BufReader<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
let me = self.project();
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the underlying reader.
if me.buf.is_empty() {
ready!(tokio_read_buf(me.inner, cx, me.buf))?;
}
Poll::Ready(Ok(&me.buf[..]))
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let me = self.project();
me.buf.advance(amt);
}
}
#[cfg(feature = "tokio")]
impl<R: tokio_dep::io::AsyncRead + tokio_dep::io::AsyncWrite> tokio_dep::io::AsyncWrite
for BufReader<R>
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.get_pin_mut().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_shutdown(cx)
}
}
impl<R: Read> Read for BufReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// If we don't have any buffered data and we're doing a massive read
// (larger than our internal buffer), bypass our internal buffer
// entirely.
if !self.buf.has_remaining_mut() && buf.len() >= self.buf.len() {
let res = self.read(buf);
self.buf.clear();
return res;
}
let nread = {
let mut rem = self.fill_buf()?;
rem.read(buf)?
};
self.consume(nread);
Ok(nread)
}
}
impl<R: Read> BufRead for BufReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the underlying reader.
// Branch using `>=` instead of the more correct `==`
// to tell the compiler that the pos..cap slice is always valid.
if self.buf.is_empty() {
Bufferless.extend_buf_sync(self)?;
}
Ok(&self.buf[..])
}
fn consume(&mut self, amt: usize) {
self.buf.advance(amt);
}
}
#[cfg(test)]
#[cfg(feature = "tokio-02")]
mod tests {
use super::{BufReader, Bufferless, CombineRead};
use std::{io, pin::Pin};
use {
bytes_05::BytesMut,
tokio_02_dep::{
self as tokio,
io::{AsyncRead, AsyncReadExt},
},
};
impl<R: AsyncRead> BufReader<R> {
async fn extend_buf_tokio_02(mut self: Pin<&mut Self>) -> io::Result<usize> {
crate::future_ext::poll_fn(|cx| Bufferless.poll_extend_buf(cx, self.as_mut())).await
}
}
#[tokio::test]
async fn buf_reader() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [1, 2, 3]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [4, 5, 6]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [7, 8, 9]);
let mut buf = [1u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [0, 1, 1]);
}
#[tokio::test]
async fn buf_reader_buf() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
let mut buf = BytesMut::with_capacity(3);
read.read_buf(&mut buf).await.unwrap();
assert_eq!(&buf[..], [1, 2, 3]);
read.read_buf(&mut buf).await.unwrap();
assert_eq!(&buf[..], [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
}
#[tokio::test]
async fn buf_reader_extend_buf() {
let read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
futures_03_dep::pin_mut!(read);
assert_eq!(read.as_mut().extend_buf_tokio_02().await.unwrap(), 3);
assert_eq!(read.buffer(), [1, 2, 3]);
assert_eq!(read.as_mut().extend_buf_tokio_02().await.unwrap(), 7);
assert_eq!(read.buffer(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
}
}
#[cfg(test)]
#[cfg(feature = "tokio")]
mod tests_tokio_1 {
use super::{BufReader, Bufferless, CombineRead};
use std::{io, pin::Pin};
use {
bytes::BytesMut,
tokio_dep::{
self as tokio,
io::{AsyncRead, AsyncReadExt},
},
};
impl<R: AsyncRead> BufReader<R> {
async fn extend_buf_tokio(mut self: Pin<&mut Self>) -> io::Result<usize> {
crate::future_ext::poll_fn(|cx| Bufferless.poll_extend_buf(cx, self.as_mut())).await
}
}
#[tokio::test]
async fn buf_reader() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [1, 2, 3]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [4, 5, 6]);
let mut buf = [0u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [7, 8, 9]);
let mut buf = [1u8; 3];
read.read(&mut buf).await.unwrap();
assert_eq!(buf, [0, 1, 1]);
}
#[tokio::test]
async fn buf_reader_buf() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
let mut buf = BytesMut::with_capacity(3);
read.read_buf(&mut buf).await.unwrap();
assert_eq!(&buf[..], [1, 2, 3]);
read.read_buf(&mut buf).await.unwrap();
assert_eq!(&buf[..], [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
}
#[tokio::test]
async fn buf_reader_extend_buf() {
let read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
futures_03_dep::pin_mut!(read);
assert_eq!(read.as_mut().extend_buf_tokio().await.unwrap(), 3);
assert_eq!(read.buffer(), [1, 2, 3]);
assert_eq!(read.as_mut().extend_buf_tokio().await.unwrap(), 7);
assert_eq!(read.buffer(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
}
}
#[cfg(test)]
mod tests_sync {
use super::{BufReader, Bufferless, CombineSyncRead};
use std::io::Read;
#[test]
#[allow(clippy::unused_io_amount)]
fn buf_reader() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
let mut buf = [0u8; 3];
read.read(&mut buf).unwrap();
assert_eq!(buf, [1, 2, 3]);
let mut buf = [0u8; 3];
read.read(&mut buf).unwrap();
assert_eq!(buf, [4, 5, 6]);
let mut buf = [0u8; 3];
read.read(&mut buf).unwrap();
assert_eq!(buf, [7, 8, 9]);
let mut buf = [1u8; 3];
read.read(&mut buf).unwrap();
assert_eq!(buf, [0, 1, 1]);
}
#[test]
fn buf_reader_extend_buf() {
let mut read = BufReader::with_capacity(3, &[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 0][..]);
assert_eq!(Bufferless.extend_buf_sync(&mut read).unwrap(), 3);
assert_eq!(read.buffer(), [1, 2, 3]);
assert_eq!(Bufferless.extend_buf_sync(&mut read).unwrap(), 7);
assert_eq!(read.buffer(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
}
}

141
vendor/combine/src/stream/buffered.rs vendored Normal file
View File

@@ -0,0 +1,141 @@
use alloc::collections::VecDeque;
use crate::{
error::StreamError,
stream::{ParseError, Positioned, ResetStream, StreamErrorFor, StreamOnce},
};
/// `Stream` which buffers items from an instance of `StreamOnce` into a ring buffer.
/// Instances of `StreamOnce` which is not able to implement `ResetStream` (such as `ReadStream`) may
/// use this as a way to implement `ResetStream` and become a full `Stream` instance.
///
/// The drawback is that the buffer only stores a limited number of items which limits how many
/// tokens that can be reset and replayed. If a `buffered::Stream` is reset past this limit an error
/// will be returned when `uncons` is next called.
///
/// NOTE: If this stream is used in conjunction with an error enhancing stream such as
/// `easy::Stream` (also via the `easy_parser` method) it is recommended that the `buffered::Stream`
/// instance wraps the `easy::Stream` instance instead of the other way around.
///
/// ```ignore
/// // DO
/// buffered::Stream::new(easy::Stream(..), ..)
/// // DON'T
/// easy::Stream(buffered::Stream::new(.., ..))
/// parser.easy_parse(buffered::Stream::new(..));
/// ```
#[derive(Debug, PartialEq)]
pub struct Stream<Input>
where
Input: StreamOnce + Positioned,
{
offset: usize,
iter: Input,
buffer_offset: usize,
buffer: VecDeque<(Input::Token, Input::Position)>,
}
impl<Input> ResetStream for Stream<Input>
where
Input: Positioned,
{
type Checkpoint = usize;
fn checkpoint(&self) -> Self::Checkpoint {
self.offset
}
fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
if checkpoint < self.buffer_offset - self.buffer.len() {
// We have backtracked to far
Err(Self::Error::from_error(
self.position(),
StreamErrorFor::<Self>::message_static_message("Backtracked to far"),
))
} else {
self.offset = checkpoint;
Ok(())
}
}
}
impl<Input> Stream<Input>
where
Input: StreamOnce + Positioned,
Input::Position: Clone,
Input::Token: Clone,
{
/// Constructs a new `BufferedStream` from a `StreamOnce` instance with a `lookahead`
/// number of elements that can be stored in the buffer.
pub fn new(iter: Input, lookahead: usize) -> Stream<Input> {
Stream {
offset: 0,
iter,
buffer_offset: 0,
buffer: VecDeque::with_capacity(lookahead),
}
}
}
impl<Input> Positioned for Stream<Input>
where
Input: StreamOnce + Positioned,
{
#[inline]
fn position(&self) -> Self::Position {
if self.offset >= self.buffer_offset {
self.iter.position()
} else if self.offset < self.buffer_offset - self.buffer.len() {
self.buffer
.front()
.expect("At least 1 element in the buffer")
.1
.clone()
} else {
self.buffer[self.buffer.len() - (self.buffer_offset - self.offset)]
.1
.clone()
}
}
}
impl<Input> StreamOnce for Stream<Input>
where
Input: StreamOnce + Positioned,
Input::Token: Clone,
{
type Token = Input::Token;
type Range = Input::Range;
type Position = Input::Position;
type Error = Input::Error;
#[inline]
fn uncons(&mut self) -> Result<Input::Token, StreamErrorFor<Self>> {
if self.offset >= self.buffer_offset {
let position = self.iter.position();
let token = self.iter.uncons()?;
self.buffer_offset += 1;
// We want the VecDeque to only keep the last .capacity() elements so we need to remove
// an element if it gets to large
if self.buffer.len() == self.buffer.capacity() {
self.buffer.pop_front();
}
self.buffer.push_back((token.clone(), position));
self.offset += 1;
Ok(token)
} else if self.offset < self.buffer_offset - self.buffer.len() {
// We have backtracked to far
Err(StreamError::message_static_message("Backtracked to far"))
} else {
let value = self.buffer[self.buffer.len() - (self.buffer_offset - self.offset)]
.0
.clone();
self.offset += 1;
Ok(value)
}
}
fn is_partial(&self) -> bool {
self.iter.is_partial()
}
}

227
vendor/combine/src/stream/decoder.rs vendored Normal file
View File

@@ -0,0 +1,227 @@
use crate::{
error::ParseError,
stream::buf_reader::{Buffer, Bufferless, CombineBuffer},
};
use std::{
fmt,
io::{self, Read},
};
#[cfg(feature = "pin-project-lite")]
use std::pin::Pin;
#[derive(Debug)]
pub enum Error<E, P> {
Parse(E),
Io { position: P, error: io::Error },
}
impl<'a, P> From<Error<crate::easy::Errors<u8, &'a [u8], P>, P>>
for crate::easy::Errors<u8, &'a [u8], P>
where
P: Ord + Clone,
{
fn from(e: Error<crate::easy::Errors<u8, &'a [u8], P>, P>) -> Self {
match e {
Error::Parse(e) => e,
Error::Io { position, error } => {
crate::easy::Errors::from_error(position, crate::easy::Error::Other(error.into()))
}
}
}
}
impl<E, P> std::error::Error for Error<E, P>
where
E: std::error::Error,
P: fmt::Display + fmt::Debug,
{
}
impl<E: fmt::Display, P: fmt::Display> fmt::Display for Error<E, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Parse(e) => e.fmt(f),
Error::Io { position: _, error } => error.fmt(f),
}
}
}
#[derive(Default)]
/// Used together with the `decode!` macro
pub struct Decoder<S, P, C = Buffer> {
position: P,
state: S,
buffer: C,
end_of_input: bool,
}
impl<S, P> Decoder<S, P, Buffer>
where
P: Default,
S: Default,
{
/// Constructs a new [`Decoder`] with an internal buffer. Allows any `AsyncRead/Read` instance to
/// be used when decoding but there may be data left in the internal buffer after decoding
/// (accessible with [`Decoder::buffer`])
pub fn new() -> Self {
Decoder::default()
}
/// Constructs a new [`Decoder`] with an internal buffer. Allows any `AsyncRead/Read` instance to
/// be used when decoding but there may be data left in the internal buffer after decoding
/// (accessible with [`Decoder::buffer`])
pub fn new_buffer() -> Self {
Decoder::new()
}
}
impl<S, P> Decoder<S, P, Bufferless>
where
P: Default,
S: Default,
{
/// Constructs a new `Decoder` without an internal buffer. Requires the read instance to be
/// wrapped with combine's [`BufReader`] instance to
///
/// [`BufReader`]: super::buf_reader::BufReader
pub fn new_bufferless() -> Self {
Decoder::default()
}
}
impl<S, P> Decoder<S, P> {
pub fn buffer(&self) -> &[u8] {
&self.buffer.0
}
}
impl<S, P, C> Decoder<S, P, C> {
#[doc(hidden)]
pub fn advance<R>(&mut self, read: &mut R, removed: usize)
where
C: CombineBuffer<R>,
{
// Remove the data we have parsed and adjust `removed` to be the amount of data we
// committed from `self.reader`
self.buffer.advance(read, removed)
}
#[doc(hidden)]
#[cfg(feature = "pin-project-lite")]
pub fn advance_pin<R>(&mut self, read: Pin<&mut R>, removed: usize)
where
C: CombineBuffer<R>,
{
// Remove the data we have parsed and adjust `removed` to be the amount of data we
// committed from `self.reader`
self.buffer.advance_pin(read, removed);
}
pub fn position(&self) -> &P {
&self.position
}
#[doc(hidden)]
pub fn __inner(&mut self) -> (&mut S, &mut P, &C, bool) {
(
&mut self.state,
&mut self.position,
&self.buffer,
self.end_of_input,
)
}
}
impl<S, P, C> Decoder<S, P, C>
where
C: ,
{
#[doc(hidden)]
pub fn __before_parse<R>(&mut self, mut reader: R) -> io::Result<()>
where
R: Read,
C: crate::stream::buf_reader::CombineSyncRead<R>,
{
if self.buffer.extend_buf_sync(&mut reader)? == 0 {
self.end_of_input = true;
}
Ok(())
}
}
#[cfg(feature = "tokio-02")]
impl<S, P, C> Decoder<S, P, C> {
#[doc(hidden)]
pub async fn __before_parse_tokio_02<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
where
R: tokio_02_dep::io::AsyncRead,
C: crate::stream::buf_reader::CombineRead<R, dyn tokio_02_dep::io::AsyncRead>,
{
let copied =
crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
.await?;
if copied == 0 {
self.end_of_input = true;
}
Ok(())
}
}
#[cfg(feature = "tokio-03")]
impl<S, P, C> Decoder<S, P, C> {
#[doc(hidden)]
pub async fn __before_parse_tokio_03<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
where
R: tokio_03_dep::io::AsyncRead,
C: crate::stream::buf_reader::CombineRead<R, dyn tokio_03_dep::io::AsyncRead>,
{
let copied =
crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
.await?;
if copied == 0 {
self.end_of_input = true;
}
Ok(())
}
}
#[cfg(feature = "tokio")]
impl<S, P, C> Decoder<S, P, C> {
#[doc(hidden)]
pub async fn __before_parse_tokio<R>(&mut self, mut reader: Pin<&mut R>) -> io::Result<()>
where
R: tokio_dep::io::AsyncRead,
C: crate::stream::buf_reader::CombineRead<R, dyn tokio_dep::io::AsyncRead>,
{
let copied =
crate::future_ext::poll_fn(|cx| self.buffer.poll_extend_buf(cx, reader.as_mut()))
.await?;
if copied == 0 {
self.end_of_input = true;
}
Ok(())
}
}
#[cfg(feature = "futures-03")]
impl<S, P, C> Decoder<S, P, C> {
#[doc(hidden)]
pub async fn __before_parse_async<R>(&mut self, reader: Pin<&mut R>) -> io::Result<()>
where
R: futures_io_03::AsyncRead,
C: crate::stream::buf_reader::CombineAsyncRead<R>,
{
let copied = self.buffer.extend_buf(reader).await?;
if copied == 0 {
self.end_of_input = true;
}
Ok(())
}
}

897
vendor/combine/src/stream/easy.rs vendored Normal file
View File

@@ -0,0 +1,897 @@
//! Stream wrapper which provides an informative and easy to use error type.
//!
//! Unless you have specific constraints preventing you from using this error type (such as being
//! a `no_std` environment) you probably want to use this stream type. It can easily be used
//! through the [`EasyParser::easy_parse`] method.
//!
//! The provided `Errors` type is roughly the same as `ParseError` in combine 1.x and 2.x.
//!
//! ```
//! #[macro_use]
//! extern crate combine;
//! use combine::{easy, Parser, EasyParser, Stream, many1};
//! use combine::parser::char::letter;
//! use combine::stream::StreamErrorFor;
//! use combine::error::{ParseError, StreamError};
//!
//! fn main() {
//! parser!{
//! fn parser[Input]()(Input) -> String
//! where [
//! Input: Stream<Token = char, Error = easy::ParseError<Input>>,
//! Input::Range: PartialEq,
//! // If we want to use the error type explicitly we need to help rustc infer
//! // `StreamError` to `easy::Error` (rust-lang/rust#24159)
//! Input::Error: ParseError<
//! Input::Token,
//! Input::Range,
//! Input::Position,
//! StreamError = easy::Error<Input::Token, Input::Range>
//! >
//! ]
//! {
//! many1(letter()).and_then(|word: String| {
//! if word == "combine" {
//! Ok(word)
//! } else {
//! Err(easy::Error::Expected(easy::Info::Static("combine")))
//! }
//! })
//! }
//! }
//!
//! parser!{
//! fn parser2[Input]()(Input) -> String
//! where [
//! Input: Stream<Token = char>,
//! ]
//! {
//! many1(letter()).and_then(|word: String| {
//! if word == "combine" {
//! Ok(word)
//! } else {
//! // Alternatively it is possible to only use the methods provided by the
//! // `StreamError` trait.
//! // In that case the extra bound is not necessary (and this method will work
//! // for other errors than `easy::Errors`)
//! Err(StreamErrorFor::<Input>::expected_static_message("combine"))
//! }
//! })
//! }
//! }
//!
//! let input = "combin";
//! let expected_error = Err(easy::Errors {
//! errors: vec![
//! easy::Error::Expected("combine".into())
//! ],
//! position: 0,
//! });
//! assert_eq!(
//! parser().easy_parse(input).map_err(|err| err.map_position(|p| p.translate_position(input))),
//! expected_error
//! );
//! assert_eq!(
//! parser2().easy_parse(input).map_err(|err| err.map_position(|p| p.translate_position(input))),
//! expected_error
//! );
//! }
//!
//! ```
//!
//! [`EasyParser::easy_parse`]: super::super::parser::EasyParser::easy_parse
use std::{error::Error as StdError, fmt};
use crate::error::{Info as PrimitiveInfo, ParseResult, StreamError, Tracked};
use crate::stream::{
Positioned, RangeStream, RangeStreamOnce, ResetStream, StreamErrorFor, StreamOnce,
};
/// Enum holding error information. Variants are defined for `Stream::Token` and `Stream::Range` as
/// well as string variants holding easy descriptions.
///
/// As there is implementations of `From` for `String` and `&'static str` the
/// constructor need not be used directly as calling `msg.into()` should turn a message into the
/// correct `Info` variant.
#[derive(Clone, Debug)]
pub enum Info<T, R> {
Token(T),
Range(R),
Owned(String),
Static(&'static str),
}
impl<T, R, F> From<PrimitiveInfo<T, R, F>> for Info<T, R>
where
F: fmt::Display,
{
fn from(info: PrimitiveInfo<T, R, F>) -> Self {
match info {
PrimitiveInfo::Token(b) => Info::Token(b),
PrimitiveInfo::Range(b) => Info::Range(b),
PrimitiveInfo::Static(b) => Info::Static(b),
PrimitiveInfo::Format(b) => Info::Owned(b.to_string()),
}
}
}
impl<T, R> Info<T, R> {
pub fn map_token<F, U>(self, f: F) -> Info<U, R>
where
F: FnOnce(T) -> U,
{
use self::Info::*;
match self {
Token(t) => Token(f(t)),
Range(r) => Range(r),
Owned(s) => Owned(s),
Static(x) => Static(x),
}
}
pub fn map_range<F, S>(self, f: F) -> Info<T, S>
where
F: FnOnce(R) -> S,
{
use self::Info::*;
match self {
Token(t) => Token(t),
Range(r) => Range(f(r)),
Owned(s) => Owned(s),
Static(x) => Static(x),
}
}
}
impl<T: PartialEq, R: PartialEq> PartialEq for Info<T, R> {
fn eq(&self, other: &Info<T, R>) -> bool {
match (self, other) {
(&Info::Token(ref l), &Info::Token(ref r)) => l == r,
(&Info::Range(ref l), &Info::Range(ref r)) => l == r,
(&Info::Owned(ref l), &Info::Owned(ref r)) => l == r,
(&Info::Static(l), &Info::Owned(ref r)) => l == r,
(&Info::Owned(ref l), &Info::Static(r)) => l == r,
(&Info::Static(l), &Info::Static(r)) => l == r,
_ => false,
}
}
}
impl<T: fmt::Display, R: fmt::Display> fmt::Display for Info<T, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Info::Token(ref c) => write!(f, "`{}`", c),
Info::Range(ref c) => write!(f, "`{}`", c),
Info::Owned(ref s) => write!(f, "{}", s),
Info::Static(s) => write!(f, "{}", s),
}
}
}
impl<R> From<char> for Info<char, R> {
fn from(s: char) -> Info<char, R> {
Info::Token(s)
}
}
impl<T, R> From<String> for Info<T, R> {
fn from(s: String) -> Info<T, R> {
Info::Owned(s)
}
}
impl<T, R> From<&'static str> for Info<T, R> {
fn from(s: &'static str) -> Info<T, R> {
Info::Static(s)
}
}
impl<R> From<u8> for Info<u8, R> {
fn from(s: u8) -> Info<u8, R> {
Info::Token(s)
}
}
/// Enum used to store information about an error that has occurred during parsing.
#[derive(Debug)]
pub enum Error<T, R> {
/// Error indicating an unexpected token has been encountered in the stream
Unexpected(Info<T, R>),
/// Error indicating that the parser expected something else
Expected(Info<T, R>),
/// Generic message
Message(Info<T, R>),
/// Variant for containing other types of errors
Other(Box<dyn StdError + Send + Sync>),
}
impl<Item, Range> StreamError<Item, Range> for Error<Item, Range>
where
Item: PartialEq,
Range: PartialEq,
{
#[inline]
fn unexpected_token(token: Item) -> Self {
Error::Unexpected(Info::Token(token))
}
#[inline]
fn unexpected_range(token: Range) -> Self {
Error::Unexpected(Info::Range(token))
}
#[inline]
fn unexpected_format<T>(msg: T) -> Self
where
T: fmt::Display,
{
Error::Unexpected(Info::Owned(msg.to_string()))
}
#[inline]
fn unexpected_static_message(msg: &'static str) -> Self {
Error::Unexpected(Info::Static(msg))
}
#[inline]
fn expected_token(token: Item) -> Self {
Error::Expected(Info::Token(token))
}
#[inline]
fn expected_range(token: Range) -> Self {
Error::Expected(Info::Range(token))
}
#[inline]
fn expected_format<T>(msg: T) -> Self
where
T: fmt::Display,
{
Error::Expected(Info::Owned(msg.to_string()))
}
#[inline]
fn expected_static_message(msg: &'static str) -> Self {
Error::Expected(Info::Static(msg))
}
#[inline]
fn message_format<T>(msg: T) -> Self
where
T: fmt::Display,
{
Error::Message(Info::Owned(msg.to_string()))
}
#[inline]
fn message_static_message(msg: &'static str) -> Self {
Error::Message(Info::Static(msg))
}
#[inline]
fn message_token(token: Item) -> Self {
Error::Message(Info::Token(token))
}
#[inline]
fn message_range(token: Range) -> Self {
Error::Message(Info::Range(token))
}
fn is_unexpected_end_of_input(&self) -> bool {
*self == Self::end_of_input()
}
#[inline]
fn other<E>(err: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
err.into()
}
#[inline]
fn into_other<T>(self) -> T
where
T: StreamError<Item, Range>,
{
match self {
Error::Unexpected(info) => match info {
Info::Token(x) => T::unexpected_token(x),
Info::Range(x) => T::unexpected_range(x),
Info::Static(x) => T::unexpected_static_message(x),
Info::Owned(x) => T::unexpected_format(x),
},
Error::Expected(info) => match info {
Info::Token(x) => T::expected_token(x),
Info::Range(x) => T::expected_range(x),
Info::Static(x) => T::expected_static_message(x),
Info::Owned(x) => T::expected_format(x),
},
Error::Message(info) => match info {
Info::Token(x) => T::expected_token(x),
Info::Range(x) => T::expected_range(x),
Info::Static(x) => T::expected_static_message(x),
Info::Owned(x) => T::expected_format(x),
},
Error::Other(err) => T::message_format(err),
}
}
}
impl<Item, Range, Position> crate::error::ParseError<Item, Range, Position> for Error<Item, Range>
where
Item: PartialEq,
Range: PartialEq,
Position: Default,
{
type StreamError = Self;
#[inline]
fn empty(_: Position) -> Self {
Self::message_static_message("")
}
#[inline]
fn from_error(_: Position, err: Self::StreamError) -> Self {
err
}
#[inline]
fn position(&self) -> Position {
Position::default()
}
#[inline]
fn set_position(&mut self, _position: Position) {}
#[inline]
fn add(&mut self, err: Self::StreamError) {
*self = err;
}
#[inline]
fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
where
F: FnOnce(&mut Tracked<Self>),
{
f(self_);
self_.error = info;
}
fn is_unexpected_end_of_input(&self) -> bool {
*self == Self::end_of_input()
}
#[inline]
fn into_other<T>(self) -> T
where
T: crate::error::ParseError<Item, Range, Position>,
{
T::from_error(Position::default(), StreamError::into_other(self))
}
}
impl<Item, Range, Position> crate::error::ParseErrorInto<Item, Range, Position>
for Errors<Item, Range, Position>
{
fn into_other_error<T, Item2, Range2, Position2>(self) -> T
where
T: crate::error::ParseError<Item2, Range2, Position2>,
Item2: From<Item>,
Range2: From<Range>,
Position2: From<Position>,
{
let mut error = T::empty(self.position.into());
for err in self.errors {
error.add(crate::error::StreamErrorInto::<Item, Range>::into_other_error(err));
}
error
}
}
impl<Item, Range> crate::error::StreamErrorInto<Item, Range> for Error<Item, Range> {
fn into_other_error<T, Item2, Range2>(self) -> T
where
T: crate::error::StreamError<Item2, Range2>,
Item2: From<Item>,
Range2: From<Range>,
{
match self {
Error::Unexpected(info) => match info {
Info::Token(x) => T::unexpected_token(x.into()),
Info::Range(x) => T::unexpected_range(x.into()),
Info::Static(x) => T::unexpected_static_message(x),
Info::Owned(x) => T::unexpected_format(x),
},
Error::Expected(info) => match info {
Info::Token(x) => T::expected_token(x.into()),
Info::Range(x) => T::expected_range(x.into()),
Info::Static(x) => T::expected_static_message(x),
Info::Owned(x) => T::expected_format(x),
},
Error::Message(info) => match info {
Info::Token(x) => T::expected_token(x.into()),
Info::Range(x) => T::expected_range(x.into()),
Info::Static(x) => T::expected_static_message(x),
Info::Owned(x) => T::expected_format(x),
},
Error::Other(err) => T::message_format(err),
}
}
}
impl<Item, Range, Position> crate::error::ParseError<Item, Range, Position>
for Errors<Item, Range, Position>
where
Item: PartialEq,
Range: PartialEq,
Position: Ord + Clone,
{
type StreamError = Error<Item, Range>;
#[inline]
fn empty(pos: Position) -> Self {
Errors::empty(pos)
}
#[inline]
fn from_error(position: Position, err: Self::StreamError) -> Self {
Self::new(position, err)
}
#[inline]
fn position(&self) -> Position {
self.position.clone()
}
#[inline]
fn set_position(&mut self, position: Position) {
self.position = position;
}
#[inline]
fn merge(self, other: Self) -> Self {
Errors::merge(self, other)
}
#[inline]
fn add(&mut self, err: Self::StreamError) {
self.add_error(err);
}
#[inline]
fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
where
F: FnOnce(&mut Tracked<Self>),
{
let start = self_.error.errors.len();
f(self_);
// Replace all expected errors that were added from the previous add_error
// with this expected error
let mut i = 0;
self_.error.errors.retain(|e| {
if i < start {
i += 1;
true
} else {
match *e {
Error::Expected(_) => false,
_ => true,
}
}
});
self_.error.add(info);
}
fn clear_expected(&mut self) {
self.errors.retain(|e| match *e {
Error::Expected(_) => false,
_ => true,
})
}
fn is_unexpected_end_of_input(&self) -> bool {
self.errors
.iter()
.any(StreamError::is_unexpected_end_of_input)
}
#[inline]
fn into_other<T>(mut self) -> T
where
T: crate::error::ParseError<Item, Range, Position>,
{
match self.errors.pop() {
Some(err) => T::from_error(self.position, StreamError::into_other(err)),
None => T::empty(self.position),
}
}
}
impl<T, R> Error<T, R> {
pub fn map_token<F, U>(self, f: F) -> Error<U, R>
where
F: FnOnce(T) -> U,
{
use self::Error::*;
match self {
Unexpected(x) => Unexpected(x.map_token(f)),
Expected(x) => Expected(x.map_token(f)),
Message(x) => Message(x.map_token(f)),
Other(x) => Other(x),
}
}
pub fn map_range<F, S>(self, f: F) -> Error<T, S>
where
F: FnOnce(R) -> S,
{
use self::Error::*;
match self {
Unexpected(x) => Unexpected(x.map_range(f)),
Expected(x) => Expected(x.map_range(f)),
Message(x) => Message(x.map_range(f)),
Other(x) => Other(x),
}
}
}
impl<T: PartialEq, R: PartialEq> PartialEq for Error<T, R> {
fn eq(&self, other: &Error<T, R>) -> bool {
match (self, other) {
(&Error::Unexpected(ref l), &Error::Unexpected(ref r))
| (&Error::Expected(ref l), &Error::Expected(ref r))
| (&Error::Message(ref l), &Error::Message(ref r)) => l == r,
_ => false,
}
}
}
impl<T, R, E> From<E> for Error<T, R>
where
E: StdError + 'static + Send + Sync,
{
fn from(e: E) -> Error<T, R> {
Error::Other(Box::new(e))
}
}
impl<T, R> Error<T, R> {
/// Returns the `end_of_input` error.
pub fn end_of_input() -> Error<T, R> {
Error::Unexpected("end of input".into())
}
/// Formats a slice of errors in a human readable way.
///
/// ```rust
/// # extern crate combine;
/// # use combine::*;
/// # use combine::parser::char::*;
/// # use combine::stream::position::{self, SourcePosition};
///
/// # fn main() {
/// let input = r"
/// ,123
/// ";
/// let result = spaces().silent().with(char('.').or(char('a')).or(digit()))
/// .easy_parse(position::Stream::new(input));
/// let m = format!("{}", result.unwrap_err());
/// let expected = r"Parse error at line: 2, column: 3
/// Unexpected `,`
/// Expected `.`, `a` or digit
/// ";
/// assert_eq!(m, expected);
/// # }
/// ```
pub fn fmt_errors(errors: &[Error<T, R>], f: &mut fmt::Formatter<'_>) -> fmt::Result
where
T: fmt::Display,
R: fmt::Display,
{
// First print the token that we did not expect
// There should really just be one unexpected message at this point though we print them
// all to be safe
let unexpected = errors.iter().filter(|e| match **e {
Error::Unexpected(_) => true,
_ => false,
});
for error in unexpected {
writeln!(f, "{}", error)?;
}
// Then we print out all the things that were expected in a comma separated list
// 'Expected 'a', 'expression' or 'let'
let iter = || {
errors.iter().filter_map(|e| match *e {
Error::Expected(ref err) => Some(err),
_ => None,
})
};
let expected_count = iter().count();
for (i, message) in iter().enumerate() {
let s = match i {
0 => "Expected",
_ if i < expected_count - 1 => ",",
// Last expected message to be written
_ => " or",
};
write!(f, "{} {}", s, message)?;
}
if expected_count != 0 {
writeln!(f)?;
}
// If there are any generic messages we print them out last
let messages = errors.iter().filter(|e| match **e {
Error::Message(_) | Error::Other(_) => true,
_ => false,
});
for error in messages {
writeln!(f, "{}", error)?;
}
Ok(())
}
}
/// Convenience alias over `Errors` for `StreamOnce` types which makes it possible to specify the
/// `Errors` type from a `StreamOnce` by writing `ParseError<Input>` instead of `Errors<Input::Token,
/// Input::Range, Input::Position>`
pub type ParseError<S> =
Errors<<S as StreamOnce>::Token, <S as StreamOnce>::Range, <S as StreamOnce>::Position>;
/// Struct which hold information about an error that occurred at a specific position.
/// Can hold multiple instances of `Error` if more that one error occurred in the same position.
#[derive(Debug, PartialEq)]
pub struct Errors<T, R, P> {
/// The position where the error occurred
pub position: P,
/// A vector containing specific information on what errors occurred at `position`. Usually
/// a fully formed message contains one `Unexpected` error and one or more `Expected` errors.
/// `Message` and `Other` may also appear (`combine` never generates these errors on its own)
/// and may warrant custom handling.
pub errors: Vec<Error<T, R>>,
}
impl<T, R, P> Errors<T, R, P> {
/// Constructs a new `ParseError` which occurred at `position`.
#[inline]
pub fn new(position: P, error: Error<T, R>) -> Errors<T, R, P> {
Self::from_errors(position, vec![error])
}
/// Constructs an error with no other information than the position it occurred at.
#[inline]
pub fn empty(position: P) -> Errors<T, R, P> {
Self::from_errors(position, vec![])
}
/// Constructs a `ParseError` with multiple causes.
#[inline]
pub fn from_errors(position: P, errors: Vec<Error<T, R>>) -> Errors<T, R, P> {
Errors { position, errors }
}
/// Constructs an end of input error. Should be returned by parsers which encounter end of
/// input unexpectedly.
#[inline]
pub fn end_of_input(position: P) -> Errors<T, R, P> {
Self::new(position, Error::end_of_input())
}
/// Adds an error if `error` does not exist in this `ParseError` already (as determined byte
/// `PartialEq`).
pub fn add_error(&mut self, error: Error<T, R>)
where
T: PartialEq,
R: PartialEq,
{
// Don't add duplicate errors
if self.errors.iter().all(|err| *err != error) {
self.errors.push(error);
}
}
/// Removes all `Expected` errors in `self` and adds `info` instead.
pub fn set_expected(&mut self, info: Info<T, R>) {
// Remove all other expected messages
self.errors.retain(|e| match *e {
Error::Expected(_) => false,
_ => true,
});
self.errors.push(Error::Expected(info));
}
/// Merges two `ParseError`s. If they exist at the same position the errors of `other` are
/// added to `self` (using `add_error` to skip duplicates). If they are not at the same
/// position the error furthest ahead are returned, ignoring the other `ParseError`.
pub fn merge(mut self, mut other: Errors<T, R, P>) -> Errors<T, R, P>
where
P: Ord,
T: PartialEq,
R: PartialEq,
{
use std::cmp::Ordering;
// Only keep the errors which occurred after consuming the most amount of data
match self.position.cmp(&other.position) {
Ordering::Less => other,
Ordering::Greater => self,
Ordering::Equal => {
for message in other.errors.drain(..) {
self.add_error(message);
}
self
}
}
}
/// Maps the position to a new value
pub fn map_position<F, Q>(self, f: F) -> Errors<T, R, Q>
where
F: FnOnce(P) -> Q,
{
Errors::from_errors(f(self.position), self.errors)
}
/// Maps all token variants to a new value
pub fn map_token<F, U>(self, mut f: F) -> Errors<U, R, P>
where
F: FnMut(T) -> U,
{
Errors::from_errors(
self.position,
self.errors
.into_iter()
.map(|error| error.map_token(&mut f))
.collect(),
)
}
/// Maps all range variants to a new value.
///
/// ```
/// use combine::*;
/// use combine::parser::range::range;
/// println!(
/// "{}",
/// range(&"HTTP"[..])
/// .easy_parse("HTT")
/// .unwrap_err()
/// .map_range(|bytes| format!("{:?}", bytes))
/// );
/// ```
pub fn map_range<F, S>(self, mut f: F) -> Errors<T, S, P>
where
F: FnMut(R) -> S,
{
Errors::from_errors(
self.position,
self.errors
.into_iter()
.map(|error| error.map_range(&mut f))
.collect(),
)
}
}
impl<T, R, P> StdError for Errors<T, R, P>
where
P: fmt::Display + fmt::Debug,
T: fmt::Display + fmt::Debug,
R: fmt::Display + fmt::Debug,
{
fn description(&self) -> &str {
"parse error"
}
}
impl<T, R, P> fmt::Display for Errors<T, R, P>
where
P: fmt::Display,
T: fmt::Display,
R: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Parse error at {}", self.position)?;
Error::fmt_errors(&self.errors, f)
}
}
impl<T: fmt::Display, R: fmt::Display> fmt::Display for Error<T, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::Unexpected(ref c) => write!(f, "Unexpected {}", c),
Error::Expected(ref s) => write!(f, "Expected {}", s),
Error::Message(ref msg) => msg.fmt(f),
Error::Other(ref err) => err.fmt(f),
}
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Stream<S>(pub S);
impl<S> From<S> for Stream<S> {
fn from(stream: S) -> Self {
Stream(stream)
}
}
impl<S> ResetStream for Stream<S>
where
S: ResetStream + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
{
type Checkpoint = S::Checkpoint;
fn checkpoint(&self) -> Self::Checkpoint {
self.0.checkpoint()
}
fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
self.0
.reset(checkpoint)
.map_err(crate::error::ParseError::into_other)
}
}
impl<S> StreamOnce for Stream<S>
where
S: StreamOnce + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
{
type Token = S::Token;
type Range = S::Range;
type Position = S::Position;
type Error = ParseError<S>;
#[inline]
fn uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>> {
self.0.uncons().map_err(StreamError::into_other)
}
fn is_partial(&self) -> bool {
self.0.is_partial()
}
}
impl<S> RangeStreamOnce for Stream<S>
where
S: RangeStream,
S::Token: PartialEq,
S::Range: PartialEq,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> {
self.0.uncons_range(size).map_err(StreamError::into_other)
}
#[inline]
fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.0.uncons_while(f).map_err(StreamError::into_other)
}
#[inline]
fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.0.uncons_while1(f).map_err(StreamError::into_other)
}
#[inline]
fn distance(&self, end: &Self::Checkpoint) -> usize {
self.0.distance(end)
}
fn range(&self) -> Self::Range {
self.0.range()
}
}
impl<S> Positioned for Stream<S>
where
S: StreamOnce + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
{
fn position(&self) -> S::Position {
self.0.position()
}
}

1896
vendor/combine/src/stream/mod.rs vendored Normal file

File diff suppressed because it is too large Load Diff

465
vendor/combine/src/stream/position.rs vendored Normal file
View File

@@ -0,0 +1,465 @@
use crate::{
error::{ParseError, ParseResult, StreamError},
lib::fmt,
stream::{
IteratorStream, Positioned, RangeStreamOnce, ResetStream, SliceStream, StreamErrorFor,
StreamOnce,
},
};
#[cfg(feature = "std")]
use crate::stream::read;
/// Trait for tracking the current position of a `Stream`.
pub trait Positioner<Item> {
/// The type which keeps track of the position
type Position: Clone + Ord;
type Checkpoint: Clone;
/// Returns the current position
fn position(&self) -> Self::Position;
/// Updates the position given that `token` has been taken from the stream
fn update(&mut self, token: &Item);
fn checkpoint(&self) -> Self::Checkpoint;
fn reset(&mut self, checkpoint: Self::Checkpoint);
}
/// Trait for tracking the current position of a `RangeStream`.
pub trait RangePositioner<Item, Range>: Positioner<Item> {
/// Updates the position given that `range` has been taken from the stream
fn update_range(&mut self, range: &Range);
}
/// Defines a default `Positioner` type for a particular `Stream` type.
pub trait DefaultPositioned {
type Positioner: Default;
}
impl<'a> DefaultPositioned for &'a str {
type Positioner = SourcePosition;
}
impl<'a, T> DefaultPositioned for &'a [T] {
type Positioner = IndexPositioner;
}
impl<'a, T> DefaultPositioned for SliceStream<'a, T> {
type Positioner = IndexPositioner;
}
impl<T> DefaultPositioned for IteratorStream<T> {
type Positioner = IndexPositioner;
}
#[cfg(feature = "std")]
impl<R> DefaultPositioned for read::Stream<R> {
type Positioner = IndexPositioner;
}
/// The `Stream<Input>` struct maintains the current position in the stream `Input` using
/// the `Positioner` trait to track the position.
///
/// ```
/// # #![cfg(feature = "std")]
/// # extern crate combine;
/// # use combine::*;
/// # use combine::stream::easy;
/// # use combine::stream::position;
/// # fn main() {
/// let result = token(b'9')
/// .message("Not a nine")
/// .easy_parse(position::Stream::new(&b"8"[..]));
/// assert_eq!(result, Err(easy::Errors {
/// position: 0,
/// errors: vec![
/// easy::Error::Unexpected(b'8'.into()),
/// easy::Error::Expected(b'9'.into()),
/// easy::Error::Message("Not a nine".into())
/// ]
/// }));
/// # }
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Stream<Input, X> {
/// The input stream used when items are requested
pub input: Input,
/// The positioner used to update the current position
pub positioner: X,
}
impl<Input, X> Stream<Input, X>
where
Input: StreamOnce,
X: Positioner<Input::Token>,
{
/// Creates a new `Stream<Input, X>` from an input stream and a positioner.
pub fn with_positioner(input: Input, positioner: X) -> Stream<Input, X> {
Stream { input, positioner }
}
}
impl<Input> Stream<Input, Input::Positioner>
where
Input: StreamOnce + DefaultPositioned,
Input::Positioner: Positioner<Input::Token>,
{
/// Creates a new `Stream<Input, X>` from an input stream and its default positioner.
pub fn new(input: Input) -> Stream<Input, Input::Positioner> {
Stream::with_positioner(input, Input::Positioner::default())
}
}
impl<Input, X, S> Positioned for Stream<Input, X>
where
Input: StreamOnce,
X: Positioner<Input::Token>,
S: StreamError<Input::Token, Input::Range>,
Input::Error: ParseError<Input::Token, Input::Range, X::Position, StreamError = S>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position, StreamError = S>,
{
#[inline]
fn position(&self) -> Self::Position {
self.positioner.position()
}
}
impl<Input, X, S> StreamOnce for Stream<Input, X>
where
Input: StreamOnce,
X: Positioner<Input::Token>,
S: StreamError<Input::Token, Input::Range>,
Input::Error: ParseError<Input::Token, Input::Range, X::Position, StreamError = S>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position, StreamError = S>,
{
type Token = Input::Token;
type Range = Input::Range;
type Position = X::Position;
type Error = Input::Error;
#[inline]
fn uncons(&mut self) -> Result<Input::Token, StreamErrorFor<Self>> {
self.input.uncons().map(|c| {
self.positioner.update(&c);
c
})
}
fn is_partial(&self) -> bool {
self.input.is_partial()
}
}
impl<Item, T> Positioner<Item> for &'_ mut T
where
Item: Clone,
T: ?Sized + Positioner<Item>,
{
type Position = T::Position;
type Checkpoint = T::Checkpoint;
#[inline]
fn position(&self) -> T::Position {
(**self).position()
}
#[inline]
fn update(&mut self, item: &Item) {
(**self).update(item)
}
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
(**self).checkpoint()
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) {
(**self).reset(checkpoint)
}
}
impl<Item, Range, T> RangePositioner<Item, Range> for &'_ mut T
where
Item: Clone,
Range: Clone + crate::stream::Range,
T: ?Sized + RangePositioner<Item, Range>,
{
fn update_range(&mut self, range: &Range) {
(**self).update_range(range);
}
}
/// The `IndexPositioner<Item, Range>` struct maintains the current index into the stream `Input`. The
/// initial index is index 0. Each `Item` committed increments the index by 1; each `range` committed
/// increments the position by `range.len()`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct IndexPositioner(usize);
impl<Item> Positioner<Item> for IndexPositioner
where
Item: Clone,
{
type Position = usize;
type Checkpoint = Self;
#[inline]
fn position(&self) -> usize {
self.0
}
#[inline]
fn update(&mut self, _item: &Item) {
self.0 += 1
}
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
self.clone()
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) {
*self = checkpoint;
}
}
impl IndexPositioner {
pub fn new() -> IndexPositioner {
IndexPositioner::new_with_position(0)
}
pub fn new_with_position(position: usize) -> IndexPositioner {
IndexPositioner(position)
}
}
impl<Item, Range> RangePositioner<Item, Range> for IndexPositioner
where
Item: Clone,
Range: Clone + crate::stream::Range,
{
fn update_range(&mut self, range: &Range) {
self.0 += range.len()
}
}
/// Struct which represents a position in a source file.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct SourcePosition {
/// Current line of the input
pub line: i32,
/// Current column of the input
pub column: i32,
}
impl Default for SourcePosition {
fn default() -> Self {
SourcePosition { line: 1, column: 1 }
}
}
impl fmt::Display for SourcePosition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "line: {}, column: {}", self.line, self.column)
}
}
impl SourcePosition {
pub fn new() -> Self {
SourcePosition::default()
}
}
impl Positioner<char> for SourcePosition {
type Position = SourcePosition;
type Checkpoint = Self;
#[inline]
fn position(&self) -> SourcePosition {
*self
}
#[inline]
fn update(&mut self, token: &char) {
self.column += 1;
if *token == '\n' {
self.column = 1;
self.line += 1;
}
}
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
*self
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) {
*self = checkpoint;
}
}
impl Positioner<u8> for SourcePosition {
type Position = SourcePosition;
type Checkpoint = Self;
#[inline]
fn position(&self) -> SourcePosition {
*self
}
#[inline]
fn update(&mut self, token: &u8) {
self.column += 1;
if *token == b'\n' {
self.column = 1;
self.line += 1;
}
}
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
*self
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) {
*self = checkpoint;
}
}
impl<'a> RangePositioner<char, &'a str> for SourcePosition {
fn update_range(&mut self, range: &&'a str) {
for c in range.chars() {
self.update(&c);
}
}
}
impl<Input, X, S> RangeStreamOnce for Stream<Input, X>
where
Input: RangeStreamOnce,
X: RangePositioner<Input::Token, Input::Range>,
S: StreamError<Input::Token, Input::Range>,
Input::Error: ParseError<Input::Token, Input::Range, X::Position, StreamError = S>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position, StreamError = S>,
Input::Position: Clone + Ord,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<Input::Range, StreamErrorFor<Self>> {
self.input.uncons_range(size).map(|range| {
self.positioner.update_range(&range);
range
})
}
#[inline]
fn uncons_while<F>(&mut self, mut predicate: F) -> Result<Input::Range, StreamErrorFor<Self>>
where
F: FnMut(Input::Token) -> bool,
{
let positioner = &mut self.positioner;
self.input.uncons_while(|t| {
if predicate(t.clone()) {
positioner.update(&t);
true
} else {
false
}
})
}
#[inline]
fn uncons_while1<F>(
&mut self,
mut predicate: F,
) -> ParseResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
let positioner = &mut self.positioner;
self.input.uncons_while1(|t| {
if predicate(t.clone()) {
positioner.update(&t);
true
} else {
false
}
})
}
#[inline]
fn distance(&self, end: &Self::Checkpoint) -> usize {
self.input.distance(&end.input)
}
fn range(&self) -> Self::Range {
self.input.range()
}
}
impl<Input, X, S> ResetStream for Stream<Input, X>
where
Input: ResetStream,
X: Positioner<Input::Token>,
S: StreamError<Input::Token, Input::Range>,
Input::Error: ParseError<Input::Token, Input::Range, X::Position, StreamError = S>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position, StreamError = S>,
{
type Checkpoint = Stream<Input::Checkpoint, X::Checkpoint>;
fn checkpoint(&self) -> Self::Checkpoint {
Stream {
input: self.input.checkpoint(),
positioner: self.positioner.checkpoint(),
}
}
fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
self.input.reset(checkpoint.input)?;
self.positioner.reset(checkpoint.positioner);
Ok(())
}
}
#[cfg(all(feature = "std", test))]
mod tests {
use crate::Parser;
use super::*;
#[test]
fn test_positioner() {
let input = ["a".to_string(), "b".to_string()];
let mut parser = crate::any();
let result = parser.parse(Stream::new(&input[..]));
assert_eq!(
result,
Ok((
"a".to_string(),
Stream::with_positioner(
&["b".to_string()][..],
IndexPositioner::new_with_position(1)
)
))
);
}
#[test]
fn test_range_positioner() {
let input = ["a".to_string(), "b".to_string(), "c".to_string()];
let mut parser = crate::parser::range::take(2);
let result = parser.parse(Stream::new(&input[..]));
assert_eq!(
result,
Ok((
&["a".to_string(), "b".to_string()][..],
Stream::with_positioner(
&["c".to_string()][..],
IndexPositioner::new_with_position(2)
)
))
);
}
}

210
vendor/combine/src/stream/read.rs vendored Normal file
View File

@@ -0,0 +1,210 @@
use std::{
fmt,
io::{self, Bytes, Read},
};
use crate::{
error::{ParseError, StreamError, Tracked},
stream::{StreamErrorFor, StreamOnce},
};
#[derive(Debug)]
pub enum Error {
Unexpected,
EndOfInput,
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Unexpected => write!(f, "unexpected parse"),
Error::EndOfInput => write!(f, "unexpected end of input"),
Error::Io(err) => write!(f, "{}", err),
}
}
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Error::Unexpected, Error::Unexpected) => true,
(Error::EndOfInput, Error::EndOfInput) => true,
_ => false,
}
}
}
impl<Item, Range> StreamError<Item, Range> for Error {
#[inline]
fn unexpected_token(_: Item) -> Self {
Error::Unexpected
}
#[inline]
fn unexpected_range(_: Range) -> Self {
Error::Unexpected
}
#[inline]
fn unexpected_format<T>(_: T) -> Self
where
T: fmt::Display,
{
Error::Unexpected
}
#[inline]
fn expected_token(_: Item) -> Self {
Error::Unexpected
}
#[inline]
fn expected_range(_: Range) -> Self {
Error::Unexpected
}
#[inline]
fn expected_format<T>(_: T) -> Self
where
T: fmt::Display,
{
Error::Unexpected
}
#[inline]
fn message_format<T>(_: T) -> Self
where
T: fmt::Display,
{
Error::Unexpected
}
#[inline]
fn message_token(_: Item) -> Self {
Error::Unexpected
}
#[inline]
fn message_range(_: Range) -> Self {
Error::Unexpected
}
#[inline]
fn end_of_input() -> Self {
Error::EndOfInput
}
#[inline]
fn is_unexpected_end_of_input(&self) -> bool {
*self == Error::EndOfInput
}
#[inline]
fn into_other<T>(self) -> T
where
T: StreamError<Item, Range>,
{
match self {
Error::Unexpected => T::unexpected_static_message("parse"),
Error::EndOfInput => T::end_of_input(),
Error::Io(err) => T::other(err),
}
}
}
impl<Item, Range, Position> ParseError<Item, Range, Position> for Error
where
Position: Default,
{
type StreamError = Self;
#[inline]
fn empty(_position: Position) -> Self {
Error::Unexpected
}
#[inline]
fn from_error(_: Position, err: Self::StreamError) -> Self {
err
}
#[inline]
fn set_position(&mut self, _position: Position) {}
#[inline]
fn add(&mut self, err: Self::StreamError) {
*self = match (&*self, err) {
(Error::EndOfInput, _) => Error::EndOfInput,
(_, err) => err,
};
}
#[inline]
fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
where
F: FnOnce(&mut Tracked<Self>),
{
f(self_);
self_.error = info;
}
fn is_unexpected_end_of_input(&self) -> bool {
*self == Error::EndOfInput
}
#[inline]
fn into_other<T>(self) -> T
where
T: ParseError<Item, Range, Position>,
{
T::from_error(Position::default(), StreamError::into_other(self))
}
}
pub struct Stream<R> {
bytes: Bytes<R>,
}
impl<R: Read> StreamOnce for Stream<R> {
type Token = u8;
type Range = &'static [u8];
type Position = usize;
type Error = Error;
#[inline]
fn uncons(&mut self) -> Result<u8, StreamErrorFor<Self>> {
match self.bytes.next() {
Some(Ok(b)) => Ok(b),
Some(Err(err)) => Err(Error::Io(err)),
None => Err(Error::EndOfInput),
}
}
}
impl<R> Stream<R>
where
R: Read,
{
/// Creates a `StreamOnce` instance from a value implementing `std::io::Read`.
///
/// NOTE: This type do not implement `Positioned` and `Clone` and must be wrapped with types
/// such as `BufferedStreamRef` and `State` to become a `Stream` which can be parsed
///
/// ```rust
/// # #![cfg(feature = "std")]
/// # extern crate combine;
/// use combine::*;
/// use combine::parser::byte::*;
/// use combine::stream::read;
/// use combine::stream::buffered;
/// use combine::stream::position;
/// use std::io::Read;
///
/// # fn main() {
/// let input: &[u8] = b"123,";
/// let stream = buffered::Stream::new(position::Stream::new(read::Stream::new(input)), 1);
/// let result = (many(digit()), byte(b','))
/// .parse(stream)
/// .map(|t| t.0);
/// assert_eq!(result, Ok((vec![b'1', b'2', b'3'], b',')));
/// # }
/// ```
pub fn new(read: R) -> Stream<R> {
Stream {
bytes: read.bytes(),
}
}
}

157
vendor/combine/src/stream/span.rs vendored Normal file
View File

@@ -0,0 +1,157 @@
use crate::lib::marker::PhantomData;
use crate::{
error::{ParseErrorInto, ParseResult, StreamErrorInto},
stream::{ResetStream, StreamErrorFor},
Positioned, RangeStream, RangeStreamOnce, StreamOnce,
};
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span<P> {
pub start: P,
pub end: P,
}
impl<P> From<P> for Span<P>
where
P: Clone,
{
#[inline]
fn from(p: P) -> Self {
Self {
start: p.clone(),
end: p,
}
}
}
impl<P> Span<P> {
pub fn map<Q>(self, mut f: impl FnMut(P) -> Q) -> Span<Q> {
Span {
start: f(self.start),
end: f(self.end),
}
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Stream<S, E>(pub S, PhantomData<fn(E) -> E>);
impl<S, E> From<S> for Stream<S, E> {
fn from(stream: S) -> Self {
Stream(stream, PhantomData)
}
}
impl<S, E> ResetStream for Stream<S, E>
where
S: ResetStream + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
<S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
StreamErrorInto<S::Token, S::Range>,
{
type Checkpoint = S::Checkpoint;
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
self.0.checkpoint()
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
self.0
.reset(checkpoint)
.map_err(ParseErrorInto::into_other_error)
}
}
impl<S, E> StreamOnce for Stream<S, E>
where
S: StreamOnce + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
<S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
StreamErrorInto<S::Token, S::Range>,
{
type Token = S::Token;
type Range = S::Range;
type Position = Span<S::Position>;
type Error = E;
#[inline]
fn uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>> {
self.0.uncons().map_err(StreamErrorInto::into_other_error)
}
#[inline]
fn is_partial(&self) -> bool {
self.0.is_partial()
}
}
impl<S, E> RangeStreamOnce for Stream<S, E>
where
S: RangeStream,
S::Token: PartialEq,
S::Range: PartialEq,
E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
<S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
StreamErrorInto<S::Token, S::Range>,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> {
self.0
.uncons_range(size)
.map_err(StreamErrorInto::into_other_error)
}
#[inline]
fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.0
.uncons_while(f)
.map_err(StreamErrorInto::into_other_error)
}
#[inline]
fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.0
.uncons_while1(f)
.map_err(StreamErrorInto::into_other_error)
}
#[inline]
fn distance(&self, end: &Self::Checkpoint) -> usize {
self.0.distance(end)
}
fn range(&self) -> Self::Range {
self.0.range()
}
}
impl<S, E> Positioned for Stream<S, E>
where
S: StreamOnce + Positioned,
S::Token: PartialEq,
S::Range: PartialEq,
E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
<S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
StreamErrorInto<S::Token, S::Range>,
{
fn position(&self) -> Span<S::Position> {
Span::from(self.0.position())
}
}

91
vendor/combine/src/stream/state.rs vendored Normal file
View File

@@ -0,0 +1,91 @@
use crate::{
error::ParseResult,
stream::{Positioned, RangeStreamOnce, ResetStream, StreamErrorFor, StreamOnce},
};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct Stream<S, U> {
pub stream: S,
pub state: U,
}
impl<S, U> Positioned for Stream<S, U>
where
S: Positioned,
{
#[inline]
fn position(&self) -> Self::Position {
self.stream.position()
}
}
impl<S, U> ResetStream for Stream<S, U>
where
S: ResetStream,
{
type Checkpoint = S::Checkpoint;
#[inline]
fn checkpoint(&self) -> Self::Checkpoint {
self.stream.checkpoint()
}
#[inline]
fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
self.stream.reset(checkpoint)
}
}
impl<S, U> StreamOnce for Stream<S, U>
where
S: StreamOnce,
{
type Token = S::Token;
type Range = S::Range;
type Position = S::Position;
type Error = S::Error;
#[inline]
fn uncons(&mut self) -> Result<S::Token, StreamErrorFor<Self>> {
self.stream.uncons()
}
fn is_partial(&self) -> bool {
self.stream.is_partial()
}
}
impl<S, U> RangeStreamOnce for Stream<S, U>
where
S: RangeStreamOnce,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> {
self.stream.uncons_range(size)
}
#[inline]
fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.stream.uncons_while(f)
}
fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Token) -> bool,
{
self.stream.uncons_while1(f)
}
#[inline]
fn distance(&self, end: &Self::Checkpoint) -> usize {
self.stream.distance(end)
}
#[inline]
fn range(&self) -> Self::Range {
self.stream.range()
}
}