espanso/src/matcher/mod.rs

133 lines
3.1 KiB
Rust
Raw Normal View History

use serde::{Serialize, Deserialize, Deserializer};
2019-09-12 20:14:41 +00:00
use crate::event::{KeyEvent, KeyModifier};
use crate::event::KeyEventReceiver;
use serde_yaml::Mapping;
use regex::Regex;
2019-08-30 19:24:03 +00:00
pub(crate) mod scrolling;
#[derive(Debug, Serialize, Clone)]
2019-08-30 19:24:03 +00:00
pub struct Match {
pub trigger: String,
pub replace: String,
pub vars: Vec<MatchVariable>,
#[serde(skip_serializing)]
pub _has_vars: bool,
}
impl <'de> serde::Deserialize<'de> for Match {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de> {
let auto_match = AutoMatch::deserialize(deserializer)?;
Ok(Match::from(&auto_match))
}
}
impl<'a> From<&'a AutoMatch> for Match{
fn from(other: &'a AutoMatch) -> Self {
lazy_static! {
static ref VarRegex: Regex = Regex::new("\\{\\{\\s*(\\w+)\\s*\\}\\}").unwrap();
}
// Check if the match contains variables
let has_vars = VarRegex.is_match(&other.replace);
Self {
trigger: other.trigger.clone(),
replace: other.replace.clone(),
vars: other.vars.clone(),
_has_vars: has_vars,
}
}
}
/// Used to deserialize the Match struct before applying some custom elaboration.
#[derive(Debug, Serialize, Deserialize, Clone)]
struct AutoMatch {
pub trigger: String,
pub replace: String,
#[serde(default = "default_vars")]
pub vars: Vec<MatchVariable>,
}
fn default_vars() -> Vec<MatchVariable> {Vec::new()}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MatchVariable {
pub name: String,
#[serde(rename = "type")]
pub var_type: String,
pub params: Mapping,
2019-08-30 19:24:03 +00:00
}
pub trait MatchReceiver {
2019-08-31 15:00:23 +00:00
fn on_match(&self, m: &Match);
2019-09-14 18:13:09 +00:00
fn on_enable_update(&self, status: bool);
2019-08-30 19:24:03 +00:00
}
2019-09-12 20:14:41 +00:00
pub trait Matcher : KeyEventReceiver {
2019-09-07 14:13:13 +00:00
fn handle_char(&self, c: char);
fn handle_modifier(&self, m: KeyModifier);
2019-09-12 20:14:41 +00:00
}
impl <M: Matcher> KeyEventReceiver for M {
fn on_key_event(&self, e: KeyEvent) {
match e {
KeyEvent::Char(c) => {
self.handle_char(c);
},
KeyEvent::Modifier(m) => {
self.handle_modifier(m);
},
2019-08-30 19:24:03 +00:00
}
}
2019-09-15 13:19:11 +00:00
}
// TESTS
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_match_has_vars_should_be_false() {
let match_str = r###"
trigger: ":test"
replace: "There are no variables"
"###;
let _match : Match = serde_yaml::from_str(match_str).unwrap();
assert_eq!(_match._has_vars, false);
}
#[test]
fn test_match_has_vars_should_be_true() {
let match_str = r###"
trigger: ":test"
replace: "There are {{one}} and {{two}} variables"
"###;
let _match : Match = serde_yaml::from_str(match_str).unwrap();
assert_eq!(_match._has_vars, true);
}
#[test]
fn test_match_has_vars_with_spaces_should_be_true() {
let match_str = r###"
trigger: ":test"
replace: "There is {{ one }} variable"
"###;
let _match : Match = serde_yaml::from_str(match_str).unwrap();
assert_eq!(_match._has_vars, true);
}
2019-08-30 19:24:03 +00:00
}