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
65
66
67
68
69
70
71
72
/// Defines the matching bounds of simple string matches with case-insensitive/sensitive variants
/// and accepting the string pattern and positivity flag as arguments
#[derive(Debug, Clone)]
pub enum StringBounds<'a> {
  StartsWithCi(&'a str, bool),
  EndsWithCi(&'a str, bool),
  ContainsCi(&'a str, bool),
  StartsWithCs(&'a str, bool),
  EndsWithCs(&'a str, bool),
  ContainsCs(&'a str, bool),
}

impl<'a> StringBounds<'a> {

  // Only used internally in utils
  // 0: starts with, 1 ends with, 2 (default) contains
  pub fn new(mode: u8, txt: &'a str, is_positive: bool, case_insensitive: bool) -> StringBounds<'a> {
    match mode {
      0 => if case_insensitive {
        Self::StartsWithCi(txt, is_positive)
      } else {
        Self::StartsWithCs(txt, is_positive)
      },
      1 => if case_insensitive {
        Self::EndsWithCi(txt, is_positive)
      } else {
        Self::EndsWithCs(txt, is_positive)
      },
      _ => if case_insensitive {
        Self::ContainsCi(txt, is_positive)
      } else {
        Self::ContainsCs(txt, is_positive)
      },
    }
  }

  pub fn case_insensitive(&self) -> bool {
    match self {
      Self::StartsWithCi(..) | Self::EndsWithCi(..) | Self::ContainsCi(..) => true,
      _ => false, 
    }
  }

  pub fn pattern(&self) -> &'a str {
    match self {
      Self::StartsWithCi(txt, _) | Self::EndsWithCi(txt, _) | Self::ContainsCi(txt, _) |
      Self::StartsWithCs(txt, _) | Self::EndsWithCs(txt, _) | Self::ContainsCs(txt, _ ) => txt,
    }.to_owned()
  }

  pub fn is_positive(&self) -> bool {
    match self {
      Self::StartsWithCi(_, is_pos) | Self::EndsWithCi(_, is_pos) | Self::ContainsCi(_, is_pos) |
      Self::StartsWithCs(_, is_pos) | Self::EndsWithCs(_, is_pos) | Self::ContainsCs(_, is_pos) => is_pos,
    }.to_owned()
  }

  pub fn starts_with(&self) -> bool {
    match self {
      Self::StartsWithCi(..) | Self::StartsWithCs(..) => true,
      _ => false
    }
  }

  pub fn ends_with(&self) -> bool {
    match self {
      Self::EndsWithCi(..) | Self::EndsWithCs(..) => true,
      _ => false
    }
  }

}