simple_string_patterns/
enums.rs

1/// Defines the matching bounds of simple string matches with case-insensitive/sensitive variants
2/// and accepting the string pattern and positivity flag as arguments
3#[derive(Debug, Clone)]
4pub enum StringBounds<'a> {
5  StartsWith(&'a str, bool, CaseMatchMode),
6  EndsWith(&'a str, bool, CaseMatchMode),
7  Contains(&'a str, bool, CaseMatchMode),
8  Whole(&'a str, bool, CaseMatchMode),
9  And(Vec<StringBounds<'a>>),
10  Or(Vec<StringBounds<'a>>)
11}
12
13impl<'a> StringBounds<'a> {
14
15  // Only used internally in utils
16  // 0: starts with, 1 ends with, 2 (default) contains, 3 whole
17  pub fn new(mode: BoundsPosition, txt: &'a str, is_positive: bool, case_mode: CaseMatchMode) -> StringBounds<'a> {
18    match mode {
19      BoundsPosition::Starts =>  Self::StartsWith(txt, is_positive, case_mode),
20      BoundsPosition::Ends => Self::EndsWith(txt, is_positive, case_mode),
21      BoundsPosition::Whole => Self::Whole(txt, is_positive, case_mode),
22      _ => Self::Contains(txt, is_positive, case_mode),
23    }
24  }
25
26  pub fn case_insensitive(&self) -> bool {
27    match self {
28      Self::StartsWith(_, _, cm) | Self::EndsWith(_, _, cm) | Self::Contains(_, _, cm) | Self::Whole(_, _, cm) => {
29        match cm {
30          CaseMatchMode::Sensitive => false,
31          _ => true,
32        }
33      },
34      _ => false, 
35    }
36  }
37
38  pub fn case_mode(&self) -> CaseMatchMode {
39    match self {
40      Self::StartsWith(_, _, cm) | Self::EndsWith(_, _, cm) | Self::Contains(_, _, cm) | Self::Whole(_, _, cm) => {
41        *cm
42      },
43      _ => CaseMatchMode::Sensitive, 
44    }
45  }
46
47  pub fn pattern(&self) -> &'a str {
48    match self {
49      Self::StartsWith(txt, _, _) | Self::EndsWith(txt, _, _) |
50      Self::Contains(txt, _, _) | Self::Whole(txt, _, _)
51      => txt,
52      _ => &""
53    }.to_owned()
54  }
55
56  pub fn is_positive(&self) -> bool {
57    match self {
58      Self::StartsWith(_, is_pos, _) | Self::EndsWith(_, is_pos, _) |
59      Self::Contains(_, is_pos, _) | Self::Whole(_, is_pos, _) => is_pos,
60      _ => &false,
61    }.to_owned()
62  }
63
64  pub fn starts_with(&self) -> bool {
65    match self {
66      Self::StartsWith(..) => true,
67      _ => false
68    }
69  }
70
71  pub fn ends_with(&self) -> bool {
72    match self {
73      Self::EndsWith(..) => true,
74      _ => false
75    }
76  }
77
78  pub fn matches_whole(&self) -> bool {
79    match self {
80      Self::Whole(..)=> true,
81      _ => false
82    }
83  }
84
85}
86
87
88/// Simple enum to define position only, unlinke StringBounds methods with patterns and matching options
89#[derive(Debug, Clone, Copy)]
90pub enum BoundsPosition {
91  Starts,
92  Ends,
93  Contains,
94  Whole
95}
96
97/// Core matching mode corresponding to function name suffixes (_cs, _ci and _ci_alphanum)
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub enum CaseMatchMode {
100  Sensitive,
101  Insensitive,
102  AlphanumInsensitive,
103}
104
105impl CaseMatchMode {
106  /// Determines if case match mode requires the sample string and pattern to be lower-cased
107  pub fn insensitive(case_insensitive: bool) -> Self {
108    if case_insensitive { 
109      Self::Insensitive
110    } else {
111      Self::Sensitive
112    }
113  }
114}