ctb_formats_eite/
exceptions.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//! Exception definitions and error code constants for the EITE engine.
21
22use crate::utilities::*;
23
24use anyhow::Result;
25
26use crate::util::array::str_print_arr;
27
28// ---------------
29// Exceptions / sentinel helpers
30// ---------------
31
32pub const DC_DATA_NO_RESULT_EXCEPTION: &str =
33    "89315802-d53d-4d11-ba5d-bf505e8ed454";
34pub const BYTE_ARRAY_FROM_BASENB_UTF8_INVALID_INPUT_EXCEPTION: &str =
35    "51 98 218 163 23 5 64 236 154 151 89 208 82 253 64 55 ";
36
37fn _excep_str(s: &str) -> bool {
38    s == DC_DATA_NO_RESULT_EXCEPTION
39        || s == BYTE_ARRAY_FROM_BASENB_UTF8_INVALID_INPUT_EXCEPTION
40}
41pub fn excep(s: &Result<String>) -> Result<bool> {
42    match s {
43        Ok(val) => Ok(_excep_str(val)),
44        Err(e) => {
45            let msg = e.to_string();
46            if _excep_str(&msg) {
47                Ok(true)
48            } else {
49                anyhow::bail!("Unexpected error: {msg}")
50            }
51        }
52    }
53}
54pub fn not_excep(s: &Result<String>) -> Result<bool> {
55    excep(s).map(|b| !b)
56}
57pub fn excep_arr<T: ToString>(arr: &[T]) -> Result<bool> {
58    let printed = str_print_arr(arr);
59    excep(&Ok(printed))
60}
61pub fn not_excep_arr<T: ToString>(arr: &[T]) -> Result<bool> {
62    excep_arr(arr).map(|b| !b)
63}
64
65/// Returns true if the result is empty or an exception marker value.
66/// Returns false if the result is not empty, or if the result is an Err other
67/// than an exception marker value.
68pub fn exc_or_empty(s: &Result<String>) -> Result<bool> {
69    if s.as_ref().is_ok_and(String::is_empty) {
70        Ok(true)
71    } else {
72        excep(s)
73    }
74}
75pub fn not_exc_or_empty(s: &Result<String>) -> Result<bool> {
76    exc_or_empty(s).map(|b| !b)
77}
78
79#[cfg(test)]
80#[allow(
81    clippy::panic,
82    clippy::expect_used,
83    clippy::unwrap_used,
84    clippy::unwrap_in_result,
85    clippy::panic_in_result_fn,
86    clippy::indexing_slicing,
87    clippy::arithmetic_side_effects,
88    reason = "Standard repository test boilerplate"
89)]
90mod tests {
91    use super::*;
92
93    use anyhow::anyhow;
94
95    #[crate::ctb_test]
96    fn test_exceptions() {
97        assert!(excep(&Ok(DC_DATA_NO_RESULT_EXCEPTION.to_string())).unwrap());
98        assert!(excep(&Err(anyhow!(DC_DATA_NO_RESULT_EXCEPTION))).unwrap());
99        assert!(not_excep(&Ok("normal".to_string())).unwrap());
100    }
101}