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;

/// Replace a string inside another string.
///
/// This will look for any code containing the `${..}` sequence where `..` is
/// filled with the matches.
/// ```rust
/// # use replacer::rule::{Rule, StringRule};
/// # fn main() -> anyhow::Result<()> {
/// let rule = StringRule::new("replace", "world")?;
/// assert_eq!(rule.convert("Hello $$replace$$!")?, "Hello world!");
/// # Ok(())
/// # }
/// ```
pub struct StringRule {
    /// The keyword that will be matched with.
    /// This is the `${..}` part in the string.
    matches: String,
    /// What the keyword will be replaced with.
    replace_with: String,
}

impl Rule for StringRule {
    fn convert(&self, template: &str) -> Result<String> {
        Ok(template.replace(&self.matches, &self.replace_with))
    }
}

impl StringRule {
    /// Setup a new rule.
    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(())
    }
}