ScrollingWindowPatternMatcher
A high-performance pattern matching library for Rust that processes streaming data with configurable window sizes and custom data extractors. This library allows you to create complex patterns that match against sequences of data, with powerful extractor functions that can modify matching behavior at runtime.
๐จ Major Version 2.0 Simplification
This is a major simplification from previous versions with breaking API changes. The dual architecture has been unified into a single Matcher
that supports both simple pattern matching and advanced stateful operations with context.
โจ Features
- Unified Architecture - Single
Matcher
type that handles all pattern matching scenarios - Context Support - Optional context for stateful operations and data capture
- Function-based Patterns - Custom matching logic with closures and predicates
- Stateful Extraction - Access and modify context during pattern matching
- Settings-based Configuration - Clean configuration for element behavior
- Rich Pattern Elements - Exact matches, predicates, and range matching
- Flexible Extraction - Extract, continue, or restart pattern matching flow
- Comprehensive Error Handling - Detailed error types with proper error propagation
- Memory Safe - Zero-copy operations where possible with Rust's ownership guarantees
๐ Quick Start
Basic Pattern Matching
use ;
// Create a matcher with a window size
let mut matcher = new;
// Add patterns to find sequence 1, 2, 3
matcher.add_pattern;
matcher.add_pattern;
matcher.add_pattern;
// Process data items
assert_eq!;
assert_eq!;
assert_eq!; // Pattern complete!
Function-based Pattern Matching
use ;
// Create a matcher with window size 10
let mut matcher = new;
// Add patterns to find even numbers followed by odd numbers
matcher.add_pattern;
matcher.add_pattern;
// Process data
assert_eq!; // Even number matches first pattern
assert_eq!; // Odd number completes the pattern
Range-based Pattern Matching
use ;
// Create a matcher
let mut matcher = new;
// Add patterns to find values in ranges
matcher.add_pattern; // 1 <= x <= 5
matcher.add_pattern; // 10 <= x <= 15
// Process data
assert_eq!; // First range matches
assert_eq!; // Second range completes pattern
Stateful Pattern Matching with Context
use ;
// Define a context to capture matched values
// Create a matcher with context
let mut matcher = new;
// Set up context
let context = MyContext ;
matcher.set_context;
// Register an extractor that captures values
matcher.register_extractor;
// Create a pattern with an extractor
let mut settings = default;
settings.extractor_id = Some;
matcher.add_pattern;
// Process an item - the extractor will double it
assert_eq!;
๐๏ธ Pattern Elements
The library supports multiple types of pattern elements:
Exact Match Elements
use ;
// Simple exact match
let element = exact;
// Exact match with settings and extractor
let mut settings = default;
settings.extractor_id = Some;
settings.optional = true;
let element = exact_with_settings;
Predicate Elements
use PatternElement;
// Simple predicate
let element = predicate;
// Predicate with settings
let mut settings = default;
settings.max_retries = 3;
let element = predicate_with_settings;
Range Elements
use PatternElement;
// Simple range (inclusive)
let element = range;
// Range with settings
let mut settings = default;
settings.timeout_ms = Some;
let element = range_with_settings;
โ๏ธ Element Settings
Configure pattern element behavior with ElementSettings
:
use ElementSettings;
let mut settings = default;
settings.max_retries = 3; // Retry failed matches
settings.optional = true; // Element is optional in pattern
settings.timeout_ms = Some; // Timeout for this element
settings.extractor_id = Some; // Associated extractor ID
// Context can be added too
settings.context = Some;
๐ Extractors
Extractors allow you to modify the matching flow and extract custom data:
ExtractorAction Types
use ExtractorAction;
// Continue normal pattern matching
Continue
// Extract data and complete the pattern
Extract
// Restart pattern matching from the beginning
Restart
Registering Extractors
// Register an extractor function with an ID
matcher.register_extractor;
// The MatchState provides information about the current match
// - state.current_item: The item being processed
// - state.position: Position in the current pattern
// - state.total_processed: Total items processed so far
๐ Matcher API
Core Methods
// Create a new matcher
let mut matcher = new;
// Add pattern elements
matcher.add_pattern;
// Process single items
let result = matcher.process_item?;
// Process multiple items
let results = matcher.process_items?;
// Reset matcher state
matcher.reset;
// Register extractors
matcher.register_extractor;
// Context management
matcher.set_context;
let context_ref = matcher.context;
State Inspection
// Check current matching state
let position = matcher.current_position;
let total = matcher.total_processed;
let count = matcher.pattern_count;
let is_matching = matcher.is_matching;
// Window size management
let size = matcher.window_size;
matcher.set_window_size;
๐งช Testing
Run the test suite:
Run tests with output:
Run specific tests:
๐ Examples
See the built-in tests for comprehensive examples:
tests::test_exact_match
- Basic exact value matchingtests::test_predicate_match
- Function-based pattern matchingtests::test_range_match
- Range-based pattern matchingtests::test_extractor
- Custom data extractiontests::test_context
- Context managementtests::test_reset
- State reset functionality
๐ฆ Error Handling
The library provides comprehensive error handling:
use ;
match matcher.process_item
๐ง Advanced Usage
Custom Context Types
let mut matcher = new;
Complex Extractors
matcher.register_extractor;
Pattern Composition
// Build complex patterns step by step
matcher.add_pattern;
matcher.add_pattern;
matcher.add_pattern;
// Or with settings
let mut settings = default;
settings.optional = true;
matcher.add_pattern;
๐ Performance
The library is designed for high-performance streaming data processing:
- Zero-copy operations where possible
- Minimal allocations during pattern matching
- Efficient state management with small memory footprint
- Configurable window sizes to control memory usage
- Async-friendly design (no blocking operations)
๐ API Reference
Types
Matcher<T, Context>
- Main pattern matcher with optional contextPatternElement<T, Context>
- Individual pattern elementsElementSettings<Context>
- Configuration for pattern elementsMatchState<T>
- Current state information for extractorsExtractorAction<T>
- Actions that extractors can returnMatcherError
- Error types for matcher operationsExtractorError
- Error types for extractor operations
Key Traits
All generic types must implement:
T: Clone + PartialEq + fmt::Debug + PartialOrd
(for pattern matching)Context: Clone + fmt::Debug
(for context operations)
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
๐ License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.