noa_parser/bytes/primitives/
whitespace.rs

1//! Recognize whitespaces
2
3use crate::bytes::token::Token;
4use crate::errors::{ParseError, ParseResult};
5use crate::recognizer::Recognizable;
6use crate::scanner::Scanner;
7use crate::visitor::Visitor;
8
9/// Recognize at least one whitespace
10pub struct Whitespaces;
11
12/// Recognize zero or more whitespaces
13pub struct OptionalWhitespaces;
14
15impl<'a> Visitor<'a, u8> for Whitespaces {
16    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
17        let mut found = false;
18
19        while Token::Whitespace.recognize(scanner)?.is_some() {
20            found = true;
21        }
22        if !found {
23            return Err(ParseError::UnexpectedToken);
24        }
25        Ok(Whitespaces)
26    }
27}
28
29impl<'a> Visitor<'a, u8> for OptionalWhitespaces {
30    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
31        if scanner.is_empty() {
32            return Ok(OptionalWhitespaces);
33        }
34        while Token::Whitespace.recognize(scanner)?.is_some() {}
35        Ok(OptionalWhitespaces)
36    }
37}