ctb_formats_eite/
exceptions.rs1use crate::utilities::*;
23
24use anyhow::Result;
25
26use crate::util::array::str_print_arr;
27
28pub 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
65pub 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}