1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use anyhow::Result;
use crate::Rule;
pub struct StringRule {
matches: String,
replace_with: String,
}
impl Rule for StringRule {
fn convert(&self, template: &str) -> Result<String> {
Ok(template.replace(&self.matches, &self.replace_with))
}
}
impl StringRule {
pub fn new(matches: &str, replace_with: &str) -> Result<Self> {
Ok(Self {
matches: format!("$${}$$", matches),
replace_with: replace_with.to_string(),
})
}
}
#[cfg(test)]
mod tests {
use anyhow::Result;
use super::*;
#[test]
fn string_rule() -> Result<()> {
assert_eq!(
StringRule::new("replace", "world")?.convert("Hello $$replace$$!")?,
"Hello world!"
);
assert_eq!(
StringRule::new("replace", "world")?.convert("Hello world!")?,
"Hello world!"
);
assert_eq!(
StringRule::new("replace", "world")?.convert("Hello $$replace$$, bye $$replace$$!")?,
"Hello world, bye world!"
);
Ok(())
}
}