1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Performs sanity checks on data words
use crate::words::its::data_words::*;
use std::fmt::Write;

/// Convenience const struct to avoid having to instantiate the struct elsewhere
pub const DATA_WORD_SANITY_CHECKER: DataWordSanityChecker = DataWordSanityChecker {};
/// Performs sanity checks on data words
pub struct DataWordSanityChecker {}

impl DataWordSanityChecker {
    /// Checks is a valid IL/ML/OL data word
    #[inline]
    pub fn check_any(&self, data_word: &[u8]) -> Result<(), String> {
        let mut err_str = String::new();
        let id = data_word[9];

        if !self.is_valid_any_id(id) {
            write!(err_str, "ID is invalid: {id:#02X}").unwrap();
            // Early return if ID is wrong
            return Err(err_str);
        }
        Ok(())
    }

    #[inline]
    fn is_valid_il_id(&self, id: u8) -> bool {
        VALID_IL_ID.contains(&id)
    }
    #[inline]
    fn is_valid_ml_id(&self, id: u8) -> bool {
        VALID_ML_CONNECT0_ID.contains(&id)
            || VALID_ML_CONNECT1_ID.contains(&id)
            || VALID_ML_CONNECT2_ID.contains(&id)
            || VALID_ML_CONNECT3_ID.contains(&id)
    }
    #[inline]
    fn is_valid_ol_id(&self, id: u8) -> bool {
        VALID_OL_CONNECT0_ID.contains(&id)
            || VALID_OL_CONNECT1_ID.contains(&id)
            || VALID_OL_CONNECT2_ID.contains(&id)
            || VALID_OL_CONNECT3_ID.contains(&id)
    }
    #[inline]
    fn is_valid_any_id(&self, id: u8) -> bool {
        self.is_valid_il_id(id) || self.is_valid_ml_id(id) || self.is_valid_ol_id(id)
    }
}