pub fn byte_lut<T: Copy>(
lut: &[Option<T>; 256],
error: ParseError,
) -> ByteLut<'_, T>
Expand description
Parser that consumes a single byte and maps it using a lookup table.
Equivalent to parser::byte().map_res(|b| LOOKUP[b as usize].ok_or("expected ..."))
, which is
usually faster than an equivalent match statement in the closure.
See also parser::byte_map!
which wraps this function, allowing a
match-like syntax to be used to define the lookup table.
ยงExamples
const LOOKUP: [Option<bool>; 256] = {
let mut x = [None; 256];
x['#' as usize] = Some(true);
x['.' as usize] = Some(false);
x
};
let parser = parser::byte_lut(&LOOKUP, ParseError::Custom("expected '#' or '.'"));
assert_eq!(parser.parse(b"#..##"), Ok((true, &b"..##"[..])));
assert_eq!(parser.parse(b"..##"), Ok((false, &b".##"[..])));
assert_eq!(parser.parse(b"abc"), Err((ParseError::Custom("expected '#' or '.'"), &b"abc"[..])));