Struct matchers::Pattern

source ·
pub struct Pattern<S = usize, A = DenseDFA<Vec<S>, S>>where
    S: StateID,
    A: DFA<ID = S>,{ /* private fields */ }
Expand description

A compiled match pattern that can match multipe inputs, or return a Matcher that matches a single input.

Implementations§

source§

impl Pattern

source

pub fn new(pattern: &str) -> Result<Self, Error>

Returns a new Pattern for the given regex, or an error if the regex was invalid.

source§

impl<S, A> Pattern<S, A>where S: StateID, A: DFA<ID = S>, Self: for<'a> ToMatcher<'a, S>,

source

pub fn matches(&self, s: &impl AsRef<str>) -> bool

Returns true if this pattern matches the given string.

source

pub fn debug_matches(&self, d: &impl Debug) -> bool

Returns true if this pattern matches the formatted output of the given type implementing fmt::Debug.

For example:

use matchers::Pattern;

#[derive(Debug)]
pub struct Hello {
    to: &'static str,
}

let pattern = Pattern::new(r#"Hello \{ to: "W[^"]*" \}"#).unwrap();

let hello_world = Hello { to: "World" };
assert!(pattern.debug_matches(&hello_world));

let hello_sf = Hello { to: "San Francisco" };
assert_eq!(pattern.debug_matches(&hello_sf), false);

let hello_washington = Hello { to: "Washington" };
assert!(pattern.debug_matches(&hello_washington));
source

pub fn display_matches(&self, d: &impl Display) -> bool

Returns true if this pattern matches the formatted output of the given type implementing fmt::Display.

For example:

use matchers::Pattern;

#[derive(Debug)]
pub struct Hello {
    to: &'static str,
}

impl fmt::Display for Hello {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Hello {}", self.to)
    }
}

let pattern = Pattern::new("Hello [Ww].+").unwrap();

let hello_world = Hello { to: "world" };
assert!(pattern.display_matches(&hello_world));
assert_eq!(pattern.debug_matches(&hello_world), false);

let hello_sf = Hello { to: "San Francisco" };
assert_eq!(pattern.display_matches(&hello_sf), false);

let hello_washington = Hello { to: "Washington" };
assert!(pattern.display_matches(&hello_washington));
source

pub fn read_matches(&self, io: impl Read) -> Result<bool>

Returns either a bool indicating whether or not this pattern matches the data read from the provided io::Read stream, or an io::Error if an error occurred reading from the stream.

Trait Implementations§

source§

impl<S, A> Clone for Pattern<S, A>where S: StateID + Clone, A: DFA<ID = S> + Clone,

source§

fn clone(&self) -> Pattern<S, A>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<S, A> Debug for Pattern<S, A>where S: StateID + Debug, A: DFA<ID = S> + Debug,

source§

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

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

impl FromStr for Pattern

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<'a, S> ToMatcher<'a, S> for Pattern<S, DenseDFA<Vec<S>, S>>where S: StateID + 'a,

§

type Automaton = DenseDFA<&'a [S], S>

source§

fn matcher(&'a self) -> Matcher<'a, S, Self::Automaton>

source§

impl<'a, S> ToMatcher<'a, S> for Pattern<S, SparseDFA<Vec<u8>, S>>where S: StateID + 'a,

§

type Automaton = SparseDFA<&'a [u8], S>

source§

fn matcher(&'a self) -> Matcher<'a, S, Self::Automaton>

Auto Trait Implementations§

§

impl<S, A> RefUnwindSafe for Pattern<S, A>where A: RefUnwindSafe,

§

impl<S, A> Send for Pattern<S, A>where A: Send,

§

impl<S, A> Sync for Pattern<S, A>where A: Sync,

§

impl<S, A> Unpin for Pattern<S, A>where A: Unpin,

§

impl<S, A> UnwindSafe for Pattern<S, A>where A: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.