feat(config): add regex option

This commit is contained in:
Federico Terzi 2021-05-06 21:21:43 +02:00
parent bfe6b13ae7
commit 745c329580
4 changed files with 23 additions and 3 deletions

View File

@ -491,7 +491,7 @@ fn triggers_for_match(m: &Value) -> Vec<String> {
} else if let Some(trigger) = m.get("trigger").and_then(|v| v.as_str()) {
vec![trigger.to_string()]
} else {
panic!("Match does not have any trigger defined: {:?}", m)
vec![]
}
}

View File

@ -17,7 +17,7 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{counter::next_id, matches::{ImageEffect, Match, Params, TextFormat, UpperCasingStyle, Value, Variable, group::{path::resolve_imports, MatchGroup}}};
use crate::{counter::next_id, matches::{ImageEffect, Match, Params, RegexCause, TextFormat, UpperCasingStyle, Value, Variable, group::{path::resolve_imports, MatchGroup}}};
use anyhow::Result;
use log::{error, warn};
use parse::YAMLMatchGroup;
@ -137,6 +137,10 @@ impl TryFrom<YAMLMatch> for Match {
.unwrap_or(TriggerCause::default().propagate_case),
uppercase_style,
})
} else if let Some(regex) = yaml_match.regex { // TODO: add test case
MatchCause::Regex(RegexCause {
regex,
})
} else {
MatchCause::None
};

View File

@ -64,6 +64,9 @@ pub struct YAMLMatch {
#[serde(default)]
pub triggers: Option<Vec<String>>,
#[serde(default)]
pub regex: Option<String>,
#[serde(default)]
pub replace: Option<String>,

View File

@ -54,7 +54,7 @@ impl Default for Match {
pub enum MatchCause {
None,
Trigger(TriggerCause),
// TODO: regex
Regex(RegexCause),
// TODO: shortcut
}
@ -88,6 +88,19 @@ pub enum UpperCasingStyle {
CapitalizeWords,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RegexCause {
pub regex: String,
}
impl Default for RegexCause {
fn default() -> Self {
Self {
regex: String::new(),
}
}
}
// Effects
#[derive(Debug, Clone, PartialEq, Eq, Hash, EnumAsInner)]