simple_string_patterns/
char_type.rs

1use std::ops::Range;
2
3/// Defines character group types with special custom types (Char, Chars, Range, Between)
4#[derive(Debug, Clone)]
5pub enum CharType<'a> {
6  Any,
7  DecDigit, // is_ascii_digit
8  Digit(u32), // define the number base, e.g. 16 for hexdecimal
9  Numeric, // as defined by the std library, i.e. a number-like character, but not decimal points or minus
10  AlphaNum,
11  Upper,
12  Lower,
13  Alpha,
14  Spaces,
15  Punctuation,
16  Char(char),
17  Chars(&'a [char]),
18  Range(Range<char>),
19  Between(char, char),
20}
21
22impl<'a> CharType<'a> {
23  pub fn is_in_range(&self, c_ref: &char) -> bool {
24    let c = c_ref.to_owned();
25    match self {
26      Self::Any => true,
27      Self::DecDigit => c.is_ascii_digit(),
28      Self::Digit(radix) => c.is_digit(*radix),
29      Self::Numeric => c.is_numeric(),
30      Self::AlphaNum => c.is_alphanumeric(),
31      Self::Lower => c.is_lowercase(),
32      Self::Upper => c.is_uppercase(),
33      Self::Alpha => c.is_alphabetic(),
34      Self::Spaces => c.is_whitespace(),
35      Self::Punctuation => c.is_ascii_punctuation(),
36      Self::Char(ch) => c == *ch,
37      Self::Chars(chars) => chars.contains(&c),
38      Self::Range(cr) => cr.contains(&c),
39      Self::Between(c1, c2) => c >= *c1 && c <= *c2,
40    }
41  }
42}