ScrollingWindowPatternMatcher
A flexible pattern matching library for Rust with an advanced extractor system for dynamic behavior modification. 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 Rewrite
This is a complete rewrite from version 1.x with breaking API changes. The previous field-based pattern syntax has been replaced with a settings-based approach, and the callback system has been replaced with a more powerful extractor architecture.
Migration from 1.x
The API has fundamentally changed. Please see the examples and documentation below for the new approach.
✨ Features
- Extractor-driven architecture - Dynamic modification of matching behavior through extractor functions
- Settings-based configuration - Clean builder pattern for pattern element configuration
- Rich pattern elements - Values, functions, pattern references, wildcards, and nested repeats
- Advanced extractor actions - Continue, Skip, Jump, pattern manipulation, and flow control
- Comprehensive error handling - Detailed error types with proper error propagation
- Zero-copy when possible - Efficient matching with minimal allocations
- Priority-based matching - Control pattern matching order with priority settings
🚀 Quick Start
use ;
// Create a matcher
let mut matcher = new;
// Add a pattern to find the value 42
matcher.add_pattern;
// Match against data
let data = vec!;
let result = matcher.run;
assert!;
🏗️ Pattern Elements
Value
Matches a specific value:
Value
Function
Matches using custom logic:
Function
Any
Wildcard matching:
Any
Pattern Reference
References another named pattern:
Pattern
Repeat
Repeats a nested pattern element:
Repeat
⚡ Extractor System
The extractor system is the core feature that allows dynamic modification of matching behavior:
Basic Extractor
use ;
let extractor = Box new;
let pattern = vec!;
Extractor Actions
Continue
Continue normal matching:
Ok
Skip
Skip ahead by N positions:
Ok // Skip 3 positions ahead
Jump
Jump relative to current position:
Ok // Jump back 2 positions
Ok // Jump forward 5 positions
Pattern Manipulation
Add or remove patterns dynamically:
// Add a new pattern
Ok
// Remove a pattern
Ok
Flow Control
Ok // Stop all matching
Ok // Discard current match
Ok // Restart from specific position
Match State Information
Extractors receive rich context information:
let extractor = Box new;
🔧 Complex Examples
Multi-element Pattern
// Pattern: value 1, then any item, then value 3
matcher.add_pattern;
Advanced Extractor Usage
use ;
// Extractor that logs matches and skips ahead
let logging_extractor = Box new;
// Extractor that adds new patterns based on matched values
let dynamic_extractor = Box new;
Priority-based Matching
// Higher priority pattern (lower number = higher priority)
matcher.add_pattern;
// Lower priority pattern
matcher.add_pattern;
🌍 Real-World Examples
Log Analysis
// Detect HTTP error codes
let error_detector = Box new;
matcher.add_pattern;
Network Traffic Analysis
// Detect potential port scanning (rapid consecutive port accesses)
let scan_detector = Box new;
matcher.add_pattern;
🚨 Error Handling
The library provides comprehensive error handling:
use ;
match matcher.run
📚 API Reference
Core Types
Matcher<T>
- Main matcher structPatternElement<T>
- Individual pattern elements (Value, Function, Any, Pattern, Repeat)ElementSettings<T>
- Configuration for pattern elementsPatternSettings<T>
- Configuration for entire patternsExtractorAction<T>
- Actions that extractors can returnMatchState<T>
- Context information provided to extractors
Matcher Methods
new()
- Create a new matcherwith_settings(settings)
- Create matcher with custom settingsadd_pattern(name, elements)
- Add a patternadd_pattern_with_settings(name, pattern)
- Add pattern with custom settingsremove_pattern(name)
- Remove a patternrun(data)
- Execute matching on data slice
Settings Builders
ElementSettings::new()
- Create element settings builderPatternSettings::new()
- Create pattern settings builder
Builder methods: .min_repeat()
, .max_repeat()
, .greedy()
, .priority()
, .extractor()
🎯 Design Philosophy
This library prioritizes flexibility and extensibility through:
- Extractor-driven architecture - Extractors provide unlimited customization capabilities
- Settings-based configuration - Clean, discoverable API through builder patterns
- Type safety - Rust's type system prevents common pattern matching errors
- Error transparency - Comprehensive error types with detailed context
- Performance awareness - Efficient algorithms with minimal allocations where possible
📝 License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
🔄 Breaking Changes from 1.x
- Complete API rewrite with settings-based configuration
- Callback system replaced with extractor architecture
match_window()
replaced withrun()
method- Field-based pattern syntax replaced with settings builders
- Return type changed from
HashMap<String, Vec<T>>
toResult<(), MatcherError>
- Pattern matching results now handled through extractors rather than return values