|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::io; |
| 3 | +use std::pin::Pin; |
| 4 | +use std::task::{Context, Poll}; |
| 5 | + |
| 6 | +use futures::Stream; |
| 7 | +use tokio::io::{AsyncRead, ReadBuf}; |
| 8 | + |
| 9 | +use crate::{Error, Position, Record}; |
| 10 | + |
| 11 | +pub(crate) fn invalid_format( |
| 12 | + message: &'static str, line: usize, column: usize, byte: usize, |
| 13 | +) -> Error { |
| 14 | + Error::InvalidFormat { |
| 15 | + message: Cow::Borrowed(message), |
| 16 | + position: Position { line, column, byte }, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +pub(crate) fn partial_data(line: usize, column: usize, byte: usize) -> Error { |
| 21 | + invalid_format("partial data at end of stream", line, column, byte) |
| 22 | +} |
| 23 | + |
| 24 | +pub(crate) fn duplicate_key(key: &str, record: Record) -> Error { |
| 25 | + Error::DuplicateKey { |
| 26 | + key: key.to_string(), |
| 27 | + record, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +pub(crate) fn cannot_output(typ: &'static str, reason: &'static str) -> Error { |
| 32 | + Error::CannotOutput { typ, reason } |
| 33 | +} |
| 34 | + |
| 35 | +pub(crate) struct TrickleReader { |
| 36 | + data: Vec<u8>, |
| 37 | + pos: usize, |
| 38 | + chunk: usize, |
| 39 | + delayed: bool, |
| 40 | +} |
| 41 | + |
| 42 | +impl TrickleReader { |
| 43 | + pub(crate) fn new(data: &str, chunk: usize) -> Self { |
| 44 | + Self { |
| 45 | + data: data.as_bytes().to_vec(), |
| 46 | + pos: 0, |
| 47 | + chunk, |
| 48 | + delayed: false, |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl AsyncRead for TrickleReader { |
| 54 | + fn poll_read( |
| 55 | + mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, |
| 56 | + ) -> Poll<io::Result<()>> { |
| 57 | + let remaining = self.data.len() - self.pos; |
| 58 | + if remaining == 0 { |
| 59 | + return Poll::Ready(Ok(())); |
| 60 | + } |
| 61 | + |
| 62 | + if !self.delayed { |
| 63 | + self.delayed = true; |
| 64 | + cx.waker().wake_by_ref(); |
| 65 | + return Poll::Pending; |
| 66 | + } |
| 67 | + |
| 68 | + let to_read = remaining.min(self.chunk).min(buf.remaining()); |
| 69 | + buf.put_slice(&self.data[self.pos..self.pos + to_read]); |
| 70 | + self.pos += to_read; |
| 71 | + self.delayed = false; |
| 72 | + Poll::Ready(Ok(())) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub(crate) struct TrickleStream<S> { |
| 77 | + inner: S, |
| 78 | + delayed: bool, |
| 79 | +} |
| 80 | + |
| 81 | +impl<S> TrickleStream<S> { |
| 82 | + pub(crate) fn new(inner: S) -> Self { |
| 83 | + Self { |
| 84 | + inner, |
| 85 | + delayed: false, |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<S> Stream for TrickleStream<S> |
| 91 | +where |
| 92 | + S: Stream + Unpin, |
| 93 | +{ |
| 94 | + type Item = S::Item; |
| 95 | + |
| 96 | + fn poll_next( |
| 97 | + mut self: Pin<&mut Self>, cx: &mut Context<'_>, |
| 98 | + ) -> Poll<Option<Self::Item>> { |
| 99 | + if !self.delayed { |
| 100 | + self.delayed = true; |
| 101 | + cx.waker().wake_by_ref(); |
| 102 | + return Poll::Pending; |
| 103 | + } |
| 104 | + |
| 105 | + self.delayed = false; |
| 106 | + Pin::new(&mut self.inner).poll_next(cx) |
| 107 | + } |
| 108 | +} |
0 commit comments