Skip to content

Commit 8303fa6

Browse files
committed
Better error handling in custom_highlighter
Signed-off-by: stropee <stropee@deltadore.com>
1 parent 1c191bd commit 8303fa6

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/custom_highlighter.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,37 @@ pub fn highlight_impl(
4545
my_tokens.remove(index);
4646
}
4747
}
48-
let set = RegexSet::new(my_tokens.clone()).unwrap();
48+
4949
let content_string = String::from(text);
5050
// let _ = file.read_to_string(&mut isi);
5151
let mut regexs:Vec<Regex> = Vec::new();
5252
for sentence in my_tokens.clone() {
53-
let re = Regex::new(&sentence).unwrap(); // should not panic because we parsed Regexes above already
54-
regexs.push(re);
53+
match Regex::new(&sentence){
54+
Ok(re) => {
55+
regexs.push(re);
56+
},
57+
Err(_err) =>{
58+
59+
},
60+
};
5561
}
5662

5763
let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new();
64+
match RegexSet::new(my_tokens.clone()){
65+
Ok(set) => {
66+
for idx in set.matches(&content_string).into_iter() {
67+
for caps in regexs[idx].captures_iter(&content_string) {
68+
highlight_list.push(HighLightElement::new(
69+
caps.get(0).unwrap().start(),
70+
caps.get(0).unwrap().end(),
71+
idx));
72+
}
73+
}
74+
},
75+
Err(_err) => {
5876

59-
for idx in set.matches(&content_string).into_iter() {
60-
for caps in regexs[idx].captures_iter(&content_string) {
61-
highlight_list.push(HighLightElement::new(
62-
caps.get(0).unwrap().start(),
63-
caps.get(0).unwrap().end(),
64-
idx));
6577
}
66-
}
78+
};
6779

6880
highlight_list.sort_by_key(|item| (item.pos_start, item.pos_end));
6981

0 commit comments

Comments
 (0)