Skip to main content

ParseWhile

Struct ParseWhile 

Source
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: F

Trait Implementations§

Source§

impl<F> Clone for ParseWhile<F>
where F: Fn(char) -> bool + Clone,

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<F> Copy for ParseWhile<F>
where F: Fn(char) -> bool + Copy,

Source§

impl<F> Debug for ParseWhile<F>
where F: Fn(char) -> bool + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F> Parser for ParseWhile<F>
where F: Fn(char) -> bool,

Source§

type Output = String

Source§

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>
where F: FnOnce(Self::Output) -> 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,

Make a new parser that consists of this parser, followed by another parser. Read more
Source§

fn and_then_combine_with<P, C>( self, other: P, combinator: C, ) -> AndThenParser<Self, P, C>
where P: Parser, C: AndCombinator<Self::Output, P::Output>,

Source§

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
Source§

fn with_mapping<T>( self, mapping: &dyn Fn(Self::Output) -> T, ) -> MapParser<'_, Self, T>

Source§

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,

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.