byte_parser/
expect_byte.rs

1
2type ExpResult<T> = Result<T, Option<u8>>;
3
4pub trait ExpectByte: Sized {
5
6	fn expect_byte_fn<F>(self, f: F) -> ExpResult<Self>
7	where F: Fn(u8) -> bool;
8
9	#[inline]
10	fn expect_byte(self, byte: u8) -> ExpResult<Self> {
11		self.expect_byte_fn(|b| b == byte)
12	}
13
14}
15
16impl ExpectByte for Option<u8> {
17
18	#[inline]
19	fn expect_byte_fn<F>(self, f: F) -> ExpResult<Self>
20	where F: Fn(u8) -> bool {
21		match self {
22			Some(b) if f(b) => Ok(self),
23			_ => Err(self)
24		}
25	}
26
27}
28
29impl ExpectByte for u8 {
30
31	#[inline]
32	fn expect_byte_fn<F>(self, f: F) -> ExpResult<Self>
33	where F: Fn(u8) -> bool {
34		match f(self) {
35			true => Ok(self),
36			false => Err(Some(self))
37		}
38	}
39
40}