valence_command/parsers/
bool.rs

1use super::Parser;
2use crate::parsers::{CommandArg, CommandArgParseError, ParseInput};
3
4impl CommandArg for bool {
5    fn parse_arg(input: &mut ParseInput) -> Result<Self, CommandArgParseError> {
6        input.skip_whitespace();
7        if input.match_next("true") {
8            Ok(true)
9        } else if input.match_next("false") {
10            Ok(false)
11        } else {
12            Err(CommandArgParseError::InvalidArgument {
13                expected: "bool".to_owned(),
14                got: input.peek_word().to_owned(),
15            })
16        }
17    }
18
19    fn display() -> Parser {
20        Parser::Bool
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_bool() {
30        let mut input = ParseInput::new("true");
31        assert!(bool::parse_arg(&mut input).unwrap());
32        assert!(input.is_done());
33
34        let mut input = ParseInput::new("false");
35        assert!(!bool::parse_arg(&mut input).unwrap());
36        assert!(input.is_done());
37
38        let mut input = ParseInput::new("false ");
39        assert!(!bool::parse_arg(&mut input).unwrap());
40        assert!(!input.is_done());
41
42        let mut input = ParseInput::new("falSe trUe");
43        assert!(!bool::parse_arg(&mut input).unwrap());
44        assert!(bool::parse_arg(&mut input).unwrap());
45        assert!(input.is_done());
46    }
47}