diff --git a/Cargo.toml b/Cargo.toml index e7b2ac0..30ca218 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "c3dio" -version = "0.1.0" +version = "0.2.0" authors = ["Claire V. Hammond "] edition = "2021" categories = ["encoding", "filesystem", "parser-implementation", "motion-capture", "biomechanics"] diff --git a/src/c3d.rs b/src/c3d.rs index 68b2335..9bf267c 100644 --- a/src/c3d.rs +++ b/src/c3d.rs @@ -32,13 +32,11 @@ impl C3d { /// Parses a C3D file from a byte slice. pub fn from_bytes(bytes: &[u8]) -> Result { - let c3d = C3d::new()? + Ok(C3d::new()? .parse_basic_info_from_bytes(bytes)? .parse_header()? .parse_parameters()? - .parse_data_from_bytes(bytes)?; - - Ok(c3d) + .parse_data_from_bytes(bytes)?) } /// Parses a C3D file with just the header data. @@ -148,7 +146,7 @@ impl C3d { return Err(C3dParseError::InsufficientBlocks("data".to_string())); } - self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index))] + self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index - 1))] .try_into() .unwrap(); @@ -203,7 +201,7 @@ impl C3d { } fn parse_data_from_bytes(mut self, bytes: &[u8]) -> Result { - let data_start_byte = 512 * (self.bytes.data_start_block_index); + let data_start_byte = 512 * (self.bytes.data_start_block_index - 1); if bytes.len() < data_start_byte { return Err(C3dParseError::InsufficientBlocks("data".to_string())); } diff --git a/tests/test_byte_and_file_parity.rs b/tests/test_byte_and_file_parity.rs new file mode 100644 index 0000000..ec8b96c --- /dev/null +++ b/tests/test_byte_and_file_parity.rs @@ -0,0 +1,19 @@ +use c3dio::C3d; +use std::fs::File; +use std::io::{BufReader, Read}; + +#[test] +fn compare_byte_to_file_load() { + let sample19_file = C3d::load("tests/data/short.c3d").unwrap(); + + let file = File::open("tests/data/short.c3d").unwrap(); + let mut buffer = BufReader::new(file); + let mut bytes = Vec::new(); + buffer.read_to_end(&mut bytes).unwrap(); + let sample19_bytes = C3d::from_bytes(&bytes).unwrap(); + + assert_eq!(sample19_file.parameters, sample19_bytes.parameters); + assert_eq!(sample19_file.events, sample19_bytes.events); + assert_eq!(sample19_file.data, sample19_bytes.data); +} +