mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-15 00:47:01 +05:30
Implement matchers and actions
This commit is contained in:
parent
05201d7de7
commit
3fd078e51b
4 changed files with 84 additions and 2 deletions
|
@ -1,4 +1,7 @@
|
|||
#![allow(dead_code)]
|
||||
use std::str::FromStr;
|
||||
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
|
@ -13,6 +16,14 @@ pub struct Substitutor {
|
|||
pub action: Action,
|
||||
}
|
||||
|
||||
pub trait Match {
|
||||
fn check_match<'a>(self: Self, string: &'a str) -> bool;
|
||||
}
|
||||
|
||||
pub trait Act {
|
||||
fn apply_action(self: Self, input: String) -> String;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub enum Matcher {
|
||||
#[serde(rename = "starts_with")]
|
||||
|
@ -25,6 +36,20 @@ pub enum Matcher {
|
|||
Regex { pattern: String },
|
||||
}
|
||||
|
||||
impl Match for Matcher {
|
||||
fn check_match<'a>(self: Self, string: &'a str) -> bool {
|
||||
return match self {
|
||||
Matcher::StartsWith { prefix } => string.starts_with(&prefix),
|
||||
Matcher::EndsWith { suffix } => string.ends_with(&suffix),
|
||||
Matcher::Contains { substring } => string.contains(&substring),
|
||||
Matcher::Regex { pattern } => {
|
||||
let regex = Regex::from_str(&pattern).expect("Failed to parse regex");
|
||||
regex.is_match(&string)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub enum Action {
|
||||
#[serde(rename = "replace")]
|
||||
|
@ -36,3 +61,14 @@ pub enum Action {
|
|||
#[serde(rename = "remove")]
|
||||
Remove { substring: String },
|
||||
}
|
||||
|
||||
impl Act for Action {
|
||||
fn apply_action(self: Self, input: String) -> String {
|
||||
return match self {
|
||||
Action::Replace { from, to } => input.replace(&from, &to),
|
||||
Action::Prefix { prefix } => format!("{}{}", prefix, input),
|
||||
Action::Suffix { suffix } => format!("{}{}", input, suffix),
|
||||
Action::Remove { substring } => input.replace(&substring, ""),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue