mod inhx;
mod inhx32;

pub use inhx::*;
pub use inhx32::*;


pub struct InhxRecord {
    bytes: Vec<u8>,
}

impl InhxRecord {
    pub fn new() -> Self {
        Self { bytes: Vec::new() }
    }

    pub fn byte(&mut self, byte: u8) {
        self.bytes.push(byte);
    }

    pub fn be_double(&mut self, double: u16) {
        let [high, low] = double.to_be_bytes();
        self.byte(high);
        self.byte(low);
    }

    pub fn le_double(&mut self, double: u16) {
        let [high, low] = double.to_be_bytes();
        self.byte(low);
        self.byte(high);
    }

    pub fn to_string(self) -> String {
        let mut sum: u8 = 0;
        for byte in &self.bytes {
            sum = sum.wrapping_add(*byte);
        }
        let checksum = sum.wrapping_neg();
        let mut output = String::new();
        for byte in &self.bytes {
            output.push_str(&format!("{byte:0>2X}"));
        }
        format!(":{output}{checksum:0>2X}\n")
    }
}