mf4_rs/blocks/
data_block.rs1use crate::blocks::common::BlockHeader;
2use crate::blocks::common::BlockParse;
3use crate::error::MdfError;
4
5#[derive(Debug)]
6pub struct DataBlock<'a> {
7 pub header: BlockHeader,
8 pub data: &'a [u8],
9}
10
11impl<'a> BlockParse<'a> for DataBlock<'a> {
12 const ID: &'static str = "##DT";
13 fn from_bytes(bytes: &'a [u8]) -> Result<Self, MdfError> {
23
24 let header = BlockHeader::from_bytes(bytes)?;
25 if header.id != "##DT" && header.id != "##DV" {
26 return Err(MdfError::BlockIDError {
27 actual: header.id.clone(),
28 expected: "##DT / ##DV".to_string(),
29 });
30 }
31
32 let data_len = (header.block_len as usize).saturating_sub(24);
33 let expected_bytes = 24 + data_len;
34 if bytes.len() < expected_bytes {
35 return Err(MdfError::TooShortBuffer {
36 actual: bytes.len(),
37 expected: expected_bytes,
38 file: file!(),
39 line: line!(),
40 });
41 }
42 let data = &bytes[24..24 + data_len];
43 Ok(Self { header, data })
44 }
45}
46impl<'a> DataBlock<'a> {
47 pub fn records(&self, record_size: usize) -> impl Iterator<Item = &'a [u8]> {
56 self.data.chunks_exact(record_size)
57 }
58}