From 66a74abe5f585a5cdf744c6d03bc521759fa89a9 Mon Sep 17 00:00:00 2001 From: Serhiy Barhamon Date: Sat, 23 Sep 2023 11:08:57 +0200 Subject: [PATCH] refactor ClinkConfig.params to be HashSet --- src/clink.rs | 21 ++++++--------------- src/config.rs | 9 +++++---- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/clink.rs b/src/clink.rs index bbde058..e703f18 100644 --- a/src/clink.rs +++ b/src/clink.rs @@ -10,21 +10,18 @@ use url::Url; pub struct Clink { config: ClinkConfig, - index: HashMap, bool>, exit_map: HashMap, Rc<[Rc]>>, 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, } @@ -63,7 +60,7 @@ impl Clink { let mut rng = rand::thread_rng(); query .map(|p| { - if self.index.contains_key::>(&p.0.clone().into()) { + if self.config.params.contains::>(&p.0.clone().into()) { ( p.0.to_string(), swap_two_chars( @@ -83,7 +80,7 @@ impl Clink { fn filter(&self, query: url::form_urlencoded::Parse<'_>) -> Vec<(String, String)> { query - .filter(|p| !self.index.contains_key::>(&p.0.clone().into())) + .filter(|p| !self.config.params.contains::>(&p.0.clone().into())) .map(|p| (p.0.to_string(), p.1.to_string())) .collect() } @@ -91,7 +88,7 @@ impl Clink { fn replace(&self, query: url::form_urlencoded::Parse<'_>) -> Vec<(String, String)> { query .map(|p| { - if self.index.contains_key::>(&p.0.clone().into()) { + if self.config.params.contains::>(&p.0.clone().into()) { (p.0.to_string(), self.config.replace_to.clone()) } else { (p.0.to_string(), p.1.to_string()) @@ -126,14 +123,6 @@ fn join_url(domain: &str, path: &str) -> Rc { format!("{}{}", domain, path).into() } -fn create_index(vec: &[Rc]) -> HashMap, bool> { - let mut map: HashMap, bool> = HashMap::new(); - for key in vec.iter().cloned() { - map.insert(key, true); - } - map -} - fn build_exit_map(input: &[Vec>]) -> HashMap, Rc<[Rc]>> { let mut map: HashMap, Rc<[Rc]>> = HashMap::new(); for row in input.iter() { @@ -148,6 +137,8 @@ fn build_exit_map(input: &[Vec>]) -> HashMap, Rc<[Rc]>> { #[cfg(test)] mod find_and_replace { + use std::collections::HashSet; + use super::*; #[test] @@ -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!( diff --git a/src/config.rs b/src/config.rs index be3d0b3..0ef2408 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashSet, path::{Path, PathBuf}, process, rc::Rc, @@ -8,8 +9,8 @@ use serde::{Deserialize, Serialize}; use crate::mode::Mode; -fn get_default_params() -> Vec> { - vec![ +fn get_default_params() -> HashSet> { + HashSet::from([ "fbclid".into(), "gclid".into(), "gclsrc".into(), @@ -20,7 +21,7 @@ fn get_default_params() -> Vec> { "utm_medium".into(), "utm_term".into(), "utm_content".into(), - ] + ]) } fn get_default_exit() -> Vec>> { @@ -41,7 +42,7 @@ pub struct ClinkConfig { pub mode: Mode, pub replace_to: String, pub sleep_duration: u64, - pub params: Vec>, + pub params: HashSet>, pub exit: Vec>>, }