Crates.io | nom-bufreader |
lib.rs | nom-bufreader |
version | 0.2.0 |
source | src |
created_at | 2021-08-04 17:50:04.857355 |
updated_at | 2021-08-21 11:07:06.976377 |
description | BufReader adapter for nom parsers |
homepage | |
repository | |
max_upload_size | |
id | 431624 |
size | 63,583 |
*/!\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.
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(())
}
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(())
}
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(())
}