Skip to content

Commit 1e70eb5

Browse files
author
Sergey Bargamon
committed
create params index once
1 parent 8b437a3 commit 1e70eb5

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod params;
33
mod utils;
44

55
use mode::Mode;
6-
use params::get_default_params;
6+
use params::{create_index, get_default_params};
77
use utils::find_and_replace;
88

99
use clipboard::{ClipboardContext, ClipboardProvider};
@@ -60,12 +60,12 @@ fn main() -> Result<(), confy::ConfyError> {
6060

6161
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
6262
let mut previous_clipboard = "".to_string();
63-
63+
let index = create_index(&cfg.params);
6464
loop {
6565
match ctx.get_contents() {
6666
Ok(current_clipboard) => {
6767
if previous_clipboard != current_clipboard {
68-
let cleaned = find_and_replace(&current_clipboard, &cfg);
68+
let cleaned = find_and_replace(&current_clipboard, &cfg, &index);
6969
if cleaned != current_clipboard {
7070
ctx.set_contents(cleaned.clone()).unwrap();
7171
}

src/utils.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::mode::Mode;
2-
use crate::params::{create_index, is_hit};
2+
use crate::params::is_hit;
33
use crate::ClinkConfig;
44
use chrono::prelude::*;
55
use linkify::{LinkFinder, LinkKind};
@@ -10,7 +10,7 @@ use url::Url;
1010
#[cfg(test)]
1111
mod find_and_replace {
1212
use super::*;
13-
use crate::params::get_default_params;
13+
use crate::params::{create_index, get_default_params};
1414

1515
#[test]
1616
fn naive() {
@@ -23,7 +23,8 @@ mod find_and_replace {
2323
except_mothers_day: false,
2424
sleep_duration: 150,
2525
params: get_default_params(),
26-
}
26+
},
27+
&create_index(&get_default_params())
2728
),
2829
"https://test.test/"
2930
);
@@ -36,7 +37,8 @@ mod find_and_replace {
3637
except_mothers_day: false,
3738
sleep_duration: 150,
3839
params: get_default_params()
39-
}
40+
},
41+
&create_index(&get_default_params())
4042
),
4143
"https://test.test/?fbclid=your_mom&utm_source=your_mom&utm_campaign=your_mom&utm_medium=your_mom"
4244
);
@@ -49,7 +51,8 @@ mod find_and_replace {
4951
except_mothers_day: false,
5052
sleep_duration: 150,
5153
params: get_default_params()
52-
}
54+
},
55+
&create_index(&get_default_params())
5356
),
5457
"https://test.test/?fbclid=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_source=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_campaign=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_medium=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs"
5558
);
@@ -65,7 +68,8 @@ mod find_and_replace {
6568
except_mothers_day: false,
6669
sleep_duration: 150,
6770
params: get_default_params()
68-
}
71+
},
72+
&create_index(&get_default_params())
6973
),
7074
"https://test.test/?abc=abc"
7175
);
@@ -78,7 +82,8 @@ mod find_and_replace {
7882
except_mothers_day: false,
7983
sleep_duration: 150,
8084
params: get_default_params()
81-
}
85+
},
86+
&create_index(&get_default_params())
8287
),
8388
"https://test.test/?abc=abc"
8489
);
@@ -94,7 +99,8 @@ mod find_and_replace {
9499
except_mothers_day: false,
95100
sleep_duration: 150,
96101
params: get_default_params()
97-
}
102+
},
103+
&create_index(&get_default_params())
98104
),
99105
"https://test.test/?abc=abc"
100106
);
@@ -107,7 +113,8 @@ mod find_and_replace {
107113
except_mothers_day: false,
108114
sleep_duration: 150,
109115
params: get_default_params()
110-
}
116+
},
117+
&create_index(&get_default_params())
111118
),
112119
"https://test.test/?abc=abc&fbclid=your_mom"
113120
);
@@ -123,7 +130,8 @@ mod find_and_replace {
123130
except_mothers_day: false,
124131
sleep_duration: 150,
125132
params: get_default_params()
126-
}
133+
},
134+
&create_index(&get_default_params())
127135
),
128136
"https://test.test/?abc=abc\nhttps://test.test/?abc=abc"
129137
);
@@ -136,7 +144,8 @@ mod find_and_replace {
136144
except_mothers_day: false,
137145
sleep_duration: 150,
138146
params: get_default_params()
139-
}
147+
},
148+
&create_index(&get_default_params())
140149
),
141150
"https://test.test/?abc=abc&fbclid=your_mom\nhttps://test.test/?abc=abc&fbclid=your_mom"
142151
);
@@ -152,7 +161,8 @@ mod find_and_replace {
152161
except_mothers_day: false,
153162
sleep_duration: 150,
154163
params: get_default_params()
155-
}
164+
},
165+
&create_index(&get_default_params())
156166
),
157167
"some text here https://test.test/?abc=abc here \nand herehttps://test.test/?abc=abc"
158168
);
@@ -165,7 +175,8 @@ mod find_and_replace {
165175
except_mothers_day: false,
166176
sleep_duration: 150,
167177
params: get_default_params()
168-
}
178+
},
179+
&create_index(&get_default_params())
169180
),
170181
"some text here https://test.test/?abc=abc&fbclid=your_mom here \nand herehttps://test.test/?abc=abc&fbclid=your_mom"
171182
);
@@ -181,7 +192,8 @@ mod find_and_replace {
181192
except_mothers_day: false,
182193
sleep_duration: 150,
183194
params: get_default_params()
184-
}
195+
},
196+
&create_index(&get_default_params())
185197
),
186198
"https://test.test/?fbclid=foo&utm_source=foo&utm_campaign=foo&utm_medium=foo"
187199
);
@@ -198,21 +210,22 @@ mod find_and_replace {
198210
except_mothers_day: false,
199211
sleep_duration: 150,
200212
params: vec!["foo".to_string()]
201-
}
213+
},
214+
&create_index(&vec!["foo".to_string()])
202215
),
203216
"https://test.test/?foo=your_mom"
204217
);
205218
}
206219
}
207220

208-
pub fn find_and_replace(str: &str, config: &ClinkConfig) -> String {
221+
pub fn find_and_replace(str: &str, config: &ClinkConfig, index: &HashMap<String, bool>) -> String {
209222
let mut finder = LinkFinder::new();
210223
finder.kinds(&[LinkKind::Url]);
211224
let mut res = str.to_string();
212225
for link in finder.links(str) {
213226
let l = Url::parse(link.as_str()).unwrap();
214227

215-
let query: Vec<(_, _)> = process_query(l.query_pairs(), config);
228+
let query: Vec<(_, _)> = process_query(l.query_pairs(), config, index);
216229

217230
let mut l2 = l.clone();
218231
l2.set_query(None);
@@ -231,27 +244,27 @@ pub fn find_and_replace(str: &str, config: &ClinkConfig) -> String {
231244
fn process_query(
232245
query: url::form_urlencoded::Parse<'_>,
233246
config: &ClinkConfig,
247+
index: &HashMap<String, bool>,
234248
) -> Vec<(String, String)> {
235-
let index = create_index(&config.params);
236249
match config.mode {
237-
Mode::Remove => filter(query, &index),
250+
Mode::Remove => filter(query, index),
238251
Mode::YourMom => {
239252
if config.except_mothers_day {
240253
let date = Utc::today();
241254
if date.month() == 5 && date.day() == 9 {
242-
filter(query, &index)
255+
filter(query, index)
243256
} else {
244-
your_mom(query, &index, config)
257+
your_mom(query, index, config)
245258
}
246259
} else {
247-
your_mom(query, &index, config)
260+
your_mom(query, index, config)
248261
}
249262
}
250263
Mode::Evil => {
251264
let mut rng = rand::thread_rng();
252265
query
253266
.map(|p| {
254-
if is_hit(&p.0, &index) {
267+
if is_hit(&p.0, index) {
255268
(
256269
p.0.to_string(),
257270
swap_two_chars(

0 commit comments

Comments
 (0)