Crate scrolling_window_pattern_matcher

Crate scrolling_window_pattern_matcher 

Source
Expand description

§Scrolling Window Pattern Matcher

A high-performance, unified pattern matching library for Rust that processes streaming data with configurable window sizes and custom data extractors. This library provides a single Matcher type that handles both simple pattern matching and advanced stateful operations with optional context management.

§Features

  • Unified Architecture - Single Matcher<T, Context> type for all pattern matching scenarios
  • Context Support - Optional context parameter for stateful operations and data capture
  • Pattern Elements - Exact matching, predicate functions, and range matching
  • Extractor System - Powerful extractors that can continue, extract, or restart pattern matching
  • Optional Elements - Flexible patterns with optional components
  • Error Handling - Comprehensive error types with proper propagation
  • Memory Safe - Zero-copy operations where possible with Rust’s ownership guarantees

§Quick Start

use scrolling_window_pattern_matcher::{Matcher, PatternElement};

// Create a matcher with window size 10
let mut matcher = Matcher::<i32, ()>::new(10);

// Add patterns to find sequence 1, 2, 3
matcher.add_pattern(PatternElement::exact(1));
matcher.add_pattern(PatternElement::exact(2));
matcher.add_pattern(PatternElement::exact(3));

// Process data items
assert_eq!(matcher.process_item(1).unwrap(), None);
assert_eq!(matcher.process_item(2).unwrap(), None);
assert_eq!(matcher.process_item(3).unwrap(), Some(3)); // Pattern complete!

§Pattern Elements

The library supports three main types of pattern elements:

  • Exact Match: PatternElement::exact(value) - matches specific values
  • Predicate: PatternElement::predicate(|x| condition) - matches based on custom logic
  • Range: PatternElement::range(min, max) - matches values within inclusive range

Each pattern element can be configured with ElementSettings for advanced behavior including optional elements and custom extractors.

§Extractors

Extractors allow you to modify the matching flow and extract custom data:

use scrolling_window_pattern_matcher::{Matcher, PatternElement, ElementSettings, ExtractorAction};

let mut matcher = Matcher::<i32, ()>::new(10);

// Register an extractor that doubles the matched value
matcher.register_extractor(1, |state| {
    Ok(ExtractorAction::Extract(state.current_item * 2))
});

let mut settings = ElementSettings::default();
settings.extractor_id = Some(1);
matcher.add_pattern(PatternElement::exact_with_settings(5, settings));

assert_eq!(matcher.process_item(5).unwrap(), Some(10)); // 5 * 2 = 10

Structs§

ElementSettings
Configuration settings for pattern elements.
MatchState
Represents the current state during pattern matching.
Matcher
The main pattern matcher that processes streaming data.

Enums§

ExtractorAction
Action to take after an extractor runs.
ExtractorError
Error types for extractors.
MatchResult
Represents the result of running a pattern element.
MatcherError
Error types for the pattern matcher.
PatternElement
A pattern element that can match against items of type T.

Type Aliases§

Extractor
Type alias for extractor functions.
ExtractorId