Function parser_compose::byte

source ·
pub fn byte(range: impl ByteRange) -> Byte
Expand description

Returns a parser that recognizes the first byte in a slice if its value is in the specified range.

The range can be specified in two ways:

  • An inclusive range (e.g. 0..=30)
  • A single u8 value (e.g. 8)
use parser_compose::{Parser, byte};

let msg = b"1a";

let digit = byte(0x30..=0x39);
let (value, rest) = digit.try_parse(msg).unwrap();

assert_eq!(value, [0x31]);
assert_eq!(rest, b"a");

let result = digit.try_parse(rest);
assert!(result.is_none());