pub struct ParseWhile<F>(pub F)
where
F: Fn(char) -> bool;Expand description
Keep parsing characters while some predicate is met. If none of the characters
meet the predicate, and error will be returned. If this is not desired, try
using ParseWhileOrNothing
§Example
use parlib::parsers::ParseWhile;
use parlib::traits::Parser;
let parse_numbers = ParseWhile(|c| c.is_numeric());
let answer_valid = parse_numbers.parse("123a 1234");
assert_eq!(answer_valid, Ok(("123".to_string(), "a 1234".to_string())));In the following example, and error will be returned, since none of the characters
met the predicate is_numeric
use parlib::parsers::ParseWhile;
use parlib::traits::Parser;
use parlib::errors::ParsingError;
let parse_numbers = ParseWhile(|c| c.is_numeric());
let answer_bad = parse_numbers.parse("x123a 1234");
assert_eq!(
answer_bad,
Err(ParsingError::PatternNotFound(
"no characters matched predicate".to_string()
))
);Tuple Fields§
§0: FTrait Implementations§
Source§impl<F> Clone for ParseWhile<F>
impl<F> Clone for ParseWhile<F>
Source§fn clone(&self) -> ParseWhile<F>
fn clone(&self) -> ParseWhile<F>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl<F> Copy for ParseWhile<F>
Source§impl<F> Debug for ParseWhile<F>
impl<F> Debug for ParseWhile<F>
Source§impl<F> Parser for ParseWhile<F>
impl<F> Parser for ParseWhile<F>
type Output = String
Source§fn parse(&self, input: &str) -> ParserRes<Self::Output>
fn parse(&self, input: &str) -> ParserRes<Self::Output>
Parse the input string, if the parser is sucessful, it will return Ok((parsed, rest)),
where parsed is the data that was parsed from the string, and the rest is what was left
over. Read more
Source§fn parse_and_then_map<F, MappedOutput>(
&self,
input: &str,
f: F,
) -> ParserRes<MappedOutput>
fn parse_and_then_map<F, MappedOutput>( &self, input: &str, f: F, ) -> ParserRes<MappedOutput>
Parse the output (see parse function), and if sucessful, map the parsed output
Source§fn and_then<P>(self, other: P) -> AndThenParser<Self, P, IdentityAndCombinator>where
P: Parser,
fn and_then<P>(self, other: P) -> AndThenParser<Self, P, IdentityAndCombinator>where
P: Parser,
Make a new parser that consists of this parser, followed by another parser. Read more
fn and_then_combine_with<P, C>( self, other: P, combinator: C, ) -> AndThenParser<Self, P, C>
Source§fn otherwise<P>(self, other: P) -> OrThenParser<Self, P>where
P: Parser,
fn otherwise<P>(self, other: P) -> OrThenParser<Self, P>where
P: Parser,
Make a new parser that consists of this parser OR another parser. Read more
fn with_mapping<T>( self, mapping: &dyn Fn(Self::Output) -> T, ) -> MapParser<'_, Self, T>
fn with_try_mapping<T>( self, try_map: &dyn Fn(Self::Output) -> Option<T>, ) -> TryMapParser<'_, Self, T>
Source§fn preceed<P>(self, other: P) -> AndThenParser<P, Self, KeepSecondOutputOnly>where
P: Parser,
fn preceed<P>(self, other: P) -> AndThenParser<P, Self, KeepSecondOutputOnly>where
P: Parser,
Preceed this parser with another parser
Auto Trait Implementations§
impl<F> Freeze for ParseWhile<F>where
F: Freeze,
impl<F> RefUnwindSafe for ParseWhile<F>where
F: RefUnwindSafe,
impl<F> Send for ParseWhile<F>where
F: Send,
impl<F> Sync for ParseWhile<F>where
F: Sync,
impl<F> Unpin for ParseWhile<F>where
F: Unpin,
impl<F> UnsafeUnpin for ParseWhile<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for ParseWhile<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more