use std::{
fs::File,
io::{BufReader, Read, Seek},
path::Path,
};
use crate::{ddr::DataDescriptiveRecord, dr::DataRecord, Reader, Result};
pub struct DataDescriptiveFile {
data_descriptive_record: DataDescriptiveRecord,
data_records: Vec<DataRecord>,
}
impl DataDescriptiveFile {
pub fn read<P: AsRef<Path>>(path: P) -> Result<DataDescriptiveFile> {
let file = File::open(path)?;
let buffer = BufReader::new(file);
DataDescriptiveFile::read_buf(buffer)
}
pub fn read_buf<T: Read + Seek>(buffer: BufReader<T>) -> Result<DataDescriptiveFile> {
let mut reader = Reader::new(buffer);
let data_descriptive_record = DataDescriptiveRecord::read(&mut reader)?;
let mut data_records = Vec::new();
while !reader.is_eof()? {
let data_record = DataRecord::read(&mut reader)?;
data_records.push(data_record);
}
Ok(DataDescriptiveFile {
data_descriptive_record,
data_records,
})
}
pub fn data_descriptive_record(&self) -> &DataDescriptiveRecord {
&self.data_descriptive_record
}
pub fn data_records(&self) -> &[DataRecord] {
&self.data_records
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::io::{BufReader, Cursor};
use crate::{tests::to_bytes, DataDescriptiveFile};
#[test]
fn test_ddr_leader() {
let bytes = [
to_bytes("30 31 37 37 35 33 4c 45 31 20 30 39 30 30 32 35"),
to_bytes("36 20 21 20 33 34 30 34 30 30 30 30 31 32 33 30"),
to_bytes("30 30 30 44 53 49 44 31 33 30 30 31 32 33 44 53"),
to_bytes("53 49 31 31 38 30 32 35 33 41 54 43 53 30 34 34"),
to_bytes("30 33 37 31 46 54 43 53 30 34 37 30 34 31 35 41"),
to_bytes("54 54 52 30 35 38 30 34 36 32 43 53 49 44 30 38"),
to_bytes("34 30 35 32 30 43 52 53 48 30 39 35 30 36 30 34"),
to_bytes("43 53 41 58 30 35 30 30 36 39 39 56 44 41 54 30"),
to_bytes("35 35 30 37 34 39 43 32 49 54 30 35 35 30 38 30"),
to_bytes("34 43 32 49 4c 30 35 35 30 38 35 39 50 52 49 44"),
to_bytes("30 37 31 30 39 31 34 43 52 49 44 30 37 31 30 39"),
to_bytes("38 35 50 54 41 53 30 35 37 31 30 35 36 53 45 47"),
to_bytes("48 30 33 35 31 31 31 33 53 52 49 44 30 37 33 31"),
to_bytes("31 34 38 52 49 41 53 30 36 37 31 32 32 31 46 52"),
to_bytes("49 44 30 38 34 31 32 38 38 46 4f 49 44 30 36 34"),
to_bytes("31 33 37 32 53 50 41 53 30 38 33 31 34 33 36 1e"),
to_bytes("30 30 30 30 3b 26 20 20 20 1f 44 53 49 44 44 53"),
to_bytes("53 49 44 53 49 44 41 54 43 53 44 53 49 44 46 54"),
to_bytes("43 53 43 53 49 44 43 52 53 48 43 52 53 48 43 53"),
to_bytes("41 58 43 52 53 48 56 44 41 54 50 52 49 44 43 32"),
to_bytes("49 54 43 52 49 44 50 54 41 53 43 52 49 44 53 45"),
to_bytes("47 48 53 45 47 48 43 32 49 4c 53 52 49 44 52 49"),
to_bytes("41 53 46 52 49 44 46 4f 49 44 46 52 49 44 41 54"),
to_bytes("54 52 46 52 49 44 53 50 41 53 1e 33 36 30 30 3b"),
to_bytes("26 25 2f 47 44 61 74 61 20 53 65 74 20 49 64 65"),
to_bytes("6e 74 69 66 69 63 61 74 69 6f 6e 1f 52 43 4e 4d"),
to_bytes("21 52 43 49 44 21 45 4e 53 50 21 45 4e 45 44 21"),
to_bytes("50 52 53 50 21 50 52 45 44 21 50 52 4f 46 21 44"),
to_bytes("53 4e 4d 21 44 53 54 4c 21 44 53 52 44 21 44 53"),
to_bytes("4c 47 21 44 53 41 42 21 44 53 45 44 5c 5c 2a 44"),
to_bytes("53 54 43 1f 28 62 31 31 2c 62 31 34 2c 37 41 2c"),
to_bytes("41 28 38 29 2c 33 41 2c 62 31 31 29 1e 31 36 30"),
to_bytes("30 3b 26 20 20 20 44 61 74 61 20 53 65 74 20 53"),
to_bytes("74 72 75 63 74 75 72 65 20 49 6e 66 6f 72 6d 61"),
to_bytes("74 69 6f 6e 1f 44 43 4f 58 21 44 43 4f 59 21 44"),
to_bytes("43 4f 5a 21 43 4d 46 58 21 43 4d 46 59 21 43 4d"),
to_bytes("46 5a 21 4e 4f 49 52 21 4e 4f 50 4e 21 4e 4f 4d"),
to_bytes("4e 21 4e 4f 43 4e 21 4e 4f 58 4e 21 4e 4f 53 4e"),
to_bytes("21 4e 4f 46 52 1f 28 33 62 34 38 2c 31 30 62 31"),
to_bytes("34 29 1e 32 36 30 30 3b 26 20 20 20 41 74 74 72"),
to_bytes("69 62 75 74 65 20 43 6f 64 65 73 1f 2a 41 54 43"),
to_bytes("44 21 41 4e 43 44 1f 28 41 2c 62 31 32 29 1e 32"),
to_bytes("36 30 30 3b 26 20 20 20 46 65 61 74 75 72 65 20"),
to_bytes("54 79 70 65 20 43 6f 64 65 73 1f 2a 46 54 43 44"),
to_bytes("21 46 54 4e 43 1f 28 41 2c 62 31 32 29 1e 32 36"),
to_bytes("30 30 3b 26 25 2f 47 41 74 74 72 69 62 75 74 65"),
to_bytes("1f 2a 4e 41 54 43 21 41 54 49 58 21 50 41 49 58"),
to_bytes("21 41 54 49 4e 21 41 54 56 4c 1f 28 33 62 31 32"),
to_bytes("2c 62 31 31 2c 41 29 1e 31 31 30 30 3b 26 20 20"),
to_bytes("20 43 6f 6f 72 64 69 6e 61 74 65 20 52 65 66 65"),
to_bytes("72 65 6e 63 65 20 53 79 73 74 65 6d 20 52 65 63"),
to_bytes("6f 72 64 20 49 64 65 6e 74 69 66 69 65 72 1f 52"),
to_bytes("43 4e 4d 21 52 43 49 44 21 4e 43 52 43 1f 28 62"),
to_bytes("31 31 2c 62 31 34 2c 62 31 31 29 1e 31 36 30 30"),
to_bytes("3b 26 25 2f 47 43 6f 6f 72 64 69 6e 61 74 65 20"),
to_bytes("52 65 66 65 72 65 6e 63 65 20 53 79 73 74 65 6d"),
to_bytes("20 48 65 61 64 65 72 1f 43 52 49 58 21 43 52 53"),
to_bytes("54 21 43 53 54 59 21 43 52 4e 4d 21 43 52 53 49"),
to_bytes("21 43 52 53 53 21 53 43 52 49 1f 28 33 62 31 31"),
to_bytes("2c 32 41 2c 62 31 31 2c 41 29 1e 32 31 30 30 3b"),
to_bytes("26 20 20 20 43 6f 6f 72 64 69 6e 61 74 65 20 53"),
to_bytes("79 73 74 65 6d 20 41 78 65 73 1f 2a 41 58 54 59"),
to_bytes("21 41 58 55 4d 1f 28 32 62 31 31 29 1e 31 36 30"),
to_bytes("30 3b 26 25 2f 47 56 65 72 74 69 63 61 6c 20 44"),
to_bytes("61 74 75 6d 1f 44 54 4e 4d 21 44 54 49 44 21 44"),
to_bytes("54 53 52 21 53 43 52 49 1f 28 32 41 2c 62 31 31"),
to_bytes("2c 41 29 1e 32 31 30 30 3b 26 20 20 20 32 2d 44"),
to_bytes("20 49 6e 74 65 67 65 72 20 43 6f 6f 72 64 69 6e"),
to_bytes("61 74 65 20 54 75 70 6c 65 1f 59 43 4f 4f 21 58"),
to_bytes("43 4f 4f 1f 28 32 62 32 34 29 1e 32 31 30 30 3b"),
to_bytes("26 20 20 20 32 2d 44 20 49 6e 74 65 67 65 72 20"),
to_bytes("43 6f 6f 72 64 69 6e 61 74 65 20 4c 69 73 74 1f"),
to_bytes("2a 59 43 4f 4f 21 58 43 4f 4f 1f 28 32 62 32 34"),
to_bytes("29 1e 31 31 30 30 3b 26 20 20 20 50 6f 69 6e 74"),
to_bytes("20 52 65 63 6f 72 64 20 49 64 65 6e 74 69 66 69"),
to_bytes("65 72 1f 52 43 4e 4d 21 52 43 49 44 21 52 56 45"),
to_bytes("52 21 52 55 49 4e 1f 28 62 31 31 2c 62 31 34 2c"),
to_bytes("62 31 32 2c 62 31 31 29 1e 31 31 30 30 3b 26 20"),
to_bytes("20 20 43 75 72 76 65 20 52 65 63 6f 72 64 20 49"),
to_bytes("64 65 6e 74 69 66 69 65 72 1f 52 43 4e 4d 21 52"),
to_bytes("43 49 44 21 52 56 45 52 21 52 55 49 4e 1f 28 62"),
to_bytes("31 31 2c 62 31 34 2c 62 31 32 2c 62 31 31 29 1e"),
to_bytes("32 31 30 30 3b 26 20 20 20 50 6f 69 6e 74 20 41"),
to_bytes("73 73 6f 63 69 61 74 69 6f 6e 1f 2a 52 52 4e 4d"),
to_bytes("21 52 52 49 44 21 54 4f 50 49 1f 28 62 31 31 2c"),
to_bytes("62 31 34 2c 62 31 31 29 1e 31 31 30 30 3b 26 20"),
to_bytes("20 20 53 65 67 6d 65 6e 74 20 48 65 61 64 65 72"),
to_bytes("1f 49 4e 54 50 1f 28 62 31 31 29 1e 31 31 30 30"),
to_bytes("3b 26 20 20 20 53 75 72 66 61 63 65 20 52 65 63"),
to_bytes("6f 72 64 20 49 64 65 6e 74 69 66 69 65 72 1f 52"),
to_bytes("43 4e 4d 21 52 43 49 44 21 52 56 45 52 21 52 55"),
to_bytes("49 4e 1f 28 62 31 31 2c 62 31 34 2c 62 31 32 2c"),
to_bytes("62 31 31 29 1e 32 31 30 30 3b 26 20 20 20 52 69"),
to_bytes("6e 67 20 41 73 73 6f 63 69 61 74 69 6f 6e 1f 2a"),
to_bytes("52 52 4e 4d 21 52 52 49 44 21 4f 52 4e 54 21 55"),
to_bytes("53 41 47 21 52 41 55 49 1f 28 62 31 31 2c 62 31"),
to_bytes("34 2c 33 62 31 31 29 1e 31 31 30 30 3b 26 20 20"),
to_bytes("20 46 65 61 74 75 72 65 20 54 79 70 65 20 52 65"),
to_bytes("63 6f 72 64 20 49 64 65 6e 74 69 66 69 65 72 1f"),
to_bytes("52 43 4e 4d 21 52 43 49 44 21 4e 46 54 43 21 52"),
to_bytes("56 45 52 21 52 55 49 4e 1f 28 62 31 31 2c 62 31"),
to_bytes("34 2c 32 62 31 32 2c 62 31 31 29 1e 31 31 30 30"),
to_bytes("3b 26 20 20 20 46 65 61 74 75 72 65 20 4f 62 6a"),
to_bytes("65 63 74 20 49 64 65 6e 74 69 66 69 65 72 1f 41"),
to_bytes("47 45 4e 21 46 49 44 4e 21 46 49 44 53 1f 28 62"),
to_bytes("31 32 2c 62 31 34 2c 62 31 32 29 1e 32 31 30 30"),
to_bytes("3b 26 20 20 20 53 70 61 74 69 61 6c 20 41 73 73"),
to_bytes("6f 63 69 61 74 69 6f 6e 1f 2a 52 52 4e 4d 21 52"),
to_bytes("52 49 44 21 4f 52 4e 54 21 53 4d 49 4e 21 53 4d"),
to_bytes("41 58 21 53 41 55 49 1f 28 62 31 31 2c 62 31 34"),
to_bytes("2c 62 31 31 2c 32 62 31 34 2c 62 31 31 29 1e 30"),
to_bytes("30 36 32 33 20 44 20 20 20 20 20 30 30 30 36 35"),
to_bytes("20 20 20 33 33 30 34 44 53 49 44 30 39 39 30 30"),
to_bytes("30 44 53 53 49 30 36 35 30 39 39 41 54 43 53 33"),
to_bytes("31 32 31 36 34 46 54 43 53 30 38 32 34 37 36 1e"),
to_bytes("0a 01 00 00 00 53 2d 31 30 30 20 50 61 72 74 20"),
to_bytes("31 30 61 1f 31 2e 31 1f 49 4e 54 2e 49 48 4f 2e"),
to_bytes("53 2d 31 30 31 2e 31 2e 30 1f 31 2e 30 1f 31 1f"),
to_bytes("31 30 31 32 43 30 30 32 43 33 4e 45 57 43 43 2e"),
to_bytes("30 30 30 1f 32 43 33 4e 45 57 43 43 56 34 31 1f"),
to_bytes("32 30 32 30 30 37 31 33 45 4e 1f 1f 31 2e 30 1f"),
to_bytes("0e 12 1e 00 00 00 00 00 00 00 00 00 00 00 00 00"),
to_bytes("00 00 00 00 00 00 00 00 00 00 00 80 96 98 00 80"),
to_bytes("96 98 00 64 00 00 00 00 00 00 00 01 00 00 00 00"),
to_bytes("00 00 00 01 00 00 00 00 00 00 00 04 00 00 00 04"),
to_bytes("00 00 00 1e 6d 61 72 6b 73 4e 61 76 69 67 61 74"),
to_bytes("69 6f 6e 61 6c 53 79 73 74 65 6d 4f 66 1f 01 00"),
to_bytes("63 61 74 65 67 6f 72 79 4f 66 54 65 6d 70 6f 72"),
to_bytes("61 6c 56 61 72 69 61 74 69 6f 6e 1f 02 00 64 61"),
to_bytes("74 61 41 73 73 65 73 73 6d 65 6e 74 1f 03 00 66"),
to_bytes("65 61 74 75 72 65 73 44 65 74 65 63 74 65 64 1f"),
to_bytes("04 00 6c 65 61 73 74 44 65 70 74 68 4f 66 44 65"),
to_bytes("74 65 63 74 65 64 46 65 61 74 75 72 65 73 4d 65"),
to_bytes("61 73 75 72 65 64 1f 05 00 73 69 67 6e 69 66 69"),
to_bytes("63 61 6e 74 46 65 61 74 75 72 65 73 44 65 74 65"),
to_bytes("63 74 65 64 1f 06 00 66 75 6c 6c 53 65 61 66 6c"),
to_bytes("6f 6f 72 43 6f 76 65 72 61 67 65 41 63 68 69 65"),
to_bytes("76 65 64 1f 07 00 68 6f 72 69 7a 6f 6e 74 61 6c"),
to_bytes("50 6f 73 69 74 69 6f 6e 55 6e 63 65 72 74 61 69"),
to_bytes("6e 74 79 1f 08 00 75 6e 63 65 72 74 61 69 6e 74"),
to_bytes("79 46 69 78 65 64 1f 09 00 76 65 72 74 69 63 61"),
to_bytes("6c 55 6e 63 65 72 74 61 69 6e 74 79 1f 0a 00 6d"),
to_bytes("61 78 69 6d 75 6d 44 69 73 70 6c 61 79 53 63 61"),
to_bytes("6c 65 1f 0b 00 6d 69 6e 69 6d 75 6d 44 69 73 70"),
to_bytes("6c 61 79 53 63 61 6c 65 1f 0c 00 1e 4e 61 76 69"),
to_bytes("67 61 74 69 6f 6e 61 6c 53 79 73 74 65 6d 4f 66"),
to_bytes("4d 61 72 6b 73 1f 01 00 51 75 61 6c 69 74 79 4f"),
to_bytes("66 42 61 74 68 79 6d 65 74 72 69 63 44 61 74 61"),
to_bytes("1f 02 00 44 61 74 61 43 6f 76 65 72 61 67 65 1f"),
to_bytes("03 00 4c 61 6e 64 41 72 65 61 1f 04 00 1e 30 30"),
to_bytes("31 33 30 20 44 20 20 20 20 20 30 30 30 36 35 20"),
to_bytes("20 20 32 32 30 34 43 53 49 44 30 37 30 30 43 52"),
to_bytes("53 48 31 37 30 37 43 52 53 48 32 30 32 34 43 53"),
to_bytes("41 58 30 33 34 34 56 44 41 54 31 38 34 37 1e 0f"),
to_bytes("01 00 00 00 02 1e 01 01 01 57 47 53 38 34 1f 34"),
to_bytes("33 32 36 1f 02 1f 1e 02 05 03 6d 65 61 6e 53 65"),
to_bytes("61 4c 65 76 65 6c 1f 1f ff 1f 1e 0c 04 1e 6d 65"),
to_bytes("61 6e 53 65 61 4c 65 76 65 6c 1f 33 1f 02 1f 1e"),
to_bytes("30 30 30 35 35 20 44 20 20 20 20 20 30 30 30 33"),
to_bytes("37 20 20 20 31 31 30 34 50 52 49 44 39 30 43 32"),
to_bytes("49 54 39 39 1e 6e 01 00 00 00 01 00 01 1e 00 54"),
to_bytes("e1 ed 00 46 c3 23 1e 30 30 31 31 36 20 44 20 20"),
to_bytes("20 20 20 30 30 30 35 37 20 20 20 32 32 30 34 43"),
to_bytes("52 49 44 30 39 30 30 50 54 41 53 30 37 30 39 53"),
to_bytes("45 47 48 30 32 31 36 43 32 49 4c 34 31 31 38 1e"),
to_bytes("78 01 00 00 00 01 00 01 1e 6e 01 00 00 00 03 1e"),
to_bytes("04 1e 00 54 e1 ed 00 46 c3 23 c0 11 d2 ed 00 46"),
to_bytes("c3 23 c0 11 d2 ed f5 ba c0 23 00 54 e1 ed f5 ba"),
to_bytes("c0 23 00 54 e1 ed 00 46 c3 23 1e 30 30 30 35 35"),
to_bytes("20 44 20 20 20 20 20 30 30 30 33 37 20 20 20 31"),
to_bytes("31 30 34 53 52 49 44 39 30 52 49 41 53 39 39 1e"),
to_bytes("82 02 00 00 00 01 00 01 1e 78 01 00 00 00 01 01"),
to_bytes("01 1e 30 30 30 35 35 20 44 20 20 20 20 20 30 30"),
to_bytes("30 33 37 20 20 20 31 31 30 34 53 52 49 44 39 30"),
to_bytes("52 49 41 53 39 39 1e 82 03 00 00 00 01 00 01 1e"),
to_bytes("78 01 00 00 00 01 01 01 1e 30 30 30 35 35 20 44"),
to_bytes("20 20 20 20 20 30 30 30 33 37 20 20 20 31 31 30"),
to_bytes("34 53 52 49 44 39 30 52 49 41 53 39 39 1e 82 04"),
to_bytes("00 00 00 01 00 01 1e 78 01 00 00 00 01 01 01 1e"),
to_bytes("30 30 30 35 35 20 44 20 20 20 20 20 30 30 30 33"),
to_bytes("37 20 20 20 31 31 30 34 53 52 49 44 39 30 52 49"),
to_bytes("41 53 39 39 1e 82 01 00 00 00 01 00 01 1e 78 01"),
to_bytes("00 00 00 01 01 01 1e 30 30 31 30 33 20 44 20 20"),
to_bytes("20 20 20 30 30 30 35 37 20 20 20 32 32 30 34 46"),
to_bytes("52 49 44 31 31 30 30 46 4f 49 44 30 39 31 31 41"),
to_bytes("54 54 52 31 30 32 30 53 50 41 53 31 36 33 30 1e"),
to_bytes("64 01 00 00 00 01 00 01 00 01 1e 12 07 f1 0f 00"),
to_bytes("00 64 00 1e 01 00 01 00 00 00 01 39 1f 1e 82 01"),
to_bytes("00 00 00 01 00 00 00 00 ff ff ff ff 01 1e 30 30"),
to_bytes("31 38 33 20 44 20 20 20 20 20 30 30 30 36 31 20"),
to_bytes("20 20 32 33 30 34 46 52 49 44 31 31 30 30 30 46"),
to_bytes("4f 49 44 30 39 30 31 31 41 54 54 52 38 36 30 32"),
to_bytes("30 53 50 41 53 31 36 31 30 36 1e 64 02 00 00 00"),
to_bytes("02 00 01 00 01 1e 12 07 f2 0f 00 00 64 00 1e 02"),
to_bytes("00 01 00 00 00 01 36 1f 03 00 01 00 00 00 01 33"),
to_bytes("1f 04 00 01 00 00 00 01 1f 05 00 01 00 03 00 01"),
to_bytes("30 1f 06 00 01 00 03 00 01 30 1f 07 00 01 00 00"),
to_bytes("00 01 30 1f 08 00 01 00 00 00 01 1f 09 00 01 00"),
to_bytes("07 00 01 1f 0a 00 01 00 00 00 01 1f 09 00 01 00"),
to_bytes("09 00 01 1f 1e 82 02 00 00 00 01 00 00 00 00 ff"),
to_bytes("ff ff ff 01 1e 30 30 31 32 31 20 44 20 20 20 20"),
to_bytes("20 30 30 30 35 37 20 20 20 32 32 30 34 46 52 49"),
to_bytes("44 31 31 30 30 46 4f 49 44 30 39 31 31 41 54 54"),
to_bytes("52 32 38 32 30 53 50 41 53 31 36 34 38 1e 64 03"),
to_bytes("00 00 00 03 00 01 00 01 1e 12 07 f3 0f 00 00 64"),
to_bytes("00 1e 0b 00 01 00 00 00 01 39 30 30 30 30 1f 0c"),
to_bytes("00 01 00 00 00 01 31 38 30 30 30 30 1f 1e 82 03"),
to_bytes("00 00 00 01 00 00 00 00 ff ff ff ff 01 1e 30 30"),
to_bytes("30 38 35 20 44 20 20 20 20 20 30 30 30 34 39 20"),
to_bytes("20 20 32 32 30 34 46 52 49 44 31 31 30 30 46 4f"),
to_bytes("49 44 30 39 31 31 53 50 41 53 31 36 32 30 1e 64"),
to_bytes("04 00 00 00 04 00 01 00 01 1e 12 07 f4 0f 00 00"),
to_bytes("64 00 1e 82 04 00 00 00 01 00 00 00 00 ff ff ff"),
to_bytes("ff 01 1e"),
]
.concat();
let buffer = Cursor::new(&bytes);
let buffer = BufReader::new(buffer);
let result = DataDescriptiveFile::read_buf(buffer);
assert!(result.is_ok());
}
}