Skip to main content

refining_char/
predicates.rs

1//! Character predicates.
2
3use core::{fmt, marker::PhantomData};
4
5use refining_core::{logical::And, predicate::Predicate};
6
7/// Checks whether the character is equal to `C`.
8pub struct CharEqual<const C: char> {
9    private: PhantomData<()>,
10}
11
12impl<const C: char> CharEqual<C> {
13    /// Returns the character `C` for which this predicate checks.
14    #[must_use]
15    pub const fn character() -> char {
16        C
17    }
18}
19
20impl<const C: char> Predicate<char> for CharEqual<C> {
21    fn check(value: &char) -> bool {
22        Self::check_value(*value)
23    }
24
25    fn check_value(value: char) -> bool {
26        value == Self::character()
27    }
28
29    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(
31            formatter,
32            "character == `{character}`",
33            character = Self::character()
34        )
35    }
36
37    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(
39            formatter,
40            "char::eq<{character}>",
41            character = Self::character()
42        )
43    }
44}
45
46/// Checks whether the character is not equal to `C`.
47pub struct CharNotEqual<const C: char> {
48    private: PhantomData<()>,
49}
50
51impl<const C: char> CharNotEqual<C> {
52    /// Returns the character `C` for which this predicate checks.
53    #[must_use]
54    pub const fn character() -> char {
55        C
56    }
57}
58
59impl<const C: char> Predicate<char> for CharNotEqual<C> {
60    fn check(value: &char) -> bool {
61        Self::check_value(*value)
62    }
63
64    fn check_value(value: char) -> bool {
65        value != Self::character()
66    }
67
68    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
69        write!(
70            formatter,
71            "character != `{character}`",
72            character = Self::character()
73        )
74    }
75
76    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(
78            formatter,
79            "char::ne<{character}>",
80            character = Self::character()
81        )
82    }
83}
84
85/// Checks whether the character is less than `C`.
86pub struct CharLess<const C: char> {
87    private: PhantomData<()>,
88}
89
90impl<const C: char> CharLess<C> {
91    /// Returns the character `C` for which this predicate checks.
92    #[must_use]
93    pub const fn character() -> char {
94        C
95    }
96}
97
98impl<const C: char> Predicate<char> for CharLess<C> {
99    fn check(value: &char) -> bool {
100        Self::check_value(*value)
101    }
102
103    fn check_value(value: char) -> bool {
104        value < Self::character()
105    }
106
107    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(
109            formatter,
110            "character < `{character}`",
111            character = Self::character()
112        )
113    }
114
115    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
116        write!(
117            formatter,
118            "char::lt<{character}>",
119            character = Self::character()
120        )
121    }
122}
123
124/// Checks whether the character is greater than `C`.
125pub struct CharGreater<const C: char> {
126    private: PhantomData<()>,
127}
128
129impl<const C: char> CharGreater<C> {
130    /// Returns the character `C` for which this predicate checks.
131    #[must_use]
132    pub const fn character() -> char {
133        C
134    }
135}
136
137impl<const C: char> Predicate<char> for CharGreater<C> {
138    fn check(value: &char) -> bool {
139        Self::check_value(*value)
140    }
141
142    fn check_value(value: char) -> bool {
143        value > Self::character()
144    }
145
146    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
147        write!(
148            formatter,
149            "character > `{character}`",
150            character = Self::character()
151        )
152    }
153
154    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
155        write!(
156            formatter,
157            "char::gt<{character}>",
158            character = Self::character()
159        )
160    }
161}
162
163/// Checks whether the character is less than or equal to `C`.
164pub struct CharLessOrEqual<const C: char> {
165    private: PhantomData<()>,
166}
167
168impl<const C: char> CharLessOrEqual<C> {
169    /// Returns the character `C` for which this predicate checks.
170    #[must_use]
171    pub const fn character() -> char {
172        C
173    }
174}
175
176impl<const C: char> Predicate<char> for CharLessOrEqual<C> {
177    fn check(value: &char) -> bool {
178        Self::check_value(*value)
179    }
180
181    fn check_value(value: char) -> bool {
182        value <= Self::character()
183    }
184
185    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
186        write!(
187            formatter,
188            "character <= `{character}`",
189            character = Self::character()
190        )
191    }
192
193    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
194        write!(
195            formatter,
196            "char::le<{character}>",
197            character = Self::character()
198        )
199    }
200}
201
202/// Checks whether the character is greater than or equal to `C`.
203pub struct CharGreaterOrEqual<const C: char> {
204    private: PhantomData<()>,
205}
206
207impl<const C: char> CharGreaterOrEqual<C> {
208    /// Returns the character `C` for which this predicate checks.
209    #[must_use]
210    pub const fn character() -> char {
211        C
212    }
213}
214
215impl<const C: char> Predicate<char> for CharGreaterOrEqual<C> {
216    fn check(value: &char) -> bool {
217        Self::check_value(*value)
218    }
219
220    fn check_value(value: char) -> bool {
221        value >= Self::character()
222    }
223
224    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
225        write!(
226            formatter,
227            "character >= `{character}`",
228            character = Self::character()
229        )
230    }
231
232    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
233        write!(
234            formatter,
235            "char::ge<{character}>",
236            character = Self::character()
237        )
238    }
239}
240
241/// Represents `(C, D)` intervals.
242pub type CharOpen<const C: char, const D: char> = And<CharGreater<C>, CharLess<D>>;
243
244/// Represents `[C, D)` intervals.
245pub type CharClosedOpen<const C: char, const D: char> = And<CharGreaterOrEqual<C>, CharLess<D>>;
246
247/// Represents `(C, D]` intervals.
248pub type CharOpenClosed<const C: char, const D: char> = And<CharGreater<C>, CharLessOrEqual<D>>;
249
250/// Represents `[C, D]` intervals.
251pub type CharClosed<const C: char, const D: char> = And<CharGreaterOrEqual<C>, CharLessOrEqual<D>>;
252
253/// Represents the null character.
254pub const NULL: char = '\0';
255
256/// Checks whether the character is [`NULL`].
257pub type CharNull = CharEqual<NULL>;
258
259/// Checks whether the character is non-[`NULL`].
260pub type CharNonNull = CharNotEqual<NULL>;