mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-16 08:07:05 +05:30
chore: revert to default formatting style
This commit is contained in:
parent
142676ac1d
commit
6048f7a537
6 changed files with 169 additions and 185 deletions
140
src/config.rs
140
src/config.rs
|
@ -1,123 +1,123 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use anyhow::{bail, Result};
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
use tracing::trace;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Replacements {
|
||||
#[serde(rename = "substitutor", default)]
|
||||
pub substitutors: Vec<Substitutor>,
|
||||
#[serde(rename = "substitutor", default)]
|
||||
pub substitutors: Vec<Substitutor>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Substitutor {
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
#[serde(alias = "matcher")]
|
||||
pub matcher: MatcherType,
|
||||
pub action: Action,
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
#[serde(alias = "matcher")]
|
||||
pub matcher: MatcherType,
|
||||
pub action: Action,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum MatcherType {
|
||||
Single(Matcher),
|
||||
Multiple(Vec<Matcher>),
|
||||
Single(Matcher),
|
||||
Multiple(Vec<Matcher>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Matcher {
|
||||
StartsWith { prefix: String },
|
||||
EndsWith { suffix: String },
|
||||
Contains { substring: String },
|
||||
Regex { pattern: String },
|
||||
Exactly { content: String },
|
||||
StartsWith { prefix: String },
|
||||
EndsWith { suffix: String },
|
||||
Contains { substring: String },
|
||||
Regex { pattern: String },
|
||||
Exactly { content: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Action {
|
||||
Set { content: String },
|
||||
Replace { from: String, to: String },
|
||||
Prefix { prefix: String },
|
||||
Suffix { suffix: String },
|
||||
Set { content: String },
|
||||
Replace { from: String, to: String },
|
||||
Prefix { prefix: String },
|
||||
Suffix { suffix: String },
|
||||
}
|
||||
|
||||
pub trait Match {
|
||||
fn check_match(&self, string: &str) -> bool;
|
||||
fn check_match(&self, string: &str) -> bool;
|
||||
}
|
||||
|
||||
pub trait Act {
|
||||
fn apply_action(&self, input: &str) -> String;
|
||||
fn apply_action(&self, input: &str) -> String;
|
||||
}
|
||||
|
||||
impl Replacements {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
for subst in &self.substitutors {
|
||||
match &subst.matcher {
|
||||
MatcherType::Single(matcher) => {
|
||||
if let Matcher::Regex { pattern } = matcher {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
for subst in &self.substitutors {
|
||||
match &subst.matcher {
|
||||
MatcherType::Single(matcher) => {
|
||||
if let Matcher::Regex { pattern } = matcher {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
MatcherType::Multiple(matchers) => {
|
||||
for matcher in matchers {
|
||||
if let Matcher::Regex { pattern } = matcher {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
MatcherType::Multiple(matchers) => {
|
||||
for matcher in matchers {
|
||||
if let Matcher::Regex { pattern } = matcher {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Match for Matcher {
|
||||
fn check_match(&self, string: &str) -> bool {
|
||||
trace!(?self, ?string, "Checking for match");
|
||||
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 } => {
|
||||
if let Ok(regex) = Regex::from_str(pattern) {
|
||||
regex.is_match(string)
|
||||
} else {
|
||||
false
|
||||
fn check_match(&self, string: &str) -> bool {
|
||||
trace!(?self, ?string, "Checking for match");
|
||||
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 } => {
|
||||
if let Ok(regex) = Regex::from_str(pattern) {
|
||||
regex.is_match(string)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Matcher::Exactly { content } => string == content,
|
||||
}
|
||||
}
|
||||
Matcher::Exactly { content } => string == content,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Match for MatcherType {
|
||||
fn check_match(&self, string: &str) -> bool {
|
||||
match self {
|
||||
MatcherType::Single(matcher) => matcher.check_match(string),
|
||||
MatcherType::Multiple(matchers) => {
|
||||
matchers.iter().all(|matcher| matcher.check_match(string))
|
||||
}
|
||||
fn check_match(&self, string: &str) -> bool {
|
||||
match self {
|
||||
MatcherType::Single(matcher) => matcher.check_match(string),
|
||||
MatcherType::Multiple(matchers) => {
|
||||
matchers.iter().all(|matcher| matcher.check_match(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Act for Action {
|
||||
fn apply_action(&self, input: &str) -> String {
|
||||
trace!(?self, ?input, "Applying action");
|
||||
match self {
|
||||
Action::Replace { from, to } => input.replace(from, to),
|
||||
Action::Prefix { prefix } => format!("{prefix}{input}"),
|
||||
Action::Suffix { suffix } => format!("{input}{suffix}"),
|
||||
Action::Set { content } => content.clone(),
|
||||
fn apply_action(&self, input: &str) -> String {
|
||||
trace!(?self, ?input, "Applying action");
|
||||
match self {
|
||||
Action::Replace { from, to } => input.replace(from, to),
|
||||
Action::Prefix { prefix } => format!("{prefix}{input}"),
|
||||
Action::Suffix { suffix } => format!("{input}{suffix}"),
|
||||
Action::Set { content } => content.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue