5 unstable releases

0.3.1 May 19, 2024
0.3.0 Dec 4, 2023
0.2.1 Nov 21, 2023
0.2.0 Nov 21, 2023
0.1.0 Nov 19, 2023

#887 in Parser implementations

Download history 5/week @ 2024-07-21 38/week @ 2024-07-28 16/week @ 2024-09-22

294 downloads per month

MIT license

79KB
2.5K SLoC

hefty

Parser for streaming data.

// Build parsers for a URI hostname (https://www.rfc-editor.org/rfc/rfc3986.html#appendix-A)
let unreserved = char::when(|c| c.is_ascii_alphanumeric() || "-._~".contains(c));
let pct_encoded = (
    '%',
    char::when(|c| c.is_ascii_hexdigit()).times(2).collect(),
)
    .seq()
    .collect();
let sub_delims = ('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=').any();

let reg_name = (&unreserved, &pct_encoded, &sub_delims)
    .any()
    .repeated(1..255)
    .collect();

// Parse a hostname arriving as a stream of data.
let ParseResult::Partial(state) = reg_name.extract(ByteStream::from("www.exa"), None, false)
else {
    panic!();
};
let ParseResult::Partial(state) =
    reg_name.extract(ByteStream::from("mple.co"), Some(state), false)
else {
    panic!();
};
let ParseResult::Match(output, input) =
    reg_name.extract(ByteStream::from("m/path"), Some(state), true)
else {
    panic!();
};
assert_eq!(output.to_string(), "www.example.com");
assert_eq!(input.to_string(), "/path");

Dependencies

~0.4–1MB
~20K SLoC