fastpasta/analyze/validators/its/
data_words.rs

1//! Performs sanity checks on data words
2use crate::words::its::data_words as dw;
3use std::fmt::Write;
4
5pub mod ib;
6pub mod ob;
7
8/// Performs sanity checks on data words
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct DataWordSanityChecker;
11
12impl DataWordSanityChecker {
13    /// Checks is a valid IL/ML/OL data word
14    #[inline]
15    pub fn check_any(data_word: &[u8]) -> Result<(), String> {
16        let mut err_str = String::new();
17        let id = data_word[9];
18
19        if !Self::is_valid_any_id(id) {
20            write!(err_str, "ID is invalid: {id:#02X}").unwrap();
21            // Early return if ID is wrong
22            return Err(err_str);
23        }
24        Ok(())
25    }
26
27    #[inline]
28    fn is_valid_il_id(id: u8) -> bool {
29        dw::VALID_IL_ID.contains(&id)
30    }
31    #[inline]
32    fn is_valid_ml_id(id: u8) -> bool {
33        dw::VALID_ML_CONNECT0_ID.contains(&id)
34            || dw::VALID_ML_CONNECT1_ID.contains(&id)
35            || dw::VALID_ML_CONNECT2_ID.contains(&id)
36            || dw::VALID_ML_CONNECT3_ID.contains(&id)
37    }
38    #[inline]
39    fn is_valid_ol_id(id: u8) -> bool {
40        dw::VALID_OL_CONNECT0_ID.contains(&id)
41            || dw::VALID_OL_CONNECT1_ID.contains(&id)
42            || dw::VALID_OL_CONNECT2_ID.contains(&id)
43            || dw::VALID_OL_CONNECT3_ID.contains(&id)
44    }
45    #[inline]
46    fn is_valid_any_id(id: u8) -> bool {
47        Self::is_valid_il_id(id) || Self::is_valid_ml_id(id) || Self::is_valid_ol_id(id)
48    }
49}