mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-14 21:17:01 +05:30
refactor: implement Match
for MatcherType
Delegate the responsibility of handling different `MatcherType`s to the config module.
This commit is contained in:
parent
3ea769676a
commit
4684713d7e
2 changed files with 15 additions and 9 deletions
|
@ -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<'_> {
|
impl Act for Action<'_> {
|
||||||
fn apply_action(&self, input: &str) -> String {
|
fn apply_action(&self, input: &str) -> String {
|
||||||
return match self {
|
return match self {
|
||||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -11,7 +11,7 @@ use clipboard::{ClipboardContext, ClipboardProvider};
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use log::{debug, error};
|
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"];
|
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");
|
ClipboardProvider::new().expect("Failed to get clipboard");
|
||||||
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
|
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
|
||||||
while let Ok(contents) = clipboard_contents.as_deref() {
|
while let Ok(contents) = clipboard_contents.as_deref() {
|
||||||
if let Some(subst) = config.substitutors.iter().find(|subst| {
|
if let Some(subst) = config
|
||||||
return match &subst.matcher {
|
.substitutors
|
||||||
MatcherType::Single(matcher) => matcher.check_match(contents),
|
.iter()
|
||||||
MatcherType::Multiple(matchers) => {
|
.find(|subst| subst.matcher.check_match(contents))
|
||||||
matchers.iter().all(|matcher| matcher.check_match(contents))
|
{
|
||||||
}
|
|
||||||
};
|
|
||||||
}) {
|
|
||||||
if subst.name.is_empty().not() {
|
if subst.name.is_empty().not() {
|
||||||
debug!("{}: matched on {}...", &subst.name, truncate(&contents, 40));
|
debug!("{}: matched on {}...", &subst.name, truncate(&contents, 40));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue