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
impl Pattern
Sourcepub fn matches(&self, text: &str) -> Option<PatternMatch>
pub fn matches(&self, text: &str) -> Option<PatternMatch>
Check if this pattern matches the given text.
Returns the match position and captures if successful.
Sourcepub const fn is_timeout(&self) -> bool
pub const fn is_timeout(&self) -> bool
Check if this is a timeout pattern.
Sourcepub const fn timeout_duration(&self) -> Option<Duration>
pub const fn timeout_duration(&self) -> Option<Duration>
Get the timeout duration if this is a timeout pattern.
Sourcepub fn shell_prompt() -> Self
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());Sourcepub fn any_prompt() -> Self
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.
Sourcepub fn ipv4() -> Result<Self, Error>
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());Sourcepub fn timestamp_iso8601() -> Result<Self, Error>
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).
Sourcepub fn error_indicator() -> Self
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());Sourcepub fn success_indicator() -> Self
pub fn success_indicator() -> Self
Create a pattern that matches common success indicators.
Matches words like “success”, “passed”, “complete”, “ok” (case-insensitive).
Sourcepub fn password_prompt() -> Self
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());Sourcepub fn login_prompt() -> Self
pub fn login_prompt() -> Self
Create a pattern that matches common login/username prompts.
Matches prompts like “login:”, “Username:”, “user: “.
Sourcepub fn confirmation_prompt() -> Self
pub fn confirmation_prompt() -> Self
Create a pattern that matches common yes/no confirmation prompts.
Matches prompts like “[y/n]”, “(yes/no)”, “[Y/n]”.
Sourcepub fn continue_prompt() -> Self
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”.