Pattern

Enum Pattern 

Source
pub enum Pattern {
    Literal(String),
    Regex(CompiledRegex),
    Glob(String),
    Eof,
    Timeout(Duration),
    Bytes(usize),
}
Expand description

A pattern that can be matched against terminal output.

Variants§

§

Literal(String)

Match an exact string.

§

Regex(CompiledRegex)

Match a regular expression.

§

Glob(String)

Match a glob pattern.

§

Eof

Match end of file (process terminated).

§

Timeout(Duration)

Match after a timeout.

§

Bytes(usize)

Match when N bytes have been received.

Implementations§

Source§

impl Pattern

Source

pub fn literal(s: impl Into<String>) -> Self

Create a literal pattern.

Source

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

Create a regex pattern.

§Errors

Returns an error if the regex pattern is invalid.

Source

pub fn glob(pattern: impl Into<String>) -> Self

Create a glob pattern.

Source

pub const fn eof() -> Self

Create an EOF pattern.

Source

pub const fn timeout(duration: Duration) -> Self

Create a timeout pattern.

Source

pub const fn bytes(n: usize) -> Self

Create a bytes pattern.

Source

pub fn as_str(&self) -> &str

Get the pattern as a string for display purposes.

Source

pub fn matches(&self, text: &str) -> Option<PatternMatch>

Check if this pattern matches the given text.

Returns the match position and captures if successful.

Source

pub const fn is_timeout(&self) -> bool

Check if this is a timeout pattern.

Source

pub const fn is_eof(&self) -> bool

Check if this is an EOF pattern.

Source

pub const fn timeout_duration(&self) -> Option<Duration>

Get the timeout duration if this is a timeout pattern.

Source

pub fn shell_prompt() -> Self

Create a pattern that matches common shell prompts.

Matches prompts ending with $, #, >, or % followed by optional whitespace. This handles most Unix shells (bash, zsh, sh) and root prompts.

§Examples
use rust_expect::Pattern;

let prompt = Pattern::shell_prompt();
assert!(prompt.matches("user@host:~$ ").is_some());
assert!(prompt.matches("root@host:~# ").is_some());
assert!(prompt.matches("> ").is_some());
Source

pub fn any_prompt() -> Self

Create a pattern that matches any common prompt character.

A simpler alternative to shell_prompt() that uses glob matching. Less precise but faster for simple cases.

Source

pub fn ipv4() -> Result<Self, Error>

Create a pattern that matches IPv4 addresses.

§Errors

Returns an error if the regex compilation fails (should not happen).

§Examples
use rust_expect::Pattern;

let ipv4 = Pattern::ipv4().unwrap();
assert!(ipv4.matches("Server IP: 192.168.1.1").is_some());
assert!(ipv4.matches("10.0.0.255 is local").is_some());
Source

pub fn email() -> Result<Self, Error>

Create a pattern that matches email addresses.

§Errors

Returns an error if the regex compilation fails (should not happen).

§Examples
use rust_expect::Pattern;

let email = Pattern::email().unwrap();
assert!(email.matches("Contact: user@example.com").is_some());
Source

pub fn timestamp_iso8601() -> Result<Self, Error>

Create a pattern that matches ISO 8601 timestamps.

Matches formats like 2024-01-15T10:30:00 or 2024-01-15 10:30:00.

§Errors

Returns an error if the regex compilation fails (should not happen).

Source

pub fn error_indicator() -> Self

Create a pattern that matches common error indicators.

Matches words like “error”, “failed”, “fatal” (case-insensitive).

§Examples
use rust_expect::Pattern;

let error = Pattern::error_indicator();
assert!(error.matches("Error: connection refused").is_some());
assert!(error.matches("Command FAILED").is_some());
Source

pub fn success_indicator() -> Self

Create a pattern that matches common success indicators.

Matches words like “success”, “passed”, “complete”, “ok” (case-insensitive).

Source

pub fn password_prompt() -> Self

Create a pattern that matches common password prompts.

Matches prompts like “Password:”, “password: “, “Passphrase:”.

§Examples
use rust_expect::Pattern;

let pwd = Pattern::password_prompt();
assert!(pwd.matches("Password: ").is_some());
assert!(pwd.matches("Enter passphrase: ").is_some());
Source

pub fn login_prompt() -> Self

Create a pattern that matches common login/username prompts.

Matches prompts like “login:”, “Username:”, “user: “.

Source

pub fn confirmation_prompt() -> Self

Create a pattern that matches common yes/no confirmation prompts.

Matches prompts like “[y/n]”, “(yes/no)”, “[Y/n]”.

Source

pub fn continue_prompt() -> Self

Create a pattern that matches common “continue?” prompts.

Matches prompts like “Continue?”, “Do you want to continue?”, “Press any key”.

Trait Implementations§

Source§

impl Clone for Pattern

Source§

fn clone(&self) -> Pattern

Returns a duplicate 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 Debug for Pattern

Source§

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

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

impl From<&str> for Pattern

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Pattern

Source§

fn from(s: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V