1use regex::{Regex, RegexBuilder};
2use std::collections::HashMap;
3use std::ffi::OsString;
4
5use std::fs::File;
6use std::io::Read;
7use std::sync::{LazyLock, Mutex};
8
9use super::args::Args;
10
11const DICT: &'static str = r#"
13{
14"rs":["//", "/\\*"],
15"go":["//", "/\\*", "// "],
16"lisp":[";"],
17"asd":[";"],
18"asdf":[";"],
19"py":["\\#"],
20"hs":["--", "-- "],
21"el":[";"],
22"clj":[";"],
23"js":["//"],
24"makefile":["\\#"],
25"dockerfile":["\\#"]
26}
27"#;
28
29static TABLE: LazyLock<Mutex<HashMap<String, Vec<String>>>> =
30 LazyLock::new(|| Mutex::new(serde_json::from_str(DICT).unwrap()));
31
32pub static REGEX_TABLE: LazyLock<Mutex<HashMap<String, Regex>>> = LazyLock::new(|| {
33 Mutex::new({
34 let a = TABLE.lock().unwrap();
35 a.iter()
36 .map(|(k, v)| (k.clone(), Regex::new(&make_regex(v)).unwrap()))
37 .collect()
38 })
39});
40
41pub static FALLBACK_REGEX: LazyLock<Regex> = LazyLock::new(|| {
42 Regex::new(r#"(//+|#+|;+|--+):=\s+(.*)"#).unwrap()
43});
44
45pub static KEYWORDS_REGEX: LazyLock<Mutex<Option<Regex>>> = LazyLock::new(|| Mutex::new(None));
46
47fn update_table(raw_json: &str) {
49 let new_table: HashMap<String, Vec<String>> = serde_json::from_str(raw_json).unwrap();
50
51 let mut table = TABLE.lock().unwrap();
52 for (k, v) in new_table.iter() {
53 table.insert(k.clone(), v.clone());
54 }
55
56 let mut re_table = REGEX_TABLE.lock().unwrap();
57 table
58 .iter()
59 .map(|(k, v)| (k.clone(), Regex::new(&make_regex(v)).unwrap()))
60 .for_each(|(k, v)| {
61 let _ = re_table.insert(k, v);
62 });
63}
64
65fn make_regex(com_syms: &Vec<String>) -> String {
67 let mut head = String::new();
68 for s in com_syms {
69 head.push('|');
70 head.push_str(s);
71 head.push_str("+");
72 }
73
74 let _ = head.drain(..1).collect::<String>();
75
76 format!("({}):=\\s+(.*)", head)
77}
78
79pub(super) fn make_key_regex(keywords: &Vec<String>) {
81 let mut ss = String::new();
82 for s in keywords {
83 ss.push_str(&s);
84 ss.push('|');
85 }
86
87 let _ = ss.drain(ss.len() - 1..).collect::<String>();
88 let mut kk = KEYWORDS_REGEX.lock().unwrap();
89 *kk = Some(
90 RegexBuilder::new(&format!("({}):\\s*(.*)", ss))
91 .case_insensitive(true)
92 .build()
93 .unwrap(),
94 );
95}
96
97pub fn clean_keywords_table() {
98 let mut kk = KEYWORDS_REGEX.lock().unwrap();
99 *kk = None;
100}
101
102#[derive(Clone, Debug)]
103pub(super) enum OutputFormat {
104 None,
105 Json,
106 List,
107 Range,
108}
109
110impl Default for OutputFormat {
111 fn default() -> Self {
112 Self::None
113 }
114}
115
116#[derive(Default, Debug, Clone)]
118pub struct Config {
119 pub(super) filetypes: Vec<OsString>,
120 pub(super) ignore_dirs: Vec<OsString>,
121 pub(super) files: Vec<String>,
122
123 pub(super) delete: bool,
125
126 pub(super) restore: bool,
128
129 pub(super) output: OutputFormat,
131
132 pub(super) show_ignored: bool,
134
135 pub(super) range: u32,
137}
138
139impl From<&Args> for Config {
140 fn from(a: &Args) -> Self {
141 match &a.jsonx {
142 Some(j) => {
143 let mut buf = vec![];
144 File::open(j).unwrap().read_to_end(&mut buf).unwrap();
145 update_table(&String::from_utf8(buf).unwrap());
146 }
147 None => (),
148 }
149
150 match &a.keywords {
151 Some(kk) => make_key_regex(&kk),
152 None => (),
153 }
154
155 let output = match &a.output_format {
156 Some(v) if v.to_lowercase().as_str() == "json" => OutputFormat::Json,
157 Some(v) if v.to_lowercase().as_str() == "list" => OutputFormat::List,
158 None if a.range > 0 => OutputFormat::Range,
159 _ => OutputFormat::None,
160 };
161
162 Self {
163 filetypes: a.filetypes.clone(),
164 ignore_dirs: a.ignore_dirs.clone(),
165 files: a.targets.clone(),
166
167 delete: a.delete,
168 restore: if a.delete { false } else { a.restore },
171
172 output,
173 show_ignored: a.show_ignore,
174
175 range: a.range,
176 }
177 }
178}
179
180#[cfg(test)]
181mod tests {
182 use super::*;
183 use regex::Regex;
184
185 #[test]
186 fn test_update_table() {
187 assert_eq!(
188 TABLE.lock().unwrap().get("rs").unwrap(),
189 &vec![String::from("//"), String::from(r#"/\*"#)]
190 );
191
192 assert_eq!(
193 REGEX_TABLE.lock().unwrap().get("rs").unwrap().as_str(),
194 &String::from(r#"(//+|/\*+):=\s+(.*)"#)
195 );
196
197 update_table(r##"{"rs":["//","#"]}"##);
199
200 assert_eq!(
201 TABLE.lock().unwrap().get("rs").unwrap(),
202 &vec![String::from("//"), String::from("#")]
203 );
204
205 assert_eq!(
206 REGEX_TABLE.lock().unwrap().get("rs").unwrap().as_str(),
207 &String::from(r#"(//+|#+):=\s+(.*)"#)
208 );
209
210 update_table(r#"{"rs":["//","/\\*"]}"#);
212
213 assert_eq!(
214 TABLE.lock().unwrap().get("rs").unwrap(),
215 &vec![String::from("//"), String::from("/\\*")]
216 );
217
218 assert_eq!(
219 TABLE.lock().unwrap().get("rs").unwrap(),
220 &vec![String::from("//"), String::from(r#"/\*"#)]
221 );
222
223 assert_eq!(
224 REGEX_TABLE.lock().unwrap().get("rs").unwrap().as_str(),
225 &String::from(r#"(//+|/\*+):=\s+(.*)"#)
226 );
227 }
228
229 #[test]
230 fn test_update_table_with_json() {
231 let mut buf = vec![];
232 File::open("./tests/testcases/test.json")
233 .unwrap()
234 .read_to_end(&mut buf)
235 .unwrap();
236 let ss: &str = &String::from_utf8(buf).unwrap();
237
238 assert_eq!(
241 serde_json::from_str::<HashMap<String, Vec<String>>>(ss).unwrap(),
242 serde_json::from_str(r#"{"rs":["//","/\\*"]}"#).unwrap() );
244 }
245
246 #[test]
247 fn test_make_regex() {
248 assert_eq!(
249 make_regex(&vec![String::from("//"), String::from(";")]),
250 String::from(r#"(//+|;+):=\s+(.*)"#)
251 );
252
253 assert_eq!(
254 make_regex(&vec![String::from("//"), String::from(r#"/\*"#)]),
255 String::from(r#"(//+|/\*+):=\s+(.*)"#)
256 );
257 }
258
259 #[test]
260 fn test_regex() {
261 let re = Regex::new(&make_regex(&vec![String::from("--"), String::from(";")])).unwrap();
262 let cap = re.captures("Aabbcc --:= test").unwrap();
263 assert_eq!(&cap[2], "test");
264
265 let cap = re.captures("Aabbcc ;:= test").unwrap();
266 assert_eq!(&cap[2], "test");
267
268 let cap = re.captures("Aabbcc ;;;:= test").unwrap();
269 assert_eq!(&cap[2], "test");
270 assert_eq!(&cap[1], ";;;");
271
272 assert!(re.captures("Aabbcc #:= test").is_none());
273
274 assert!(re.captures("Aabbcc ; test").is_none());
275
276 assert!(re.captures("Aabbcc ; := test").is_none());
277
278 let re = Regex::new(&make_regex(&vec![
280 String::from("//"),
281 String::from(r#"/\*"#),
282 String::from(r#"// "#),
283 ]))
284 .unwrap();
285 assert!(re.captures("err := test").is_none());
286 assert!(re.captures("err // := test").is_some());
287 assert_eq!(&re.captures("err // := test").unwrap()[1], "// ");
288 }
289
290 #[test]
291 fn test_restore_overwrited_by_delete() {
292 let mut arg: Args = Default::default();
293 arg.delete = true;
294 arg.restore = true;
295 let conf = Config::from(&arg);
296 assert!(conf.delete);
297 assert!(!conf.restore);
298
299 arg.delete = false;
300 arg.restore = true;
301 let conf = Config::from(&arg);
302 assert!(!conf.delete);
303 assert!(conf.restore);
304 }
305}