mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-14 21:17:01 +05:30
all: refactor to elide matchers when not required
This commit is contained in:
parent
abd91e6601
commit
13bcb918bd
2 changed files with 43 additions and 29 deletions
|
@ -4,39 +4,41 @@ use regex::Regex;
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize)]
|
#[derive(Clone, Debug, Default, Deserialize)]
|
||||||
pub struct Replacements {
|
pub struct Replacements<'config> {
|
||||||
#[serde(rename = "substitutor")]
|
#[serde(rename = "substitutor", borrow, default)]
|
||||||
pub substitutors: Vec<Substitutor>,
|
pub substitutors: Vec<Substitutor<'config>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub struct Substitutor {
|
pub struct Substitutor<'config> {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub name: String,
|
pub name: &'config str,
|
||||||
pub matcher: Matcher,
|
#[serde(borrow)]
|
||||||
pub action: Action,
|
pub matcher: Matcher<'config>,
|
||||||
|
#[serde(borrow)]
|
||||||
|
pub action: Action<'config>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub enum Matcher {
|
pub enum Matcher<'config> {
|
||||||
#[serde(rename = "starts_with")]
|
#[serde(rename = "starts_with")]
|
||||||
StartsWith { prefix: String },
|
StartsWith { prefix: &'config str },
|
||||||
#[serde(rename = "ends_with")]
|
#[serde(rename = "ends_with")]
|
||||||
EndsWith { suffix: String },
|
EndsWith { suffix: &'config str },
|
||||||
#[serde(rename = "contains")]
|
#[serde(rename = "contains")]
|
||||||
Contains { substring: String },
|
Contains { substring: &'config str },
|
||||||
#[serde(rename = "regex")]
|
#[serde(rename = "regex")]
|
||||||
Regex { pattern: String },
|
Regex { pattern: &'config str },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub enum Action {
|
pub enum Action<'config> {
|
||||||
#[serde(rename = "replace")]
|
#[serde(rename = "replace")]
|
||||||
Replace { from: String, to: String },
|
Replace { from: &'config str, to: &'config str },
|
||||||
#[serde(rename = "prefix")]
|
#[serde(rename = "prefix")]
|
||||||
Prefix { prefix: String },
|
Prefix { prefix: &'config str },
|
||||||
#[serde(rename = "suffix")]
|
#[serde(rename = "suffix")]
|
||||||
Suffix { suffix: String },
|
Suffix { suffix: &'config str },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Match {
|
pub trait Match {
|
||||||
|
@ -44,27 +46,27 @@ pub trait Match {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Act {
|
pub trait Act {
|
||||||
fn apply_action(self, input: String) -> String;
|
fn apply_action(self, input: &str) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Match for Matcher {
|
impl Match for Matcher<'_> {
|
||||||
fn check_match(self, string: &str) -> bool {
|
fn check_match(self, string: &str) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Matcher::StartsWith { prefix } => string.starts_with(&prefix),
|
Matcher::StartsWith { prefix } => string.starts_with(&prefix),
|
||||||
Matcher::EndsWith { suffix } => string.ends_with(&suffix),
|
Matcher::EndsWith { suffix } => string.ends_with(&suffix),
|
||||||
Matcher::Contains { substring } => string.contains(&substring),
|
Matcher::Contains { substring } => string.contains(&substring),
|
||||||
Matcher::Regex { pattern } => {
|
Matcher::Regex { pattern } => {
|
||||||
let regex = Regex::from_str(&pattern).expect("Failed to parse regex");
|
let regex = Regex::from_str(pattern).expect("Failed to parse regex");
|
||||||
regex.is_match(string)
|
regex.is_match(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Act for Action {
|
impl Act for Action<'_> {
|
||||||
fn apply_action(self, input: String) -> String {
|
fn apply_action(self, input: &str) -> String {
|
||||||
return match self {
|
return match self {
|
||||||
Action::Replace { from, to } => input.replace(&from, &to),
|
Action::Replace { from, to } => input.replace(from, to),
|
||||||
Action::Prefix { prefix } => format!("{}{}", prefix, input),
|
Action::Prefix { prefix } => format!("{}{}", prefix, input),
|
||||||
Action::Suffix { suffix } => format!("{}{}", input, suffix),
|
Action::Suffix { suffix } => format!("{}{}", input, suffix),
|
||||||
};
|
};
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -1,6 +1,7 @@
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
use std::ops::Not;
|
use std::error::Error;
|
||||||
|
use std::ops::{Deref, Not};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use clipboard::{ClipboardContext, ClipboardProvider};
|
use clipboard::{ClipboardContext, ClipboardProvider};
|
||||||
|
@ -15,16 +16,16 @@ fn main() -> Result<()> {
|
||||||
config_path.push("substitutor");
|
config_path.push("substitutor");
|
||||||
config_path.push("config");
|
config_path.push("config");
|
||||||
config_path.set_extension("toml");
|
config_path.set_extension("toml");
|
||||||
let config: Replacements = if config_path.exists() {
|
let config_str = std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
|
||||||
let config_str = std::fs::read_to_string(config_path.as_path())?;
|
let config = if config_path.exists() {
|
||||||
toml::from_str(&config_str)?
|
toml::from_str(&config_str)?
|
||||||
} else {
|
} else {
|
||||||
Replacements::default()
|
Replacements::default()
|
||||||
};
|
};
|
||||||
let mut clipboard: ClipboardContext =
|
let mut clipboard: ClipboardContext =
|
||||||
ClipboardProvider::new().expect("Failed to get clipboard");
|
ClipboardProvider::new().expect("Failed to get clipboard");
|
||||||
loop {
|
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
|
||||||
let contents = clipboard.get_contents().expect("Failed to read clipboard");
|
while let Ok(contents) = clipboard_contents.as_deref() {
|
||||||
if let Some(subst) = config
|
if let Some(subst) = config
|
||||||
.substitutors
|
.substitutors
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -33,12 +34,23 @@ fn main() -> Result<()> {
|
||||||
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));
|
||||||
}
|
}
|
||||||
let result = subst.action.clone().apply_action(contents);
|
let result = subst.action.clone().apply_action(contents.deref());
|
||||||
if let Err(e) = clipboard.set_contents(result) {
|
if let Err(e) = clipboard.set_contents(result.to_owned()) {
|
||||||
error!("{}", e);
|
error!("{}", e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
while let Ok(new_contents) = get_clipboard_contents(&mut clipboard) {
|
||||||
|
if new_contents != contents {
|
||||||
|
clipboard_contents = Ok(new_contents);
|
||||||
|
break;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_clipboard_contents(clipboard: &mut ClipboardContext) -> Result<String, Box<dyn Error>> {
|
||||||
|
clipboard.get_contents()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn truncate(s: &str, max_chars: usize) -> &str {
|
fn truncate(s: &str, max_chars: usize) -> &str {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue