3 unstable releases

0.2.0 Aug 21, 2021
0.1.1 Aug 6, 2021
0.1.0 Aug 4, 2021

#173 in Parser tooling

Download history 43/week @ 2024-02-25 18/week @ 2024-03-03 22/week @ 2024-03-10 13/week @ 2024-03-17 16/week @ 2024-03-24 78/week @ 2024-03-31 13/week @ 2024-04-07 9/week @ 2024-04-14 15/week @ 2024-04-21 15/week @ 2024-04-28 106/week @ 2024-05-05 28/week @ 2024-05-12 75/week @ 2024-05-19 46/week @ 2024-05-26 28/week @ 2024-06-02 2/week @ 2024-06-09

154 downloads per month
Used in sie4

MIT license

39KB
558 lines

nom-bufreader, adapters for BufReader around nom

*/!\Work in progress, if you put it in production, you fix it/!*

With this crate, you can assemble a nom parser with a BufReader alternative, synchronous or asynchronous. Due to incompatible buffering strategies, std::io::BufReader and futures::io::BufReader cannot be used directly. This crate proovide compatible forks instead, in the bufreader and async_bufreader modules.

It will hide for you the Incomplete handling in nom for streaming parsers, retrying and refilling buffers automatically.

Examples

sync

use nom_bufreader::bufreader::BufReader;
use nom_bufreader::{Error, Parse};
use std::{net::TcpListener, str::from_utf8};

fn main() -> Result<(), Error<()>> {
    let listener = TcpListener::bind("127.0.0.1:8080")?;
    let mut i = BufReader::new(listener.incoming().next().unwrap()?);

    // method, space and path are nom parsers
    let m = i.parse(method)?;
    let _ = i.parse(space)?;
    let p = i.parse(path)?;
    println!("got method {}, path {}", m, p);
    Ok(())
}

async

tokio

use nom_bufreader::async_bufreader::BufReader;
use nom_bufreader::{AsyncParse, Error};
use std::str::from_utf8;
use tokio_util::compat::TokioAsyncReadCompatExt;
use tokio::net::TcpListener;

#[tokio::main]
async fn main() -> Result<(), Error<()>> {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;
    let mut i = BufReader::new(listener.accept().await?.0.compat());

    let m = i.parse(method).await?;
    let _ = i.parse(space).await?;
    let p = i.parse(path).await?;
    println!("got method {}, path {}", m, p);
    Ok(())
}

async-std

use nom_bufreader::async_bufreader::BufReader;
use nom_bufreader::{AsyncParse, Error};
use std::str::from_utf8;
use async_std::net::TcpListener;

#[async_std::main]
async fn main() -> Result<(), Error<()>> {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;
    let mut i = BufReader::new(listener.accept().await?.0);

    let m = i.parse(method).await?;
    let _ = i.parse(space).await?;
    let p = i.parse(path).await?;
    println!("got method {}, path {}", m, p);
    Ok(())
}

Dependencies

~0.9–1.3MB
~25K SLoC