Skip to content

Commit

Permalink
Merge pull request #582 from epage/peek
Browse files Browse the repository at this point in the history
docs(examples): Avoid parse_peek in production code
  • Loading branch information
epage authored Aug 2, 2024
2 parents bf4ae12 + 6a27c4e commit 43b3c53
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions examples/ndjson/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use winnow::error::ErrMode;
use winnow::error::Needed;
use winnow::prelude::*;
use winnow::stream::Offset;
use winnow::stream::Stream as _;

fn main() -> Result<(), lexopt::Error> {
let args = Args::parse()?;
Expand Down Expand Up @@ -38,13 +39,15 @@ fn main() -> Result<(), lexopt::Error> {
buffer.fill(read);

loop {
let input = parser::Stream::new(std::str::from_utf8(buffer.data()).map_err(to_lexopt)?);
match parser::ndjson::<ContextError>.parse_peek(input) {
Ok((remainder, value)) => {
let mut input =
parser::Stream::new(std::str::from_utf8(buffer.data()).map_err(to_lexopt)?);
let start = input.checkpoint();
match parser::ndjson::<ContextError>.parse_next(&mut input) {
Ok(value) => {
println!("{value:?}");
println!();
// Tell the buffer how much we read
let consumed = remainder.offset_from(&input);
let consumed = input.offset_from(&start);
buffer.consume(consumed);
}
Err(ErrMode::Backtrack(e)) | Err(ErrMode::Cut(e)) => {
Expand Down

0 comments on commit 43b3c53

Please sign in to comment.