diff --git a/.cargo/config.toml b/.cargo/config.toml index e9ac139..320b8e2 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,2 @@ [alias] coverage = "tarpaulin --out Html --skip-clean --output-dir coverage" - diff --git a/src/data_to_text.rs b/src/data_to_text.rs index 789bf35..4f2ba87 100644 --- a/src/data_to_text.rs +++ b/src/data_to_text.rs @@ -5,7 +5,14 @@ impl DataToText { /// /// Convert a block of u8 data to text for user display skipping specified bytes pub fn data_to_text_window(to_convert: &[u8], offset: usize, length: usize) -> String { - DataToText::data_to_text(&to_convert[offset..(offset + length)]) + let convert_len = to_convert.len(); + + if offset >= convert_len { + return String::from(""); + } + + let end_of_window = offset.saturating_add(length).min(convert_len); + DataToText::data_to_text(&to_convert[offset..end_of_window]) } /// data_to_text diff --git a/src/lib.rs b/src/lib.rs index 6a2297a..2ae9027 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ pub mod number_system_conversion; pub mod test_compression; -pub mod data_to_text; \ No newline at end of file +pub mod data_to_text; diff --git a/src/test_compression.rs b/src/test_compression.rs index 828cdef..fc64828 100644 --- a/src/test_compression.rs +++ b/src/test_compression.rs @@ -100,27 +100,10 @@ impl TestCompression { } pub fn decompress_file_to_array(to_decompress: &Path) -> Vec { - let mut read_buffer = Vec::new(); - let mut file = File::open(to_decompress).expect( - format!( - "Unable to open file to decompress [{}]", - to_decompress.display() - ) - .as_str(), - ); - file.read_to_end(&mut read_buffer) - .expect(format!("Unable to read from [{}]", to_decompress.display()).as_str()); - read_buffer + let file = File::open(to_decompress).expect("Failed to open file"); + let mut decoder = DeflateDecoder::new(file); + let mut output = Vec::new(); + decoder.read_to_end(&mut output).expect("Failed to decompress"); + output } - - // let file = File::open(input_path) - // .map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e))); - // - // let mut decoder = DeflateDecoder::new(file.unwrap()); - // let mut return_vec = Vec::new(); - // decoder.read_to_end(&mut return_vec) - // .map_err(|e| std::io::Error::new(e.kind(), format!("Unable to decompress data for [{}]: {}", input_path.display(), e))); - // - // debug!("Decompressed file from path: {}", input_path.display()); - // return_vec } diff --git a/tests/data_to_text.rs b/tests/data_to_text.rs index 5c75afa..2b3912c 100644 --- a/tests/data_to_text.rs +++ b/tests/data_to_text.rs @@ -2,13 +2,13 @@ use trevors_utilities::data_to_text::DataToText; fn read_bin(source: &str) -> Vec { let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.bin", source); - println!("FULL PATH BIN: [{}]", full_path); + // println!("FULL PATH BIN: [{}]", full_path); std::fs::read(full_path).unwrap() } fn read_display(source: &str) -> String { let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.display", source); - println!("FULL PATH DIS: [{}]", full_path); + // println!("FULL PATH DIS: [{}]", full_path); std::fs::read_to_string(full_path).unwrap() } @@ -46,3 +46,13 @@ fn quickbrownfoxtest() { ); } } + +#[test] +fn data_to_text_window_all() { + let bin_data = read_bin("quickbrownfox"); + let formatted = DataToText::data_to_text_window(&bin_data, 0, bin_data.len()); + let expected = read_display("quickbrownfox"); + assert_eq!( + formatted, expected + ); +} diff --git a/tests/test_compression.rs b/tests/test_compression.rs index d3d2912..8886ac1 100644 --- a/tests/test_compression.rs +++ b/tests/test_compression.rs @@ -136,4 +136,29 @@ fn compress_file_to_array() { } #[test] -fn decompress_to_array() {} +fn decompress_to_array() { + let original_text = "The quick brown fox jumps over the lazy dog."; + let compressed_data = TestCompression::compress_string(original_text); + + // Write the compressed data to a temp file + let mut temp_file = NamedTempFile::new().expect("Failed to create temp file"); + temp_file + .write_all(&compressed_data) + .expect("Failed to write compressed data to file"); + + // Run the method under test + let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path()); + + // Convert to string to compare + let decompressed_text = String::from_utf8(decompressed_data) + .expect("Decompressed bytes were not valid UTF-8"); + assert_eq!(decompressed_text, original_text); + +} + +#[test] +#[should_panic(expected = "No such file or directory")] +fn decompress_file_panics_on_missing_path() { + let bad_path = std::path::Path::new("/definitely/invalid/path"); + TestCompression::decompress_file_to_array(bad_path); +}