2019-08-31 14:07:45 +00:00
|
|
|
use crate::matcher::{Match, MatchReceiver};
|
|
|
|
|
|
|
|
pub struct ScrollingMatcher<'a>{
|
2019-08-31 15:00:23 +00:00
|
|
|
matches: &'a Vec<Match>,
|
2019-09-01 18:46:46 +00:00
|
|
|
receiver: &'a dyn MatchReceiver,
|
2019-08-31 15:00:23 +00:00
|
|
|
current_set: Vec<MatchEntry<'a>>
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MatchEntry<'a> {
|
|
|
|
remaining: &'a str,
|
|
|
|
_match: &'a Match
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> super::Matcher for ScrollingMatcher<'a> {
|
2019-08-31 15:00:23 +00:00
|
|
|
fn handle_char(&mut self, c: char) {
|
|
|
|
let mut new_matches: Vec<MatchEntry> = self.matches.iter()
|
|
|
|
.filter(|&x| x.trigger.chars().nth(0).unwrap() == c)
|
|
|
|
.map(|x | MatchEntry{remaining: &x.trigger[1..], _match: &x})
|
|
|
|
.collect();
|
|
|
|
// TODO: use an associative structure to improve the efficiency of this first "new_matches" lookup.
|
|
|
|
|
|
|
|
let old_matches = self.current_set.iter()
|
|
|
|
.filter(|&x| x.remaining.chars().nth(0).unwrap() == c)
|
|
|
|
.map(|x | MatchEntry{remaining: &x.remaining[1..], _match: &x._match})
|
|
|
|
.collect();
|
2019-09-01 20:00:31 +00:00
|
|
|
|
2019-08-31 15:00:23 +00:00
|
|
|
self.current_set = old_matches;
|
|
|
|
self.current_set.append(&mut new_matches);
|
|
|
|
|
2019-09-01 18:46:46 +00:00
|
|
|
let mut found_match = None;
|
2019-08-31 15:00:23 +00:00
|
|
|
|
|
|
|
for entry in self.current_set.iter_mut() {
|
|
|
|
if entry.remaining.len() == 0 {
|
2019-09-01 18:46:46 +00:00
|
|
|
found_match = Some(entry._match);
|
2019-08-31 15:00:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 14:07:45 +00:00
|
|
|
|
2019-09-01 18:46:46 +00:00
|
|
|
if let Some(_match) = found_match {
|
2019-08-31 15:00:23 +00:00
|
|
|
self.current_set.clear();
|
|
|
|
self.receiver.on_match(_match);
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> ScrollingMatcher<'a> {
|
2019-09-01 18:46:46 +00:00
|
|
|
pub fn new(matches:&'a Vec<Match>, receiver: &'a dyn MatchReceiver) -> ScrollingMatcher<'a> {
|
2019-08-31 15:00:23 +00:00
|
|
|
let current_set = Vec::new();
|
|
|
|
ScrollingMatcher{ matches, receiver, current_set }
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
}
|