ctb_formats_eite/util/
pred.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Character tests
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate::{
30    encoding::ascii::byte_from_stagel_char, util::math::int_is_between_i32,
31};
32
33pub fn is_byte(v: i32) -> bool {
34    int_is_between_i32(v, 0, 255)
35}
36pub fn is_int_bit(v: i32) -> bool {
37    v == 0 || v == 1
38}
39pub fn is_char_byte(v: i32) -> bool {
40    int_is_between_i32(v, 32, 126)
41}
42
43pub fn is_char_str(s: &str) -> bool {
44    if s.chars().count() != 1 {
45        return false;
46    }
47    let Ok(b) = byte_from_stagel_char(s) else {
48        return false;
49    };
50    is_char_byte(i32::from(b))
51}
52
53// ---------------
54// Ident validation (lowercase first letter, then alnum)
55// ---------------
56
57pub fn is_valid_ident(s: &str) -> bool {
58    let mut chars = s.chars();
59    let Some(first) = chars.next() else {
60        return false;
61    };
62    if !first.is_ascii_lowercase() {
63        return false;
64    }
65    chars.all(|c| c.is_ascii_alphanumeric())
66}
67
68#[cfg(test)]
69#[allow(
70    clippy::panic,
71    clippy::expect_used,
72    clippy::unwrap_used,
73    clippy::unwrap_in_result,
74    clippy::panic_in_result_fn,
75    clippy::indexing_slicing,
76    clippy::arithmetic_side_effects,
77    reason = "Standard repository test boilerplate"
78)]
79mod tests {
80    use super::*;
81
82    #[crate::ctb_test]
83    fn test_ident_validation() {
84        assert!(is_valid_ident("abc1"));
85        assert!(!is_valid_ident("Abc"));
86        assert!(!is_valid_ident("a-b"));
87    }
88}