Skip to content

Commit

Permalink
refactor ClinkConfig.params to be HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Sep 23, 2023
1 parent 18ee925 commit 66a74ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
21 changes: 6 additions & 15 deletions src/clink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ use url::Url;

pub struct Clink {
config: ClinkConfig,
index: HashMap<Rc<str>, bool>,
exit_map: HashMap<Rc<str>, Rc<[Rc<str>]>>,
finder: LinkFinder,
}

impl Clink {
pub fn new(config: ClinkConfig) -> Self {
let index = create_index(&config.params);
let exit_map = build_exit_map(&config.exit);
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Url]);

Clink {
config,
index,
exit_map,
finder,
}
Expand Down Expand Up @@ -63,7 +60,7 @@ impl Clink {
let mut rng = rand::thread_rng();
query
.map(|p| {
if self.index.contains_key::<Rc<str>>(&p.0.clone().into()) {
if self.config.params.contains::<Rc<str>>(&p.0.clone().into()) {

Check warning on line 63 in src/clink.rs

View check run for this annotation

Codecov / codecov/patch

src/clink.rs#L63

Added line #L63 was not covered by tests
(
p.0.to_string(),
swap_two_chars(
Expand All @@ -83,15 +80,15 @@ impl Clink {

fn filter(&self, query: url::form_urlencoded::Parse<'_>) -> Vec<(String, String)> {
query
.filter(|p| !self.index.contains_key::<Rc<str>>(&p.0.clone().into()))
.filter(|p| !self.config.params.contains::<Rc<str>>(&p.0.clone().into()))
.map(|p| (p.0.to_string(), p.1.to_string()))
.collect()
}

fn replace(&self, query: url::form_urlencoded::Parse<'_>) -> Vec<(String, String)> {
query
.map(|p| {
if self.index.contains_key::<Rc<str>>(&p.0.clone().into()) {
if self.config.params.contains::<Rc<str>>(&p.0.clone().into()) {
(p.0.to_string(), self.config.replace_to.clone())
} else {
(p.0.to_string(), p.1.to_string())
Expand Down Expand Up @@ -126,14 +123,6 @@ fn join_url(domain: &str, path: &str) -> Rc<str> {
format!("{}{}", domain, path).into()
}

fn create_index(vec: &[Rc<str>]) -> HashMap<Rc<str>, bool> {
let mut map: HashMap<Rc<str>, bool> = HashMap::new();
for key in vec.iter().cloned() {
map.insert(key, true);
}
map
}

fn build_exit_map(input: &[Vec<Rc<str>>]) -> HashMap<Rc<str>, Rc<[Rc<str>]>> {
let mut map: HashMap<Rc<str>, Rc<[Rc<str>]>> = HashMap::new();
for row in input.iter() {
Expand All @@ -148,6 +137,8 @@ fn build_exit_map(input: &[Vec<Rc<str>>]) -> HashMap<Rc<str>, Rc<[Rc<str>]>> {
#[cfg(test)]
mod find_and_replace {

use std::collections::HashSet;

use super::*;

#[test]
Expand Down Expand Up @@ -260,7 +251,7 @@ mod find_and_replace {
mode: Mode::Replace,
replace_to: "clink".to_string(),
sleep_duration: 150,
params: vec!["foo".into()],
params: HashSet::from(["foo".into()]),
exit: vec![],
});
assert_eq!(
Expand Down
9 changes: 5 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashSet,
path::{Path, PathBuf},
process,
rc::Rc,
Expand All @@ -8,8 +9,8 @@ use serde::{Deserialize, Serialize};

use crate::mode::Mode;

fn get_default_params() -> Vec<Rc<str>> {
vec![
fn get_default_params() -> HashSet<Rc<str>> {
HashSet::from([
"fbclid".into(),
"gclid".into(),
"gclsrc".into(),
Expand All @@ -20,7 +21,7 @@ fn get_default_params() -> Vec<Rc<str>> {
"utm_medium".into(),
"utm_term".into(),
"utm_content".into(),
]
])
}

fn get_default_exit() -> Vec<Vec<Rc<str>>> {
Expand All @@ -41,7 +42,7 @@ pub struct ClinkConfig {
pub mode: Mode,
pub replace_to: String,
pub sleep_duration: u64,
pub params: Vec<Rc<str>>,
pub params: HashSet<Rc<str>>,
pub exit: Vec<Vec<Rc<str>>>,
}

Expand Down

0 comments on commit 66a74ab

Please sign in to comment.