libsubconverter/models/
regex_match_config.rs1use serde::Deserialize;
2
3use crate::utils::{
4 matcher::{
5 apply_compiled_rule_to_string, compile_rule, replace_with_compiled_regex, CompiledRule,
6 },
7 reg_replace,
8};
9use regex::Regex;
10
11#[derive(Debug, Clone, Deserialize)]
13pub struct RegexMatchConfig {
14 #[serde(rename = "match")]
15 pub _match: String,
16 pub replace: String,
17 pub script: String,
18
19 #[serde(skip)]
21 pub compiled_rule: Option<CompiledRule>,
22 #[serde(skip)]
24 pub compiled_regex_for_replace: Option<Regex>,
25}
26
27impl RegexMatchConfig {
28 pub fn new(_match: String, replace: String, script: String) -> Self {
29 let mut config = Self {
30 _match,
31 replace,
32 script,
33 compiled_rule: None,
34 compiled_regex_for_replace: None,
35 };
36
37 config.compile();
38 config
39 }
40
41 pub fn compile(&mut self) {
44 self.compiled_rule = Some(compile_rule(&self._match));
45 self.compiled_regex_for_replace = Regex::new(&format!("(?i){}", self._match)).ok();
48 }
49
50 pub fn process(&self, remark: &mut String) {
51 let mut matched = false;
52
53 if let Some(compiled) = &self.compiled_rule {
55 matched = apply_compiled_rule_to_string(compiled, remark);
57 } else {
58 matched = crate::utils::matcher::reg_find(remark, &self._match);
61 }
62
63 if matched {
64 if let Some(re) = &self.compiled_regex_for_replace {
66 *remark = replace_with_compiled_regex(remark, re, &self.replace, true, false);
67 } else {
68 *remark = reg_replace(remark, &self._match, &self.replace, true, false);
70 }
71 }
72 }
73}
74
75pub type RegexMatchConfigs = Vec<RegexMatchConfig>;