dangerous/reader/
string.rs

1use crate::error::{CoreOperation, ExpectedLength};
2use crate::input::PrivateExt;
3
4use super::StringReader;
5
6impl<'i, E> StringReader<'i, E> {
7    /// Read a char.
8    ///
9    /// # Errors
10    ///
11    /// Returns an error if there is no more input.
12    #[inline]
13    pub fn read_char(&mut self) -> Result<char, E>
14    where
15        E: From<ExpectedLength<'i>>,
16    {
17        self.try_advance(|input| input.split_token(CoreOperation::ReadChar))
18    }
19
20    /// Read an optional char.
21    ///
22    /// Returns `Some(char)` if there was enough input, `None` if not.
23    #[inline]
24    pub fn read_char_opt(&mut self) -> Option<char> {
25        self.advance_opt(PrivateExt::split_token_opt)
26    }
27
28    /// Peek the next char in the input without mutating the `Reader`.
29    ///
30    /// # Errors
31    ///
32    /// Returns an error if the `Reader` has no more input.
33    #[inline]
34    pub fn peek_char(&self) -> Result<char, E>
35    where
36        E: From<ExpectedLength<'i>>,
37    {
38        self.input
39            .clone()
40            .split_token(CoreOperation::PeekChar)
41            .map(|(token, _)| token)
42    }
43
44    /// Peek the next char in the input without mutating the `Reader`.
45    ///
46    /// This is equivalent to `peek_char` but does not return an error. Don't use
47    /// this function if you want an error if there isn't enough input.
48    #[inline]
49    #[must_use = "peek result must be used"]
50    pub fn peek_char_opt(&self) -> Option<char> {
51        self.input.clone().split_token_opt().map(|(token, _)| token)
52    }
53}