Skip to main content

Pattern

Trait Pattern 

Source
pub trait Pattern {
Show 13 methods // Required methods fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>; fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>; fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>; fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>; // Provided methods fn immediate_matches<I: Input>(&self, input: I) -> (I, I) { ... } fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) { ... } fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) { ... } fn by_ref(&self) -> Ref<'_, Self> { ... } fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other> where Self: Sized { ... } fn not_escaped_by<Prefix: Pattern>( self, prefix: Prefix, ) -> NotEscaped<Prefix, Self> where Self: Sized { ... } fn not_enclosed_by<Enclosure: Pattern>( self, enc: Enclosure, ) -> NotEnclosed<Enclosure, Self> where Self: Sized { ... } fn many(self) -> Many<Self> where Self: Sized { ... } fn maybe(self) -> Maybe<Self> where Self: Sized { ... }
}
Expand description

This trait represents an object that can be matched onto a string. This includes functions, characters, [arrays of] characters, strings, but also custom patterns like NotEscaped

See built-in patterns and parser adapters for patterns in the pattern module

Hint: on the success path, the 1st element of the return tuple is the rest of the input (with or without the matched pattern at the start)

Required Methods§

Source

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

The return values are (rest of the input, matched fragment at the beginning).

§Errors

In the case of no match, the original input is returned as the Err variant.

Used by crate::parser::one.

Source

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Like Pattern::immediate_match, but matches at the end of input. The return values are (the input before the match, the match)

§Errors

In the case of no match, the original input is returned as the Err variant.

Used by the Pattern impl of NotEscaped

Source

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

The return values are (the match + rest of the input, (string before the match, the match)).

§Errors

Returns the provided input unchanged in the Err variant if there’s no match.

Used by crate::parser::until.

Source

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Like Pattern::first_match, but the match is excluded from the rest of the input.

§Errors

Returns the provided input unchanged in the Err variant if there’s no match.

Used by crate::parser::until_ex.

Provided Methods§

Source

fn immediate_matches<I: Input>(&self, input: I) -> (I, I)

The return values are (rest of the input, contiguous matched fragments from the beginning).

0 is also a valid number of matches.

Used by crate::parser::many

Source

fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))

Like Pattern::immediate_matches, but also counts the number of matches.

Used by the Pattern impl of NotEscaped

Source

fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)

Like Pattern::immediate_matches_counted, but matches at the end of input, and doesn’t return the matched fragment of the input.

Used by the Pattern impl of NotEscaped

Source

fn by_ref(&self) -> Ref<'_, Self>

Get the pattern by reference to avoid moving it, which will happen in generic code

Do not override this method.

Source

fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other>
where Self: Sized,

Combine self and another pattern into a pattern that matches both of them in a sequence, with self before other

Do not override this method.

Source

fn not_escaped_by<Prefix: Pattern>( self, prefix: Prefix, ) -> NotEscaped<Prefix, Self>
where Self: Sized,

Create a pattern that’ll match self only if it’s not escaped (immediately preceded) by the provided pattern.

Do not override this method.

Source

fn not_enclosed_by<Enclosure: Pattern>( self, enc: Enclosure, ) -> NotEnclosed<Enclosure, Self>
where Self: Sized,

Create a pattern that’ll match self only if it’s not enclosed (preceded & superceded) by the provided pattern.

Do not override this method.

Source

fn many(self) -> Many<Self>
where Self: Sized,

Creates a greedy pattern that matches self as many times as possible. See Many

Source

fn maybe(self) -> Maybe<Self>
where Self: Sized,

Creates a pattern that matches self 0 or 1 times. See Maybe

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Pattern for &str

Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn immediate_matches<I: Input>(&self, input: I) -> (I, I)

Source§

fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))

Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)

Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

impl Pattern for char

Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn immediate_matches<I: Input>(&self, input: I) -> (I, I)

Source§

fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))

Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)

Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

impl<L: Pattern, R: Pattern> Pattern for Either<L, R>

Available on crate feature either only.
Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn immediate_matches<I: Input>(&self, input: I) -> (I, I)

Source§

fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))

Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)

Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

impl<P1: Pattern, P2: Pattern> Pattern for (P1, P2)

A tuple pattern matches either of the 2 patterns in a short-circuiting & commutative manner, with self tried first. Tuples of more than 2 elements can be turned into a pattern via the IntoPattern trait.

§Performance

If you want to match either of N chars, use an array of them as a pattern instead, as this struct has a general impl that may miss optimisations applicable to the case of [char; N] being the pattern.

Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

impl<const N: usize> Pattern for [char; N]

This is a specialised, optimised impl for matching any char in the array. For a more general pattern combinator, use a tuple.

Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Implementors§

Source§

impl Pattern for Any

Source§

impl<Enclosure: Pattern, Inner: Pattern> Pattern for NotEnclosed<Enclosure, Inner>

Source§

impl<F: Fn(char) -> bool> Pattern for F

Source§

impl<P1: Pattern, P2: Pattern> Pattern for Chain<P1, P2>

Source§

impl<Prefix: Pattern, Inner: Pattern> Pattern for NotEscaped<Prefix, Inner>

Source§

impl<T: ?Sized + Pattern> Pattern for Ref<'_, T>

Source§

impl<T: Pattern> Pattern for Many<T>

Source§

impl<T: Pattern> Pattern for Maybe<T>