From 4684713d7e37f7cfae985437bda78d561b3983c5 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 22 Feb 2022 09:38:31 +0530 Subject: [PATCH] refactor: implement `Match` for `MatcherType` Delegate the responsibility of handling different `MatcherType`s to the config module. --- src/config.rs | 9 +++++++++ src/main.rs | 15 ++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 90e0cb8..7bc7568 100644 --- a/src/config.rs +++ b/src/config.rs @@ -83,6 +83,15 @@ impl Match for Matcher<'_> { } } +impl Match for MatcherType<'_> { + fn check_match(&self, string: &str) -> bool { + match self { + Self::Single(matcher) => matcher.check_match(string), + Self::Multiple(matchers) => matchers.iter().all(|matcher| matcher.check_match(string)), + } + } +} + impl Act for Action<'_> { fn apply_action(&self, input: &str) -> String { return match self { diff --git a/src/main.rs b/src/main.rs index 0ca9597..0dcf99c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use clipboard::{ClipboardContext, ClipboardProvider}; use dirs::config_dir; use log::{debug, error}; -use crate::config::{Act, Match, MatcherType, Replacements}; +use crate::config::{Act, Match, Replacements}; const VERSION_ARGS: [&str; 3] = ["version", "-v", "--version"]; @@ -57,14 +57,11 @@ fn loop_clipboard<'a>(config: Replacements<'a>) { ClipboardProvider::new().expect("Failed to get clipboard"); let mut clipboard_contents = get_clipboard_contents(&mut clipboard); while let Ok(contents) = clipboard_contents.as_deref() { - if let Some(subst) = config.substitutors.iter().find(|subst| { - return match &subst.matcher { - MatcherType::Single(matcher) => matcher.check_match(contents), - MatcherType::Multiple(matchers) => { - matchers.iter().all(|matcher| matcher.check_match(contents)) - } - }; - }) { + if let Some(subst) = config + .substitutors + .iter() + .find(|subst| subst.matcher.check_match(contents)) + { if subst.name.is_empty().not() { debug!("{}: matched on {}...", &subst.name, truncate(&contents, 40)); }