Format code

This commit is contained in:
Federico Terzi 2021-03-13 13:45:52 +01:00
parent beab299aa0
commit 2a2fbbd792
6 changed files with 229 additions and 88 deletions

View File

@ -19,10 +19,7 @@
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Key {
key: Key,
chars: Option<String>
},
Key { key: Key, chars: Option<String> },
VirtualSeparator,
}
@ -81,4 +78,4 @@ pub enum Key {
// Others
Other,
}
}

View File

@ -22,8 +22,8 @@ use std::collections::HashMap;
use log::error;
use regex::{Regex, RegexSet};
use crate::{MatchResult, event::Event};
use crate::Matcher;
use crate::{event::Event, MatchResult};
#[derive(Debug)]
pub struct RegexMatch<Id> {
@ -92,7 +92,6 @@ where
if let Some(captures) = regex.captures(&buffer) {
let full_match = captures.get(0).map_or("", |m| m.as_str());
if !full_match.is_empty() {
// Now extract the captured names as variables
let variables: HashMap<String, String> = regex
.capture_names()
@ -110,12 +109,15 @@ where
}
}
} else {
error!("received inconsistent index from regex set with index: {}", index);
error!(
"received inconsistent index from regex set with index: {}",
index
);
}
}
if !matches.is_empty() {
return (RegexMatcherState::default(), matches)
return (RegexMatcherState::default(), matches);
}
}
@ -144,8 +146,12 @@ impl<Id: Clone> RegexMatcher<Id> {
}
let regex_set = RegexSet::new(&good_regexes).expect("unable to build regex set");
Self { ids, regex_set, regexes }
Self {
ids,
regex_set,
regexes,
}
}
}
@ -155,9 +161,10 @@ mod tests {
use crate::util::tests::get_matches_after_str;
fn match_result<Id: Default>(id: Id, trigger: &str, vars: &[(&str, &str)]) -> MatchResult<Id> {
let vars: HashMap<String, String> = vars.iter().map(|(key, value)| {
(key.to_string(), value.to_string())
}).collect();
let vars: HashMap<String, String> = vars
.iter()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect();
MatchResult {
id,
@ -173,10 +180,22 @@ mod tests {
RegexMatch::new(2, "num\\d{1,3}s"),
]);
assert_eq!(get_matches_after_str("hi", &matcher), vec![]);
assert_eq!(get_matches_after_str("hello", &matcher), vec![match_result(1, "hello", &[])]);
assert_eq!(get_matches_after_str("say hello", &matcher), vec![match_result(1, "hello", &[])]);
assert_eq!(get_matches_after_str("num1s", &matcher), vec![match_result(2, "num1s", &[])]);
assert_eq!(get_matches_after_str("num134s", &matcher), vec![match_result(2, "num134s", &[])]);
assert_eq!(
get_matches_after_str("hello", &matcher),
vec![match_result(1, "hello", &[])]
);
assert_eq!(
get_matches_after_str("say hello", &matcher),
vec![match_result(1, "hello", &[])]
);
assert_eq!(
get_matches_after_str("num1s", &matcher),
vec![match_result(2, "num1s", &[])]
);
assert_eq!(
get_matches_after_str("num134s", &matcher),
vec![match_result(2, "num134s", &[])]
);
assert_eq!(get_matches_after_str("nums", &matcher), vec![]);
}
@ -187,8 +206,18 @@ mod tests {
RegexMatch::new(2, "multi\\((?P<name1>.*?),(?P<name2>.*?)\\)"),
]);
assert_eq!(get_matches_after_str("hi", &matcher), vec![]);
assert_eq!(get_matches_after_str("say hello(mary)", &matcher), vec![match_result(1, "hello(mary)", &[("name", "mary")])]);
assert_eq!(
get_matches_after_str("say hello(mary)", &matcher),
vec![match_result(1, "hello(mary)", &[("name", "mary")])]
);
assert_eq!(get_matches_after_str("hello(mary", &matcher), vec![]);
assert_eq!(get_matches_after_str("multi(mary,jane)", &matcher), vec![match_result(2, "multi(mary,jane)", &[("name1", "mary"), ("name2", "jane")])]);
assert_eq!(
get_matches_after_str("multi(mary,jane)", &matcher),
vec![match_result(
2,
"multi(mary,jane)",
&[("name1", "mary"), ("name2", "jane")]
)]
);
}
}
}

View File

@ -19,9 +19,16 @@
use std::collections::HashMap;
use super::{RollingMatch, tree::{MatcherTreeNode, MatcherTreeRef}, util::extract_string_from_events};
use crate::{MatchResult, event::{Event, Key}};
use super::{
tree::{MatcherTreeNode, MatcherTreeRef},
util::extract_string_from_events,
RollingMatch,
};
use crate::Matcher;
use crate::{
event::{Event, Key},
MatchResult,
};
use unicase::UniCase;
#[derive(Clone)]
@ -76,19 +83,26 @@ where
// First compute the old refs
if let Some(prev_state) = prev_state {
for node_path in prev_state.paths.iter() {
next_refs.extend(self.find_refs(node_path.node, &event).into_iter().map(|node_ref| {
let mut new_events = node_path.events.clone();
new_events.push(event.clone());
(node_ref, new_events)
}));
next_refs.extend(
self
.find_refs(node_path.node, &event)
.into_iter()
.map(|node_ref| {
let mut new_events = node_path.events.clone();
new_events.push(event.clone());
(node_ref, new_events)
}),
);
}
}
// Calculate new ones
let root_refs = self.find_refs(&self.root, &event);
next_refs.extend(root_refs.into_iter().map(|node_ref| {
(node_ref, vec![event.clone()])
}));
next_refs.extend(
root_refs
.into_iter()
.map(|node_ref| (node_ref, vec![event.clone()])),
);
let mut next_paths = Vec::new();
@ -96,13 +110,14 @@ where
match node_ref {
MatcherTreeRef::Matches(matches) => {
let trigger = extract_string_from_events(&events);
let results = matches.iter().map(|id| {
MatchResult {
id: id.clone(),
trigger: trigger.clone(),
let results = matches
.iter()
.map(|id| MatchResult {
id: id.clone(),
trigger: trigger.clone(),
vars: HashMap::new(),
}
}).collect();
})
.collect();
// Reset the state and return the matches
return (RollingMatcherState::default(), results);
@ -191,7 +206,7 @@ impl<Id: Clone> RollingMatcher<Id> {
#[cfg(test)]
mod tests {
use super::*;
use crate::rolling::{StringMatchOptions};
use crate::rolling::StringMatchOptions;
use crate::util::tests::get_matches_after_str;
fn match_result<Id: Default>(id: Id, trigger: &str) -> MatchResult<Id> {
@ -217,9 +232,18 @@ mod tests {
},
);
assert_eq!(get_matches_after_str("hi", &matcher), vec![match_result(1, "hi"), match_result(5, "hi")]);
assert_eq!(get_matches_after_str("my", &matcher), vec![match_result(3, "my")]);
assert_eq!(get_matches_after_str("mmy", &matcher), vec![match_result(3, "my")]);
assert_eq!(
get_matches_after_str("hi", &matcher),
vec![match_result(1, "hi"), match_result(5, "hi")]
);
assert_eq!(
get_matches_after_str("my", &matcher),
vec![match_result(3, "my")]
);
assert_eq!(
get_matches_after_str("mmy", &matcher),
vec![match_result(3, "my")]
);
assert_eq!(get_matches_after_str("invalid", &matcher), vec![]);
}
@ -245,7 +269,10 @@ mod tests {
);
assert_eq!(get_matches_after_str("hi", &matcher), vec![]);
assert_eq!(get_matches_after_str(".hi,", &matcher), vec![match_result(1, ".hi,")]);
assert_eq!(
get_matches_after_str(".hi,", &matcher),
vec![match_result(1, ".hi,")]
);
}
#[test]
@ -277,11 +304,26 @@ mod tests {
},
);
assert_eq!(get_matches_after_str("hi", &matcher), vec![match_result(1, "hi")]);
assert_eq!(get_matches_after_str("Hi", &matcher), vec![match_result(1, "Hi")]);
assert_eq!(get_matches_after_str("HI", &matcher), vec![match_result(1, "HI")]);
assert_eq!(get_matches_after_str("arty", &matcher), vec![match_result(3, "arty")]);
assert_eq!(get_matches_after_str("arTY", &matcher), vec![match_result(3, "arTY")]);
assert_eq!(
get_matches_after_str("hi", &matcher),
vec![match_result(1, "hi")]
);
assert_eq!(
get_matches_after_str("Hi", &matcher),
vec![match_result(1, "Hi")]
);
assert_eq!(
get_matches_after_str("HI", &matcher),
vec![match_result(1, "HI")]
);
assert_eq!(
get_matches_after_str("arty", &matcher),
vec![match_result(3, "arty")]
);
assert_eq!(
get_matches_after_str("arTY", &matcher),
vec![match_result(3, "arTY")]
);
assert_eq!(get_matches_after_str("ARTY", &matcher), vec![]);
}
}

View File

@ -119,10 +119,14 @@ mod tests {
#[test]
fn test_match_from_string_left_word() {
assert_eq!(
RollingMatch::from_string(1, "test", &StringMatchOptions {
left_word: true,
..Default::default()
}),
RollingMatch::from_string(
1,
"test",
&StringMatchOptions {
left_word: true,
..Default::default()
}
),
RollingMatch {
id: 1,
items: vec![
@ -139,10 +143,14 @@ mod tests {
#[test]
fn test_match_from_string_right_word() {
assert_eq!(
RollingMatch::from_string(1, "test", &StringMatchOptions {
right_word: true,
..Default::default()
}),
RollingMatch::from_string(
1,
"test",
&StringMatchOptions {
right_word: true,
..Default::default()
}
),
RollingMatch {
id: 1,
items: vec![
@ -159,10 +167,14 @@ mod tests {
#[test]
fn test_match_from_string_case_insensitive() {
assert_eq!(
RollingMatch::from_string(1, "test", &StringMatchOptions {
case_insensitive: true,
..Default::default()
}),
RollingMatch::from_string(
1,
"test",
&StringMatchOptions {
case_insensitive: true,
..Default::default()
}
),
RollingMatch {
id: 1,
items: vec![
@ -178,11 +190,15 @@ mod tests {
#[test]
fn test_match_from_string_preserve_case_markers() {
assert_eq!(
RollingMatch::from_string(1, "test", &StringMatchOptions {
case_insensitive: true,
preserve_case_markers: true,
..Default::default()
}),
RollingMatch::from_string(
1,
"test",
&StringMatchOptions {
case_insensitive: true,
preserve_case_markers: true,
..Default::default()
}
),
RollingMatch {
id: 1,
items: vec![

View File

@ -41,33 +41,84 @@ mod tests {
#[test]
fn extract_string_from_events_all_chars() {
assert_eq!(extract_string_from_events(&[
Event::Key { key: Key::Other, chars: Some("h".to_string()) },
Event::Key { key: Key::Other, chars: Some("e".to_string()) },
Event::Key { key: Key::Other, chars: Some("l".to_string()) },
Event::Key { key: Key::Other, chars: Some("l".to_string()) },
Event::Key { key: Key::Other, chars: Some("o".to_string()) },
]), "hello");
assert_eq!(
extract_string_from_events(&[
Event::Key {
key: Key::Other,
chars: Some("h".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("e".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("l".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("l".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("o".to_string())
},
]),
"hello"
);
}
#[test]
fn extract_string_from_events_no_chars() {
assert_eq!(extract_string_from_events(&[
Event::Key { key: Key::ArrowUp, chars: None },
Event::Key { key: Key::ArrowUp, chars: None },
Event::Key { key: Key::ArrowUp, chars: None },
]), "");
assert_eq!(
extract_string_from_events(&[
Event::Key {
key: Key::ArrowUp,
chars: None
},
Event::Key {
key: Key::ArrowUp,
chars: None
},
Event::Key {
key: Key::ArrowUp,
chars: None
},
]),
""
);
}
#[test]
fn extract_string_from_events_mixed() {
assert_eq!(extract_string_from_events(&[
Event::Key { key: Key::Other, chars: Some("h".to_string()) },
Event::Key { key: Key::Other, chars: Some("e".to_string()) },
Event::Key { key: Key::Other, chars: Some("l".to_string()) },
Event::Key { key: Key::Other, chars: Some("l".to_string()) },
Event::Key { key: Key::Other, chars: Some("o".to_string()) },
Event::Key { key: Key::ArrowUp, chars: None },
]), "hello");
assert_eq!(
extract_string_from_events(&[
Event::Key {
key: Key::Other,
chars: Some("h".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("e".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("l".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("l".to_string())
},
Event::Key {
key: Key::Other,
chars: Some("o".to_string())
},
Event::Key {
key: Key::ArrowUp,
chars: None
},
]),
"hello"
);
}
}
}

View File

@ -19,9 +19,15 @@
#[cfg(test)]
pub(crate) mod tests {
use crate::{MatchResult, Matcher, event::{Event, Key}};
use crate::{
event::{Event, Key},
MatchResult, Matcher,
};
pub(crate) fn get_matches_after_str<'a, Id: Clone, S, M: Matcher<'a, S, Id>>(string: &str, matcher: &'a M) -> Vec<MatchResult<Id>> {
pub(crate) fn get_matches_after_str<'a, Id: Clone, S, M: Matcher<'a, S, Id>>(
string: &str,
matcher: &'a M,
) -> Vec<MatchResult<Id>> {
let mut prev_state = None;
let mut matches = Vec::new();
@ -40,4 +46,4 @@ pub(crate) mod tests {
matches
}
}
}