Crates.io | bparse |
lib.rs | bparse |
version | 0.14.0 |
source | src |
created_at | 2023-12-12 01:44:41.732054 |
updated_at | 2024-06-27 19:16:54.649031 |
description | A library for parsing bytes |
homepage | |
repository | https://github.com/eze-works/bparse |
max_upload_size | |
id | 1065755 |
size | 26,222 |
This crate provides utilites for parsing byte slices. The API borrows some concepts from other parser-combinator crates but heavily simplifies things by eschewing error management and focusing exclusively on parsing byte slices.
The classic nom example; a hexadecimal color parser:
use bparse::{Pattern, Parser, range, end};
use std::str::from_utf8;
#[derive(Debug, PartialEq)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
}
fn main() {
assert_eq!(hex_color("#2F14DF"), Some(Color {
red: 47,
green: 20,
blue: 223,
}));
}
fn hex_color(input: &str) -> Option<Color> {
let hexbyte = range(b'0', b'9').or(range(b'A', b'F')).or(range(b'a', b'f')).repeats(2);
let mut parser = Parser::new(input.as_bytes());
parser.try_match("#")?;
let red = parser.try_match(hexbyte)?;
let green = parser.try_match(hexbyte)?;
let blue = parser.try_match(hexbyte)?;
parser.try_match(end)?;
Some(Color {
red: u8::from_str_radix(from_utf8(red).unwrap(), 16).unwrap(),
green: u8::from_str_radix(from_utf8(green).unwrap(), 16).unwrap(),
blue: u8::from_str_radix(from_utf8(blue).unwrap(), 16).unwrap()
})
}