Module all_patterns

Source
Expand description

Sequences of subpatterns can be matched using an all-pattern. In hitori syntax it is represented as a tuple of its subpatterns.

/// "hello"
pub struct Hello;

#[hitori::impl_expr]
impl Expr<usize, char> for Hello {
    const PATTERN: _ = (
        |ch| ch == 'h',
        |ch| ch == 'e',
        |ch| ch == 'l',
        |ch| ch == 'l',
        |ch| ch == 'o',
    );
}

assert!(hitori::string::starts_with(Hello, "hello").is_some());
assert!(hitori::string::starts_with(Hello, "world").is_none());

equivalent to hello in regex syntax

§Trailing comma

The only way to apply attributes such as #[hitori::capture] or #[hitori::repeat] to a single character test is by wrapping it inside of an all-pattern. In that case trailing comma is not optional.

/// Numeric-only password with up to 8 characters
pub struct BadPassword;

#[hitori::impl_expr]
impl Expr<usize, char> for BadPassword {
    const PATTERN: _ = #[hitori::repeat(gt = 0, le = 8)]
    (|ch: char| ch.is_ascii_digit(),); // removing a comma in this line won't compile
}

assert!(hitori::string::starts_with(BadPassword, "12345").is_some());
assert!(hitori::string::starts_with(BadPassword, "cUFK^06#43Gs").is_none());

equivalent to \d{1, 8} in regex syntax

§Empty all-pattern

An empty all-pattern is always true.

/// An empty all-pattern
pub struct True;

#[hitori::impl_expr]
impl Expr<usize, char> for True {
    const PATTERN: _ = ();
}

for s in ["Hello, world!", "34", "hitori"] {
    assert!(hitori::string::starts_with(True, s).is_some());
}

Structs§

BadPassword
Numeric-only password with up to 8 characters
BadPasswordCapture
This is an empty placeholder-struct
Hello
“hello”
HelloCapture
This is an empty placeholder-struct
True
An empty all-pattern
TrueCapture
This is an empty placeholder-struct