bparse

Trait Pattern

Source
pub trait Pattern {
    // Required method
    fn eval(&self, input: &[u8]) -> Option<usize>;

    // Provided methods
    fn then<P>(self, other: P) -> Sequence<Self, P>
       where Self: Sized,
             P: Pattern { ... }
    fn and<P>(self, other: P) -> Composition<Self, P>
       where Self: Sized,
             P: Pattern { ... }
    fn or<P>(self, other: P) -> Choice<Self, P>
       where Self: Sized,
             P: Pattern { ... }
}
Expand description

Expresses that the implementing can be used to match a slice of bytes.

Required Methods§

Source

fn eval(&self, input: &[u8]) -> Option<usize>

Evaluates the pattern against a slice of bytes. If the pattern matches, the length of matching bytes should be returned. Otherwise, None should be returned.

It is assumed that the pattern begins matching from the beining of the slice.

Provided Methods§

Source

fn then<P>(self, other: P) -> Sequence<Self, P>
where Self: Sized, P: Pattern,

Returns a new pattern that will match if self and other match in sequence.

use bparse::Pattern;

let pattern = "a".then("b");
assert_eq!(pattern.eval(b"abc"), Some(2));
Source

fn and<P>(self, other: P) -> Composition<Self, P>
where Self: Sized, P: Pattern,

Returns a new pattern that will match if both self and other match.

Unlike Pattern::then, self and other are evaluated against the same input.

use bparse::{Pattern, hex, alpha};

let pattern = hex().and(alpha());
assert_eq!(pattern.eval(b"a"), Some(1));
assert_eq!(pattern.eval(b"3"), None);
Source

fn or<P>(self, other: P) -> Choice<Self, P>
where Self: Sized, P: Pattern,

Returns a new pattern that will match if either self or other match.

use bparse::Pattern;

let pattern = "a".or("b");
assert_eq!(pattern.eval(b"ba"), Some(1));

Implementations on Foreign Types§

Source§

impl Pattern for &str

Source§

fn eval(&self, input: &[u8]) -> Option<usize>

Implementors§

Source§

impl Pattern for Any

Source§

impl Pattern for ByteRange

Source§

impl Pattern for ByteSlice<'_>

Source§

impl Pattern for End

Source§

impl Pattern for LookupTable

Source§

impl<P1, P2> Pattern for Choice<P1, P2>
where P1: Pattern, P2: Pattern,

Source§

impl<P1, P2> Pattern for Composition<P1, P2>
where P1: Pattern, P2: Pattern,

Source§

impl<P1, P2> Pattern for Sequence<P1, P2>
where P1: Pattern, P2: Pattern,

Source§

impl<P> Pattern for NegativeLookahead<P>
where P: Pattern,

Source§

impl<P> Pattern for Repetition<P>
where P: Pattern,