tap-reader

Crates.iotap-reader
lib.rstap-reader
version1.0.1
sourcesrc
created_at2018-09-09 04:01:02.142808
updated_at2018-09-09 14:44:09.380185
descriptionWraps an existing reader and copies the read bytes into it's own buffer
homepagehttps://gitlab.com/pwoolcoc/tap-reader
repositoryhttps://gitlab.com/pwoolcoc/tap-reader
max_upload_size
id83676
size41,433
Paul Woolcock (pwoolcoc)

documentation

https://docs.rs/tap-reader

README

tap-reader

Wraps an existing reader and copies the read bytes into it's own buffer

Installation

Add the following line to your Cargo.toml:

[dependencies]
tap-reader = "1"

Usage

To use, add the following lines to your crate root (src/lib.rs, src/main.rs, etc.)

extern crate tap_reader;

use tap_reader::Tap;

Example

extern crate tap_reader;
extern crate serde_json;
extern crate serde;
#[macro_use] extern crate serde_derive;

use std::error::Error;
use std::io::Cursor;
use tap_reader::Tap;

#[derive(Debug, Deserialize, PartialEq)]
struct Foo {
    foo: String,
    bar: usize,
}

fn main() -> Result<(), Box<Error>> {
    let input = r#"{"foo":"hello, world!","bar":42}"#;
    let bytes = input.as_bytes();
    let reader = Cursor::new(bytes);
    let mut reader = Tap::new(reader);
    
    let foo: Foo = match serde_json::from_reader(&mut reader) {
        Ok(foo) => foo,
        Err(e) => {
            eprintln!("Error serializing Foo from input: '{}'\n error was: {}",
                      String::from_utf8_lossy(&reader.bytes),
                      e.description());
            return Err(());
        }
    };
    
    assert_eq!(
        foo,
        Foo {
            foo: "hello, world!".to_string(),
            bar: 42
        }
    );
    assert_eq!(reader.bytes, bytes);
    Ok(())
}
Commit count: 0

cargo fmt