ctoolbox/formats/eite/
exceptions.rs

1use anyhow::Result;
2
3use crate::formats::eite::util::array::str_print_arr;
4
5// ---------------
6// Exceptions / sentinel helpers
7// ---------------
8
9pub const DC_DATA_NO_RESULT_EXCEPTION: &str =
10    "89315802-d53d-4d11-ba5d-bf505e8ed454";
11pub const BYTE_ARRAY_FROM_BASENB_UTF8_INVALID_INPUT_EXCEPTION: &str =
12    "51 98 218 163 23 5 64 236 154 151 89 208 82 253 64 55 ";
13
14fn _excep_str(s: String) -> bool {
15    s == DC_DATA_NO_RESULT_EXCEPTION
16        || s == BYTE_ARRAY_FROM_BASENB_UTF8_INVALID_INPUT_EXCEPTION
17}
18pub fn excep(s: &Result<String>) -> bool {
19    match s {
20        Ok(_) => _excep_str(s.as_ref().unwrap().to_string()),
21        Err(e) => {
22            let msg = e.to_string();
23            if _excep_str(msg.clone()) {
24                true
25            } else {
26                panic!("Unexpected error: {msg}");
27            }
28        }
29    }
30}
31pub fn not_excep(s: &Result<String>) -> bool {
32    !excep(s)
33}
34pub fn excep_arr<T: ToString>(arr: &[T]) -> bool {
35    let printed = str_print_arr(arr);
36    excep(&Ok(printed))
37}
38pub fn not_excep_arr<T: ToString>(arr: &[T]) -> bool {
39    !excep_arr(arr)
40}
41
42/// Returns true if the result is empty or an exception marker value.
43/// Returns false if the result is not empty, or if the result is an Err other
44/// than an exception marker value.
45pub fn exc_or_empty(s: &Result<String>) -> bool {
46    (s.is_ok() && s.as_ref().unwrap().is_empty()) || excep(s)
47}
48pub fn not_exc_or_empty(s: &Result<String>) -> bool {
49    !exc_or_empty(s)
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    use anyhow::anyhow;
57
58    #[crate::ctb_test]
59    fn test_exceptions() {
60        assert!(excep(&Ok(DC_DATA_NO_RESULT_EXCEPTION.to_string())));
61        assert!(excep(&Err(anyhow!(DC_DATA_NO_RESULT_EXCEPTION))));
62        assert!(not_excep(&Ok("normal".to_string())));
63    }
64}