chore: revert to default formatting style

This commit is contained in:
Harsh Shandilya 2025-01-19 17:55:01 +05:30
parent 142676ac1d
commit 6048f7a537
6 changed files with 169 additions and 185 deletions

View file

@ -1,11 +0,0 @@
comment_width = 80
group_imports = "StdExternalCrate"
imports_granularity = "Module"
max_width = 80
newline_style = "Unix"
normalize_comments = true
normalize_doc_attributes = true
reorder_impl_items = true
tab_spaces = 2
version = "Two"
wrap_comments = true

View file

@ -5,24 +5,23 @@ use std::time::Duration;
use tracing::{debug, error}; use tracing::{debug, error};
pub fn monitor(config: &Replacements) -> Result<()> { pub fn monitor(config: &Replacements) -> Result<()> {
loop { loop {
let mut clipboard = let mut clipboard = ClipboardContext::new().expect("Failed to get clipboard");
ClipboardContext::new().expect("Failed to get clipboard"); if let Ok(contents) = clipboard.get_contents() {
if let Ok(contents) = clipboard.get_contents() { if let Some(subst) = config
if let Some(subst) = config .substitutors
.substitutors .iter()
.iter() .find(|subst| subst.matcher.check_match(&contents))
.find(|subst| subst.matcher.check_match(&contents)) {
{ if !subst.name.is_empty() {
if !subst.name.is_empty() { debug!(?subst.name, ?contents);
debug!(?subst.name, ?contents); }
let result = subst.action.apply_action(&contents);
if let Err(e) = clipboard.set_contents(result) {
error!("{}", e);
}
};
} }
let result = subst.action.apply_action(&contents); std::thread::sleep(Duration::from_millis(1_000));
if let Err(e) = clipboard.set_contents(result) {
error!("{}", e);
}
};
} }
std::thread::sleep(Duration::from_millis(1_000));
}
} }

View file

@ -1,123 +1,123 @@
use std::str::FromStr; use std::str::FromStr;
use anyhow::{Result, bail}; use anyhow::{bail, Result};
use regex::Regex; use regex::Regex;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use tracing::trace; use tracing::trace;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Replacements { pub struct Replacements {
#[serde(rename = "substitutor", default)] #[serde(rename = "substitutor", default)]
pub substitutors: Vec<Substitutor>, pub substitutors: Vec<Substitutor>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Substitutor { pub struct Substitutor {
#[serde(default)] #[serde(default)]
pub name: String, pub name: String,
#[serde(alias = "matcher")] #[serde(alias = "matcher")]
pub matcher: MatcherType, pub matcher: MatcherType,
pub action: Action, pub action: Action,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum MatcherType { pub enum MatcherType {
Single(Matcher), Single(Matcher),
Multiple(Vec<Matcher>), Multiple(Vec<Matcher>),
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum Matcher { pub enum Matcher {
StartsWith { prefix: String }, StartsWith { prefix: String },
EndsWith { suffix: String }, EndsWith { suffix: String },
Contains { substring: String }, Contains { substring: String },
Regex { pattern: String }, Regex { pattern: String },
Exactly { content: String }, Exactly { content: String },
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum Action { pub enum Action {
Set { content: String }, Set { content: String },
Replace { from: String, to: String }, Replace { from: String, to: String },
Prefix { prefix: String }, Prefix { prefix: String },
Suffix { suffix: String }, Suffix { suffix: String },
} }
pub trait Match { pub trait Match {
fn check_match(&self, string: &str) -> bool; fn check_match(&self, string: &str) -> bool;
} }
pub trait Act { 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 {
MatcherType::Single(matcher) => { MatcherType::Single(matcher) => {
if let Matcher::Regex { pattern } = matcher { if let Matcher::Regex { pattern } = matcher {
if let Err(e) = Regex::from_str(pattern) { if let Err(e) = Regex::from_str(pattern) {
bail!(e); 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) => { Ok(())
for matcher in matchers {
if let Matcher::Regex { pattern } = matcher {
if let Err(e) = Regex::from_str(pattern) {
bail!(e);
}
}
}
}
}
} }
Ok(())
}
} }
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 {
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 } => {
if let Ok(regex) = Regex::from_str(pattern) { if let Ok(regex) = Regex::from_str(pattern) {
regex.is_match(string) regex.is_match(string)
} else { } else {
false false
}
}
Matcher::Exactly { content } => string == content,
} }
}
Matcher::Exactly { content } => string == content,
} }
}
} }
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),
MatcherType::Multiple(matchers) => { MatcherType::Multiple(matchers) => {
matchers.iter().all(|matcher| matcher.check_match(string)) 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 {
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.clone(), Action::Set { content } => content.clone(),
}
} }
}
} }

View file

@ -1,30 +1,28 @@
use tracing::Level;
use tracing::dispatcher::SetGlobalDefaultError; use tracing::dispatcher::SetGlobalDefaultError;
use tracing::subscriber::set_global_default; use tracing::subscriber::set_global_default;
use tracing::Level;
use tracing_subscriber::filter::Targets; use tracing_subscriber::filter::Targets;
#[cfg(not(feature = "journald"))] #[cfg(not(feature = "journald"))]
fn configure_tracing(filter: Targets) -> Result<(), SetGlobalDefaultError> { fn configure_tracing(filter: Targets) -> Result<(), SetGlobalDefaultError> {
use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{Layer, fmt}; use tracing_subscriber::{fmt, Layer};
let stdout_log = fmt::layer().pretty(); let stdout_log = fmt::layer().pretty();
let subscriber = let subscriber = tracing_subscriber::registry().with(stdout_log.with_filter(filter));
tracing_subscriber::registry().with(stdout_log.with_filter(filter)); set_global_default(subscriber)
set_global_default(subscriber)
} }
#[cfg(feature = "journald")] #[cfg(feature = "journald")]
fn configure_tracing(filter: Targets) -> Result<(), SetGlobalDefaultError> { fn configure_tracing(filter: Targets) -> Result<(), SetGlobalDefaultError> {
use tracing_journald::layer; use tracing_journald::layer;
use tracing_subscriber::{layer::SubscriberExt, registry}; use tracing_subscriber::{layer::SubscriberExt, registry};
let subscriber = registry().with(filter).with(layer().unwrap()); let subscriber = registry().with(filter).with(layer().unwrap());
set_global_default(subscriber) set_global_default(subscriber)
} }
pub fn init() -> Result<(), SetGlobalDefaultError> { pub fn init() -> Result<(), SetGlobalDefaultError> {
let tracing_filter = let tracing_filter = Targets::new().with_target("clipboard_subsitutor", Level::DEBUG);
Targets::new().with_target("clipboard_subsitutor", Level::DEBUG); configure_tracing(tracing_filter)
configure_tracing(tracing_filter)
} }

View file

@ -6,7 +6,7 @@ mod test;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::{Result, anyhow, bail}; use anyhow::{anyhow, bail, Result};
use dirs::config_dir; use dirs::config_dir;
use tracing::debug; use tracing::debug;
@ -14,43 +14,41 @@ use crate::clipboard::monitor;
use crate::config::Replacements; use crate::config::Replacements;
fn main() -> Result<()> { fn main() -> Result<()> {
if check_for_version_arg() { if check_for_version_arg() {
return Ok(()); return Ok(());
} }
if let Err(e) = logging::init() { if let Err(e) = logging::init() {
bail!(e) bail!(e)
}; };
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)
} }
fn check_for_version_arg() -> bool { fn check_for_version_arg() -> bool {
for arg in argv::iter() { for arg in argv::iter() {
if arg == "-v" || arg == "version" || arg == "--version" { if arg == "-v" || arg == "version" || arg == "--version" {
print_version(); print_version();
return true; return true;
}
} }
} false
false
} }
fn print_version() { fn print_version() {
println!( println!(
"{}", "{}",
concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION")) concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"))
); );
} }
fn get_config_path() -> Result<PathBuf> { fn get_config_path() -> Result<PathBuf> {
let mut config_path = let mut config_path = config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;
config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?; 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"); debug!("Config file: {}", config_path.to_string_lossy());
debug!("Config file: {}", config_path.to_string_lossy()); Ok(config_path)
Ok(config_path)
} }

View file

@ -4,49 +4,49 @@ use crate::config::{Act, Action, Match, Matcher, MatcherType, Replacements};
#[assay] #[assay]
fn regex_matcher() { fn regex_matcher() {
let matcher = Matcher::Regex { let matcher = Matcher::Regex {
pattern: "^https.*".to_string(), pattern: "^https.*".to_string(),
}; };
assert!(matcher.check_match("https://example.com")); assert!(matcher.check_match("https://example.com"));
assert!(!matcher.check_match("example.com")); assert!(!matcher.check_match("example.com"));
} }
#[assay] #[assay]
fn set_action() { fn set_action() {
let action = Action::Set { let action = Action::Set {
content: "doe".to_string(), content: "doe".to_string(),
}; };
assert_eq!("doe", &action.apply_action("john")); assert_eq!("doe", &action.apply_action("john"));
} }
#[assay] #[assay]
fn replace_action() { fn replace_action() {
let action = Action::Replace { let action = Action::Replace {
from: "doe".to_string(), from: "doe".to_string(),
to: "bow".to_string(), to: "bow".to_string(),
}; };
assert_eq!("john bow", &action.apply_action("john doe")); assert_eq!("john bow", &action.apply_action("john doe"));
} }
#[assay] #[assay]
fn prefix_action() { fn prefix_action() {
let action = Action::Prefix { let action = Action::Prefix {
prefix: "hello ".to_string(), prefix: "hello ".to_string(),
}; };
assert_eq!("hello john", &action.apply_action("john")); assert_eq!("hello john", &action.apply_action("john"));
} }
#[assay] #[assay]
fn suffix_action() { fn suffix_action() {
let action = Action::Suffix { let action = Action::Suffix {
suffix: " doe".to_string(), suffix: " doe".to_string(),
}; };
assert_eq!("john doe", &action.apply_action("john")); assert_eq!("john doe", &action.apply_action("john"));
} }
#[assay] #[assay]
fn parse_with_multiple_matchers() { fn parse_with_multiple_matchers() {
let config = r#" let config = r#"
[[substitutor]] [[substitutor]]
name = "Example" name = "Example"
matcher = [ matcher = [
@ -55,50 +55,50 @@ fn parse_with_multiple_matchers() {
] ]
action = { prefix = { prefix = "/mirror" } } action = { prefix = { prefix = "/mirror" } }
"#; "#;
let config: Replacements = toml::from_str(config)?; let config: Replacements = toml::from_str(config)?;
assert_eq!(1, config.substitutors.len()); assert_eq!(1, config.substitutors.len());
let subst = &config.substitutors[0]; let subst = &config.substitutors[0];
assert_eq!("Example", &subst.name); assert_eq!("Example", &subst.name);
assert!(matches!(subst.matcher, MatcherType::Multiple(_))); assert!(matches!(subst.matcher, MatcherType::Multiple(_)));
assert!(matches!(subst.action, Action::Prefix { .. })); assert!(matches!(subst.action, Action::Prefix { .. }));
} }
#[assay] #[assay]
fn parse_with_single_matcher() { fn parse_with_single_matcher() {
let config = r#" let config = r#"
[[substitutor]] [[substitutor]]
name = "Example" name = "Example"
matcher = { starts_with = { prefix = "https://example.com" } } matcher = { starts_with = { prefix = "https://example.com" } }
action = { prefix = { prefix = "/mirror" } } action = { prefix = { prefix = "/mirror" } }
"#; "#;
let config: Replacements = toml::from_str(config)?; let config: Replacements = toml::from_str(config)?;
assert_eq!(1, config.substitutors.len()); assert_eq!(1, config.substitutors.len());
let subst = &config.substitutors[0]; let subst = &config.substitutors[0];
assert_eq!("Example", &subst.name); assert_eq!("Example", &subst.name);
assert!(matches!(subst.matcher, MatcherType::Single(_))); assert!(matches!(subst.matcher, MatcherType::Single(_)));
assert!(matches!(subst.action, Action::Prefix { .. })); assert!(matches!(subst.action, Action::Prefix { .. }));
} }
#[assay] #[assay]
fn config_validation_success() { fn config_validation_success() {
let config = r#" let config = r#"
[[substitutor]] [[substitutor]]
name = "vxTwitter" name = "vxTwitter"
matcher = { regex = { pattern = "^https://(?P<host>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } } matcher = { regex = { pattern = "^https://(?P<host>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } }
action = { replace = { from = "twitter.com", to = "vxtwitter.com" } } action = { replace = { from = "twitter.com", to = "vxtwitter.com" } }
"#; "#;
let config: Replacements = toml::from_str(config)?; let config: Replacements = toml::from_str(config)?;
assert!(config.validate().is_ok()); assert!(config.validate().is_ok());
} }
#[assay] #[assay]
fn config_validation_failure() { fn config_validation_failure() {
let config = r#" let config = r#"
[[substitutor]] [[substitutor]]
name = "vxTwitter" name = "vxTwitter"
matcher = { regex = { pattern = "^https://(?P<>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } } matcher = { regex = { pattern = "^https://(?P<>(?:mobile.)?twitter.com)/.*/status/[0-9]+.*" } }
action = { replace = { from = "twitter.com", to = "vxtwitter.com" } } action = { replace = { from = "twitter.com", to = "vxtwitter.com" } }
"#; "#;
let config: Replacements = toml::from_str(config)?; let config: Replacements = toml::from_str(config)?;
assert!(config.validate().is_err()); assert!(config.validate().is_err());
} }