Crates.io | noggin |
lib.rs | noggin |
version | 0.1.0 |
source | src |
created_at | 2023-10-04 21:58:46.917193 |
updated_at | 2023-10-04 21:58:46.917193 |
description | A declarative, zero-copy, proc-macro based HTTP header parser |
homepage | |
repository | https://github.com/plasmaconduit/noggin/ |
max_upload_size | |
id | 993374 |
size | 7,028 |
A declarative, zero-copy, proc-macro based header parser for Rust.
Table of Contents
Define your HTTP header structure and derive the parsing logic using noggin::Noggin
:
use noggin::{Noggin, HeadParser};
#[derive(Noggin)]
pub struct TestHeaders<'a> {
pub content_type: &'a str,
pub content_length: u32,
pub accept: Vec<&'a str>,
pub connection: Option<&'a str>,
pub pragma: Option<Vec<&'a str>>,
}
let raw_headers = b"content-type: text/html\r\n\
content-length: 12\r\n\
accept: text/html, text/plain\r\n\
pragma: no-cache, public\r\n\
accept: application/json\r\n\r\n\
hello world!";
let (parsed_headers, body) = TestHeaders::parse_headers(raw_headers).unwrap();
assert_eq!(parsed_headers.content_type, "text/html");
assert_eq!(parsed_headers.content_length, 12);
assert_eq!(parsed_headers.accept, vec!["text/html", "text/plain", "application/json"]);
assert_eq!(parsed_headers.pragma.unwrap(), vec!["no-cache", "public"]);
assert_eq!(body, b"hello world!");
Tests should run fine with the standard cargo test
.
However, for consistency, we recommend using the dockerized test environment.
To use the dockerized test environment the only requirements are make
and
docker
(you don't even need rust installed locally). Simply run the
following command.
make test