mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-15 10:07:00 +05:30
chore: reformat with new rustfmt config
This commit is contained in:
parent
2ee2cdb107
commit
5d6b27249d
3 changed files with 158 additions and 152 deletions
126
src/config.rs
126
src/config.rs
|
@ -5,102 +5,102 @@ use serde_derive::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Replacements<'config> {
|
pub struct Replacements<'config> {
|
||||||
#[serde(rename = "substitutor", borrow, default)]
|
#[serde(rename = "substitutor", borrow, default)]
|
||||||
pub substitutors: Vec<Substitutor<'config>>,
|
pub substitutors: Vec<Substitutor<'config>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Substitutor<'config> {
|
pub struct Substitutor<'config> {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub name: &'config str,
|
pub name: &'config str,
|
||||||
#[serde(borrow, alias = "matcher")]
|
#[serde(borrow, alias = "matcher")]
|
||||||
pub matcher: MatcherType<'config>,
|
pub matcher: MatcherType<'config>,
|
||||||
#[serde(borrow)]
|
#[serde(borrow)]
|
||||||
pub action: Action<'config>,
|
pub action: Action<'config>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum MatcherType<'config> {
|
pub enum MatcherType<'config> {
|
||||||
#[serde(borrow)]
|
#[serde(borrow)]
|
||||||
Single(Matcher<'config>),
|
Single(Matcher<'config>),
|
||||||
#[serde(borrow)]
|
#[serde(borrow)]
|
||||||
Multiple(Vec<Matcher<'config>>),
|
Multiple(Vec<Matcher<'config>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub enum Matcher<'config> {
|
pub enum Matcher<'config> {
|
||||||
#[serde(rename = "starts_with")]
|
#[serde(rename = "starts_with")]
|
||||||
StartsWith { prefix: &'config str },
|
StartsWith { prefix: &'config str },
|
||||||
#[serde(rename = "ends_with")]
|
#[serde(rename = "ends_with")]
|
||||||
EndsWith { suffix: &'config str },
|
EndsWith { suffix: &'config str },
|
||||||
#[serde(rename = "contains")]
|
#[serde(rename = "contains")]
|
||||||
Contains { substring: &'config str },
|
Contains { substring: &'config str },
|
||||||
#[serde(rename = "regex")]
|
#[serde(rename = "regex")]
|
||||||
Regex { pattern: &'config str },
|
Regex { pattern: &'config str },
|
||||||
#[serde(rename = "exactly")]
|
#[serde(rename = "exactly")]
|
||||||
Exactly { content: &'config str },
|
Exactly { content: &'config str },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub enum Action<'config> {
|
pub enum Action<'config> {
|
||||||
#[serde(rename = "set")]
|
#[serde(rename = "set")]
|
||||||
Set { content: &'config str },
|
Set { content: &'config str },
|
||||||
#[serde(rename = "replace")]
|
#[serde(rename = "replace")]
|
||||||
Replace {
|
Replace {
|
||||||
from: &'config str,
|
from: &'config str,
|
||||||
to: &'config str,
|
to: &'config str,
|
||||||
},
|
},
|
||||||
#[serde(rename = "prefix")]
|
#[serde(rename = "prefix")]
|
||||||
Prefix { prefix: &'config str },
|
Prefix { prefix: &'config str },
|
||||||
#[serde(rename = "suffix")]
|
#[serde(rename = "suffix")]
|
||||||
Suffix { suffix: &'config str },
|
Suffix { suffix: &'config str },
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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 } => {
|
||||||
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 {
|
||||||
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}"),
|
||||||
Action::Set { content } => content.to_owned().to_owned(),
|
Action::Set { content } => content.to_owned().to_owned(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
121
src/main.rs
121
src/main.rs
|
@ -17,85 +17,90 @@ use crate::config::{Act, Match, Replacements};
|
||||||
const VERSION_ARGS: [&str; 3] = ["version", "-v", "--version"];
|
const VERSION_ARGS: [&str; 3] = ["version", "-v", "--version"];
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
if check_for_version_arg() {
|
if check_for_version_arg() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
configure_tracing();
|
configure_tracing();
|
||||||
let config_path = get_config_path()?;
|
let config_path = get_config_path()?;
|
||||||
let config_str = std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
|
let config_str =
|
||||||
let config: Replacements<'_> = toml::from_str(&config_str)?;
|
std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
|
||||||
loop_clipboard(config);
|
let config: Replacements<'_> = toml::from_str(&config_str)?;
|
||||||
Ok(())
|
loop_clipboard(config);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_for_version_arg() -> bool {
|
fn check_for_version_arg() -> bool {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
for arg in args {
|
for arg in args {
|
||||||
if VERSION_ARGS.contains(&arg.deref()) {
|
if VERSION_ARGS.contains(&arg.deref()) {
|
||||||
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 configure_tracing() {
|
fn configure_tracing() {
|
||||||
let subscriber = FmtSubscriber::builder()
|
let subscriber = FmtSubscriber::builder()
|
||||||
.with_max_level(Level::TRACE)
|
.with_max_level(Level::TRACE)
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
tracing::subscriber::set_global_default(subscriber)
|
||||||
|
.expect("setting default subscriber failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_config_path() -> Result<PathBuf> {
|
fn get_config_path() -> Result<PathBuf> {
|
||||||
let mut config_path = config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;
|
let mut config_path =
|
||||||
config_path.push("substitutor");
|
config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;
|
||||||
config_path.push("config");
|
config_path.push("substitutor");
|
||||||
config_path.set_extension("toml");
|
config_path.push("config");
|
||||||
Ok(config_path)
|
config_path.set_extension("toml");
|
||||||
|
Ok(config_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loop_clipboard(config: Replacements) {
|
fn loop_clipboard(config: Replacements) {
|
||||||
let mut clipboard: ClipboardContext =
|
let mut clipboard: ClipboardContext =
|
||||||
ClipboardProvider::new().expect("Failed to get clipboard");
|
ClipboardProvider::new().expect("Failed to get clipboard");
|
||||||
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
|
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
|
||||||
while let Ok(contents) = clipboard_contents.as_deref() {
|
while let Ok(contents) = clipboard_contents.as_deref() {
|
||||||
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().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.apply_action(contents);
|
let result = subst.action.apply_action(contents);
|
||||||
if let Err(e) = clipboard.set_contents(result.to_owned()) {
|
if let Err(e) = clipboard.set_contents(result.to_owned()) {
|
||||||
error!("{e}");
|
error!("{e}");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
while let Ok(new_contents) = get_clipboard_contents(&mut clipboard) {
|
while let Ok(new_contents) = get_clipboard_contents(&mut clipboard) {
|
||||||
if new_contents != contents {
|
if new_contents != contents {
|
||||||
clipboard_contents = Ok(new_contents);
|
clipboard_contents = Ok(new_contents);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_clipboard_contents(clipboard: &mut ClipboardContext) -> Result<String, Box<dyn Error>> {
|
fn get_clipboard_contents(
|
||||||
clipboard.get_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 {
|
||||||
match s.char_indices().nth(max_chars) {
|
match s.char_indices().nth(max_chars) {
|
||||||
None => s,
|
None => s,
|
||||||
Some((idx, _)) => &s[..idx],
|
Some((idx, _)) => &s[..idx],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
63
src/test.rs
63
src/test.rs
|
@ -1,45 +1,46 @@
|
||||||
use crate::config::{Act, Action, Match, Matcher, MatcherType, Replacements};
|
|
||||||
use assay::assay;
|
use assay::assay;
|
||||||
|
|
||||||
|
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.*",
|
pattern: "^https.*",
|
||||||
};
|
};
|
||||||
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 { content: "doe" };
|
let action = Action::Set { content: "doe" };
|
||||||
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",
|
from: "doe",
|
||||||
to: "bow",
|
to: "bow",
|
||||||
};
|
};
|
||||||
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 { prefix: "hello " };
|
let action = Action::Prefix { prefix: "hello " };
|
||||||
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 { suffix: " doe" };
|
let action = Action::Suffix { suffix: " doe" };
|
||||||
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 = [
|
||||||
|
@ -48,26 +49,26 @@ 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 { .. }));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue