| Crates.io | nombytes |
| lib.rs | nombytes |
| version | 0.1.1 |
| created_at | 2022-07-23 20:42:35.470473+00 |
| updated_at | 2022-07-24 12:45:28.728959+00 |
| description | A library that provides a wrapper for the bytes::Bytes byte container for use with nom |
| homepage | |
| repository | https://github.com/alexschrod/nombytes |
| max_upload_size | |
| id | 631705 |
| size | 50,078 |
nombytes is a library that provides a wrapper for the bytes::Bytes byte
container for use with nom.
I originally made this so that I could have a function take a file name path
and return parsed values that still had references to the loaded file without
running into the lifetime issues associated with &[u8] and &str that
would prevent me from doing so. I decided to release it as a crate so that
others can make use of my efforts too.
This library has been tested to work with bytes down to v5.3.0 and nom down
to v6.0.0 and has been marked as such in its Cargo.toml.
Put this in your Cargo.toml:
[dependencies]
nombytes = "0.1.1"
mietteWith the miette feature enabled, the NomBytes implements its
SourceCode trait so it can be used directly with miette's
#[source_code] error attribute. This feature also enables the std
feature.
This library has been tested to work with miette down to v3.0.0 and
has been marked as such in its Cargo.toml.
serdeAdds serde::Serialize and serde::Deserialize implementations to the types
in this library to allow for using them with serde.
stdEnabled by default; allows creating NomBytes directly from Strings
through a From<String> impl. With this feature turned off, this crate
is #![no_std] compatible.
Borrowed from the nom crate, using NomBytes instead of &str. Had to be
modified slightly because NomBytes acts as &[u8] rather than as &str.
use nom::{
IResult,
bytes::complete::{tag, take_while_m_n},
combinator::map_res,
sequence::tuple};
use nombytes::NomBytes;
#[derive(Debug,PartialEq)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
}
fn from_hex(input: NomBytes) -> Result<u8, std::num::ParseIntError> {
u8::from_str_radix(input.to_str(), 16)
}
fn is_hex_digit(c: u8) -> bool {
(c as char).is_digit(16)
}
fn hex_primary(input: NomBytes) -> IResult<NomBytes, u8> {
map_res(
take_while_m_n(2, 2, is_hex_digit),
from_hex
)(input)
}
fn hex_color(input: NomBytes) -> IResult<NomBytes, Color> {
let (input, output) = tag("#")(input)?;
let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?;
Ok((input, Color { red, green, blue }))
}
fn main() {
assert!(matches!(hex_color(NomBytes::from("#2F14DF")),
Ok((r, Color {
red: 47,
green: 20,
blue: 223,
})) if r.to_str() == ""));
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.