Skip to content

Commit

Permalink
docs(examples): Avoid parse_peek in production code
Browse files Browse the repository at this point in the history
Inspired by #580
  • Loading branch information
epage committed Aug 2, 2024
1 parent bf4ae12 commit 6a27c4e
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 6a27c4e

Please sign in to comment.