use alice_protocol_reader::rdh::RDH;
#[derive(Debug)]
pub struct ItsRdhValidator<T: RDH> {
rdh: Option<T>,
}
impl<T: RDH> ItsRdhValidator<T> {
pub fn new(rdh: &T) -> Self {
Self {
rdh: Some(T::load(&mut rdh.to_byte_slice()).unwrap()),
}
}
pub fn rdh(&self) -> &T {
self.rdh.as_ref().expect(
"Attempted to borrow the current RDH before properly initializing the ItsRdhValidator",
)
}
pub fn check_at_ddw0(&self) -> Result<(), Vec<String>> {
let mut errors = Vec::<String>::new();
if self.rdh.as_ref().unwrap().stop_bit() != 1 {
errors.push("[E110] DDW0 observed but RDH stop bit is not 1".into());
}
if self.rdh.as_ref().unwrap().pages_counter() == 0 {
errors.push("[E111] DDW0 observed but RDH page counter is 0".into());
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
pub fn check_at_initial_ihw(&self) -> Result<(), Vec<String>> {
let mut errors = Vec::<String>::new();
if self.rdh.as_ref().unwrap().stop_bit() != 0 {
errors.push("[E12] IHW observed but RDH stop bit is not 0".into());
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl<T: RDH> Default for ItsRdhValidator<T> {
fn default() -> Self {
Self {
rdh: Default::default(),
}
}
}