use crate::*; pub fn format_inhx(segments: &[Segment]) -> Result, FormatError> { let mut records = Vec::new(); let mut address; for segment in segments { address = segment.address; for chunk in segment.words.chunks(16) { records.push(data_record(chunk, address)?); address += 16; } } records.push(terminating_record()); let mut output = String::new(); for record in records { output.push_str(&record.to_string()); } return Ok(output.into_bytes()); } fn data_record(words: &[Tracked], address: usize) -> Result { let Ok(address) = u16::try_from(address) else { return Err(FormatError::AddressTooLarge(u16::MAX as usize, address)); }; let mut record = InhxRecord::new(); record.byte((words.len()) as u8); record.be_double(address); record.byte(0x00); for word in words { if word.value.width > 8 { return Err(FormatError::WordTooWide(8, word.width, word.source.clone())); } record.byte(word.value.value as u8); } return Ok(record); } fn terminating_record() -> InhxRecord { let mut record = InhxRecord::new(); record.byte(0x00); record.be_double(0x0000); record.byte(0x01); return record; }