2 releases

0.1.1 Aug 1, 2021
0.1.0 Aug 1, 2021

#126 in Parser tooling

Download history 86/week @ 2024-06-17 156/week @ 2024-06-24 43/week @ 2024-07-01 88/week @ 2024-07-08 137/week @ 2024-07-15 114/week @ 2024-07-22 205/week @ 2024-07-29 74/week @ 2024-08-05 148/week @ 2024-08-12 166/week @ 2024-08-19 201/week @ 2024-08-26 73/week @ 2024-09-02 174/week @ 2024-09-09 116/week @ 2024-09-16 198/week @ 2024-09-23 144/week @ 2024-09-30

634 downloads per month
Used in 9 crates (5 directly)

Zlib license

35KB
660 lines

peekread

This crate allows you to take an arbitrary Read stream and 'peek ahead' into the stream without consuming the original stream. This is done through the PeekRead trait which has the method peek. When this method is called it returns a new PeekCursor object implementing Read, BufRead and Seek that allows you to read from the stream without affecting the original stream. The PeekRead trait is directly implemented on a select few types, but for most you will have to wrap your type in a SeekPeekReader or BufPeekReader that implements the peeking behavior using respectively seeking or buffering. Please refer to the the documentation for more information.

The minimum required stable Rust version for peekread is 1.31.0. To start using peekread add the following to your Cargo.toml:

[dependencies]
peekread = "0.1"

Example

A short example:

use peekread::{PeekRead, SeekPeekReader};

let mut f = SeekPeekReader::new(File::open("ambiguous")?);

// HTML is so permissive its parser never fails, so check for signature.
if f.starts_with("<!DOCTYPE") {
    Ok(ParseResult::Html(parse_as_html(f)))
} else {
    // Can pass PeekCursor to functions accepting T: Read without them
    // having to be aware of peekread.
    parse_as_jpg(f.peek()).map(ParseResult::Jpg)
       .or_else(|_| parse_as_png(f.peek()).map(ParseResult::Png))
       .or_else(|_| parse_as_gif(f.peek()).map(ParseResult::Gif))
       .or_else(|_| parse_as_javascript(f.peek()).map(ParseResult::Js))
}

License

peekread is released under the Zlib license, a permissive license. It is OSI and FSF approved and GPL compatible.

No runtime deps