Crates.io | streamparser |
lib.rs | streamparser |
version | 0.1.0 |
source | src |
created_at | 2020-06-21 11:52:42.553428 |
updated_at | 2020-06-21 11:52:42.553428 |
description | Adapt simple stream parsers for different types of streams |
homepage | |
repository | https://gitlab.com/fkrull/streamparser |
max_upload_size | |
id | 256295 |
size | 67,345 |
streamparser
— incrementally parse input from a streamThis crate provides adapters to turn a stream of input into a sequence of parsed items, using a simple incremental parsing function. It supports both sync and async input and allows returning items that hold a reference to the input.
Add the crate to your Cargo.toml
:
[dependencies]
streamparser = "0.1"
A complete example parsing whitespace-separated words from a stream:
use std::{convert::Infallible, error::Error, io::Cursor};
use streamparser::{read::ReadSource, utf8::Utf8Adapter, Parse, Parsed, StreamParser, Streaming};
struct SplitWords;
impl<'a> Parse<'a> for SplitWords {
type Input = &'a str;
type Output = &'a str;
type Error = Infallible;
fn parse(
&mut self,
input: Self::Input,
) -> Result<Streaming<Parsed<Self::Output>>, Self::Error> {
let trimmed = input.trim_start();
let trimmed_count = input.len() - trimmed.len();
match trimmed.find(' ') {
Some(end) => Ok(Streaming::Item((
Some(&trimmed[..end]),
trimmed_count + end,
))),
None => Ok(Streaming::Incomplete),
}
}
fn parse_eof(
&mut self,
input: Self::Input,
) -> Result<Parsed<Self::Output>, Self::Error> {
let trimmed = input.trim();
if !trimmed.is_empty() {
Ok((Some(trimmed), input.len()))
} else {
Ok((None, input.len()))
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
let mut results = Vec::new();
let mut parser = StreamParser::new(
Utf8Adapter::new(SplitWords),
ReadSource::new(Cursor::new(b"words separated by whitespace"), 6),
);
while let Some(streaming) = parser.get()? {
match streaming {
Streaming::Item(item) => {
results.push(item.to_owned());
}
Streaming::Incomplete => parser.advance()?,
}
}
assert_eq!(results, vec!["words", "separated", "by", "whitespace",]);
Ok(())
}
Cargo.toml
README.md
v<VERSION>
, e.g. v0.1.0