Skip to main content

refining_int/
bytes.rs

1//! Predicates for bytes.
2
3use core::{fmt, marker::PhantomData};
4
5use refining_core::{logical::Not, predicate::Predicate};
6
7use crate::{internals::predicate, predicates::u8};
8
9/// Represents the null byte.
10pub const NULL: u8 = b'\0';
11
12/// Checks whether the byte is [`NULL`].
13pub struct ByteNull {
14    private: PhantomData<()>,
15}
16
17/// The `null byte` literal.
18pub const NULL_MESSAGE: &str = "null byte";
19
20/// The `byte::null` literal.
21pub const NULL_CODE: &str = "byte::null";
22
23impl Predicate<u8> for ByteNull {
24    fn check(value: &u8) -> bool {
25        Self::check_value(*value)
26    }
27
28    fn check_value(value: u8) -> bool {
29        value == NULL
30    }
31
32    fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33        formatter.write_str(NULL_MESSAGE)
34    }
35
36    fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        formatter.write_str(NULL_CODE)
38    }
39}
40
41/// Checks whether the byte is non-[`NULL`].
42pub type ByteNonNull = Not<ByteNull>;
43
44predicate! {
45    Name = ByteAscii,
46    Method = is_ascii,
47    Doc = "Checks whether the given byte is within the ASCII range.",
48    Expected = "ascii byte",
49    Code = byte::ascii,
50}
51
52predicate! {
53    Name = ByteAsciiAlphabetic,
54    Method = is_ascii_alphabetic,
55    Doc = "Checks whether the given byte is an ASCII alphabetic character.",
56    Expected = "ascii alphabetic byte",
57    Code = byte::ascii::alphabetic,
58}
59
60predicate! {
61    Name = ByteAsciiAlphanumeric,
62    Method = is_ascii_alphanumeric,
63    Doc = "Checks whether the given byte is an ASCII alphanumeric character.",
64    Expected = "ascii alphanumeric byte",
65    Code = byte::ascii::alphanumeric,
66}
67
68predicate! {
69    Name = ByteAsciiControl,
70    Method = is_ascii_control,
71    Doc = "Checks whether the given byte is an ASCII control character.",
72    Expected = "ascii control byte",
73    Code = byte::ascii::control,
74}
75
76predicate! {
77    Name = ByteAsciiDigit,
78    Method = is_ascii_digit,
79    Doc = "Checks whether the given byte is an ASCII digit.",
80    Expected = "ascii digit byte",
81    Code = byte::ascii::digit,
82}
83
84predicate! {
85    Name = ByteAsciiGraphic,
86    Method = is_ascii_graphic,
87    Doc = "Checks whether the given byte is an ASCII graphic character.",
88    Expected = "ascii graphic byte",
89    Code = byte::ascii::graphic,
90}
91
92predicate! {
93    Name = ByteAsciiLowercase,
94    Method = is_ascii_lowercase,
95    Doc = "Checks whether the given byte is an ASCII lowercase character.",
96    Expected = "ascii lowercase byte",
97    Code = byte::ascii::lowercase,
98}
99
100predicate! {
101    Name = ByteAsciiPunctuation,
102    Method = is_ascii_punctuation,
103    Doc = "Checks whether the given byte is an ASCII punctuation character.",
104    Expected = "ascii punctuation byte",
105    Code = byte::ascii::punctuation,
106}
107
108predicate! {
109    Name = ByteAsciiUppercase,
110    Method = is_ascii_uppercase,
111    Doc = "Checks whether the given byte is an ASCII uppercase character.",
112    Expected = "ascii uppercase byte",
113    Code = byte::ascii::uppercase,
114}
115
116predicate! {
117    Name = ByteAsciiWhitespace,
118    Method = is_ascii_whitespace,
119    Doc = "Checks whether the given byte is an ASCII whitespace character.",
120    Expected = "ascii whitespace byte",
121    Code = byte::ascii::whitespace,
122}