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

/// Defines the start, end and both bounds of a word
pub enum WordBounds {
  None,
  Start,
  End,
  Both,
}

impl WordBounds {
  /// Convert word bounds 
  pub fn to_pattern(&self, word: &str) -> String {
    match self {
      WordBounds::Start => [r#"\b"#, word].concat(),
      WordBounds::End => [word, r#"\b"#].concat(),
      WordBounds::Both => [r#"\b"#, word, r#"\b"#].concat(),
      _ => word.to_owned(),
    }
  }
}

#[derive(Debug, Clone)]
pub enum StringBounds<'a> {
  StartsWith(&'a str, bool),
  EndsWith(&'a str, bool),
  Contains(&'a str, bool)
}

impl<'a> StringBounds<'a> {

  pub fn new(mode: u8, txt: &'a str, case_insensitive: bool) -> StringBounds<'a> {
    match mode {
      0 => StringBounds::StartsWith(txt, case_insensitive),
      1 => StringBounds::EndsWith(txt, case_insensitive),
      _ => StringBounds::Contains(txt, case_insensitive),
    }
  }

  pub fn case_insensitive(&self) -> bool {
    match self {
      Self::StartsWith(_, val) => val,
      Self::EndsWith(_, val) => val,
      Self::Contains(_, val) => val,
    }.to_owned()
  }

  pub fn pattern(&self) -> &'a str {
    match self {
      Self::StartsWith(txt, _) => txt,
      Self::EndsWith(txt, _) => txt,
      Self::Contains(txt, _) => txt,
    }.to_owned()
  }

}