Skip to main content

refining_char/
ascii.rs

1//! ASCII character predicates.
2
3use core::{fmt, marker::PhantomData};
4
5use refining_core::predicate::Predicate;
6
7use crate::internals::predicate;
8
9/// Represents integer base for checks.
10pub type Base = u32;
11
12/// The octal base.
13pub const OCTAL: Base = 8;
14
15/// The decimal base.
16pub const DECIMAL: Base = 10;
17
18/// The hexadecimal base.
19pub const HEXADECIMAL: Base = 16;
20
21/// Checks whether the given character is a digit in the specified base `B`.
22///
23/// The default base is [`DECIMAL`].
24pub struct CharDigit<const B: Base = DECIMAL> {
25    private: PhantomData<()>,
26}
27
28/// [`CharDigit<B>`] with `B` set to [`DECIMAL`].
29pub type CharDecDigit = CharDigit<DECIMAL>;
30
31/// [`CharDigit<B>`] with `B` set to [`OCTAL`].
32pub type CharOctDigit = CharDigit<OCTAL>;
33
34/// [`CharDigit<B>`] with `B` set to [`HEXADECIMAL`].
35pub type CharHexDigit = CharDigit<HEXADECIMAL>;
36
37impl<const B: Base> CharDigit<B> {
38    /// Returns the base `B` for which this predicate checks.
39    #[must_use]
40    pub const fn base() -> Base {
41        B
42    }
43}
44
45impl<const B: Base> Predicate<char> for CharDigit<B> {
46    fn check(value: &char) -> bool {
47        value.is_digit(Self::base())
48    }
49
50    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(formatter, "digit in base `{base}`", base = Self::base())
52    }
53
54    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(formatter, "char::digit<{base}>", base = Self::base())
56    }
57}
58
59predicate! {
60    Name = CharAsciiAlphabetic,
61    Method = is_ascii_alphabetic,
62    Doc = "Checks whether the given character is ASCII alphabetic.",
63    Expected = "ascii alphabetic character",
64    Code = char::ascii::alphabetic,
65}
66
67predicate! {
68    Name = CharAsciiAlphanumeric,
69    Method = is_ascii_alphanumeric,
70    Doc = "Checks whether the given character is ASCII alphanumeric.",
71    Expected = "ascii alphanumeric character",
72    Code = char::ascii::alphanumeric,
73}
74
75predicate! {
76    Name = CharAsciiControl,
77    Method = is_ascii_control,
78    Doc = "Checks whether the given character is ASCII control.",
79    Expected = "ascii control character",
80    Code = char::ascii::control,
81}
82
83predicate! {
84    Name = CharAsciiGraphic,
85    Method = is_ascii_graphic,
86    Doc = "Checks whether the given character is ASCII graphic.",
87    Expected = "ascii graphic character",
88    Code = char::ascii::graphic,
89}
90
91predicate! {
92    Name = CharAsciiPunctuation,
93    Method = is_ascii_punctuation,
94    Doc = "Checks whether the given character is ASCII punctuation.",
95    Expected = "ascii punctuation character",
96    Code = char::ascii::punctuation,
97}
98
99predicate! {
100    Name = CharAsciiLowercase,
101    Method = is_ascii_lowercase,
102    Doc = "Checks whether the given character is ASCII lowercase.",
103    Expected = "ascii lowercase character",
104    Code = char::ascii::lowercase,
105}
106
107predicate! {
108    Name = CharAsciiUppercase,
109    Method = is_ascii_uppercase,
110    Doc = "Checks whether the given character is ASCII uppercase.",
111    Expected = "ascii uppercase character",
112    Code = char::ascii::uppercase,
113}
114
115predicate! {
116    Name = CharAsciiWhitespace,
117    Method = is_ascii_whitespace,
118    Doc = "Checks whether the given character is ASCII whitespace.",
119    Expected = "ascii whitespace character",
120    Code = char::ascii::whitespace,
121}