Decodes a canvas drawing represented as a stream of characters.
(source: In)
| 1696 | /// Decodes a canvas drawing represented as a stream of characters. |
| 1697 | /// |
| 1698 | pub fn decode_drawing_stream<In: Unpin+Stream<Item=Result<char, E>>, E>(source: In) -> impl Unpin+Stream<Item=Result<Draw, StreamDecoderError<E>>> { |
| 1699 | let mut source = source; |
| 1700 | let mut decoder = CanvasDecoder::new(); |
| 1701 | let mut seen_error = false; |
| 1702 | |
| 1703 | stream::poll_fn(move |context| { |
| 1704 | if seen_error { |
| 1705 | // Only allow one error from the decoder (it remains in an error state after this) |
| 1706 | Poll::Ready(None) |
| 1707 | } else { |
| 1708 | loop { |
| 1709 | match source.poll_next_unpin(context) { |
| 1710 | Poll::Ready(None) => { return Poll::Ready(None); }, |
| 1711 | Poll::Pending => { return Poll::Pending; }, |
| 1712 | Poll::Ready(Some(Ok(c))) => { |
| 1713 | match decoder.decode(c) { |
| 1714 | Ok(None) => { continue; }, |
| 1715 | Ok(Some(draw)) => { return Poll::Ready(Some(Ok(draw))); }, |
| 1716 | Err(err) => { seen_error = true; return Poll::Ready(Some(Err(StreamDecoderError::Decoder(err)))); } |
| 1717 | } |
| 1718 | }, |
| 1719 | |
| 1720 | Poll::Ready(Some(Err(err))) => { return Poll::Ready(Some(Err(StreamDecoderError::Stream(err)))); } |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | }) |
| 1725 | } |
| 1726 | |
| 1727 | #[cfg(test)] |
| 1728 | mod test { |