1use udled::{
2 tokenizers::or, AsBytes, AsChar, Buffer, Error, Item, Reader, Tokenizer, TokenizerExt,
3};
4
5#[derive(Debug, Clone, Copy, Default)]
7pub struct Bool;
8
9impl<'input, B> Tokenizer<'input, B> for Bool
10where
11 B: Buffer<'input>,
12 B::Item: AsChar,
13 B::Source: AsBytes<'input>,
14{
15 type Token = Item<bool>;
16
17 fn to_token<'a>(&self, reader: &mut Reader<'_, 'input, B>) -> Result<Self::Token, Error> {
18 let item = reader
19 .parse(or(
20 "true".map_ok(|m| m.map(|_| true)),
21 "false".map_ok(|m| m.map(|_| false)),
22 ))?
23 .unify();
24
25 Ok(item)
26 }
27
28 fn eat(&self, reader: &mut Reader<'_, 'input, B>) -> Result<(), Error> {
29 reader.eat(or("true", "false"))
30 }
31
32 fn peek<'a>(&self, reader: &mut Reader<'_, 'input, B>) -> bool {
33 reader.is(or("true", "false"))
34 }
35}
36
37#[cfg(test)]
38mod test {
39 use udled::{Input, EOF};
40
41 use super::Bool;
42
43 #[test]
44 fn bool() {
45 let mut input = Input::new("true false");
46
47 let (a, _, b) = input.parse((Bool, ' ', Bool)).unwrap();
48
49 assert_eq!(a.value, true);
50 assert_eq!(b.value, false);
51 assert!(input.is(EOF))
52 }
53}