fix: adjust for toml 0.6.0 upgrade

This commit is contained in:
Harsh Shandilya 2023-01-24 17:15:51 +00:00
parent 561733796b
commit a0ba1db28a
3 changed files with 33 additions and 36 deletions

View File

@ -7,12 +7,12 @@ use tracing::{debug, error};
use crate::config::{Act, Match, Replacements}; use crate::config::{Act, Match, Replacements};
struct Handler<'a> { struct Handler {
ctx: ClipboardContext, ctx: ClipboardContext,
config: Replacements<'a>, config: Replacements,
} }
impl<'a> ClipboardHandler for Handler<'a> { impl ClipboardHandler for Handler {
fn on_clipboard_change(&mut self) -> CallbackResult { fn on_clipboard_change(&mut self) -> CallbackResult {
if let Ok(contents) = self.ctx.get_contents() { if let Ok(contents) = self.ctx.get_contents() {
if let Some(subst) = self if let Some(subst) = self
@ -39,7 +39,7 @@ impl<'a> ClipboardHandler for Handler<'a> {
} }
} }
pub fn monitor(config: Replacements<'_>) { pub fn monitor(config: Replacements) {
let ctx = ClipboardContext::new().expect("Failed to acquire clipboard"); let ctx = ClipboardContext::new().expect("Failed to acquire clipboard");
let handler = Handler { ctx, config }; let handler = Handler { ctx, config };
let _master = Master::new(handler).run(); let _master = Master::new(handler).run();

View File

@ -6,57 +6,54 @@ use serde_derive::Deserialize;
use tracing::trace; use tracing::trace;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Replacements<'config> { pub struct Replacements {
#[serde(rename = "substitutor", borrow, default)] #[serde(rename = "substitutor", default)]
pub substitutors: Vec<Substitutor<'config>>, pub substitutors: Vec<Substitutor>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Substitutor<'config> { pub struct Substitutor {
#[serde(default)] #[serde(default)]
pub name: &'config str, pub name: String,
#[serde(borrow, alias = "matcher")] #[serde(alias = "matcher")]
pub matcher: MatcherType<'config>, pub matcher: MatcherType,
#[serde(borrow)] pub action: Action,
pub action: Action<'config>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum MatcherType<'config> { pub enum MatcherType {
#[serde(borrow)] Single(Matcher),
Single(Matcher<'config>), Multiple(Vec<Matcher>),
#[serde(borrow)]
Multiple(Vec<Matcher<'config>>),
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub enum Matcher<'config> { pub enum Matcher {
#[serde(rename = "starts_with")] #[serde(rename = "starts_with")]
StartsWith { prefix: &'config str }, StartsWith { prefix: String },
#[serde(rename = "ends_with")] #[serde(rename = "ends_with")]
EndsWith { suffix: &'config str }, EndsWith { suffix: String },
#[serde(rename = "contains")] #[serde(rename = "contains")]
Contains { substring: &'config str }, Contains { substring: String },
#[serde(rename = "regex")] #[serde(rename = "regex")]
Regex { pattern: &'config str }, Regex { pattern: String },
#[serde(rename = "exactly")] #[serde(rename = "exactly")]
Exactly { content: &'config str }, Exactly { content: String },
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub enum Action<'config> { pub enum Action {
#[serde(rename = "set")] #[serde(rename = "set")]
Set { content: &'config str }, Set { content: String },
#[serde(rename = "replace")] #[serde(rename = "replace")]
Replace { Replace {
from: &'config str, from: String,
to: &'config str, to: String,
}, },
#[serde(rename = "prefix")] #[serde(rename = "prefix")]
Prefix { prefix: &'config str }, Prefix { prefix: String },
#[serde(rename = "suffix")] #[serde(rename = "suffix")]
Suffix { suffix: &'config str }, Suffix { suffix: String },
} }
pub trait Match { pub trait Match {
@ -67,7 +64,7 @@ pub trait Act {
fn apply_action(&self, input: &str) -> String; fn apply_action(&self, input: &str) -> String;
} }
impl Replacements<'_> { impl Replacements {
pub fn validate(&self) -> Result<()> { pub fn validate(&self) -> Result<()> {
for subst in &self.substitutors { for subst in &self.substitutors {
match &subst.matcher { match &subst.matcher {
@ -93,7 +90,7 @@ impl Replacements<'_> {
} }
} }
impl Match for Matcher<'_> { impl Match for Matcher {
fn check_match(&self, string: &str) -> bool { fn check_match(&self, string: &str) -> bool {
trace!(?self, ?string, "Checking for match"); trace!(?self, ?string, "Checking for match");
match self { match self {
@ -112,7 +109,7 @@ impl Match for Matcher<'_> {
} }
} }
impl Match for MatcherType<'_> { impl Match for MatcherType {
fn check_match(&self, string: &str) -> bool { fn check_match(&self, string: &str) -> bool {
match self { match self {
MatcherType::Single(matcher) => matcher.check_match(string), MatcherType::Single(matcher) => matcher.check_match(string),
@ -123,14 +120,14 @@ impl Match for MatcherType<'_> {
} }
} }
impl Act for Action<'_> { impl Act for Action {
fn apply_action(&self, input: &str) -> String { fn apply_action(&self, input: &str) -> String {
trace!(?self, ?input, "Applying action"); trace!(?self, ?input, "Applying action");
match self { 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),
Action::Set { content } => content.to_owned().to_owned(), Action::Set { content } => content.to_owned(),
} }
} }
} }

View File

@ -23,7 +23,7 @@ fn main() -> Result<()> {
let config_path = get_config_path()?; let config_path = get_config_path()?;
let config_str = let config_str =
std::fs::read_to_string(config_path.as_path()).unwrap_or_default(); std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
let config: Replacements<'_> = toml::from_str(&config_str)?; let config: Replacements = toml::from_str(&config_str)?;
config.validate()?; config.validate()?;
monitor(config); monitor(config);
Ok(()) Ok(())