From 42b1d6235355c4aa2ea306fa25bd3f19d96e31dd Mon Sep 17 00:00:00 2001 From: roothunter Date: Sun, 17 Nov 2024 11:48:04 +0100 Subject: [PATCH] Refactoring --- Cargo.toml | 10 ++++++---- LICENSE | 2 +- src/engine/data.rs | 2 +- src/lib.rs | 45 ++++++++++++++++++++++++++++++++++++--------- tests/engine.rs | 26 +++++++++++++------------- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f69dc8..30d5368 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,13 +2,15 @@ name = "tc" version = "0.2.0" edition = "2021" +license-file = "LICENSE" +authors = ["Antonio Ricciardi "] [dependencies] -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1", features = ["derive"] } bincode = "1.3" -rustc-hash = "2.0.0" +rustc-hash = "2" [dev-dependencies] log = "0.4" -env_logger = "0.11.5" -chrono = "0.4.38" \ No newline at end of file +env_logger = "0.11" +chrono = "0.4" \ No newline at end of file diff --git a/LICENSE b/LICENSE index c081e62..119ae7e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2024 Antonio Ricciardi +Copyright (c) 2024 Antonio Ricciardi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation diff --git a/src/engine/data.rs b/src/engine/data.rs index 4564fef..cfe24e8 100644 --- a/src/engine/data.rs +++ b/src/engine/data.rs @@ -385,7 +385,7 @@ impl Data { }; } - pub fn decompress(self) -> String { + pub fn to_str(self) -> String { let mut data = Vec::::new(); data.reserve(self.length); diff --git a/src/lib.rs b/src/lib.rs index 3559e87..184fe2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,7 @@ use engine::data::Data; const SEPARATOR: char = ' '; -pub fn compress(path: &str, buf: &[u8]) { - +pub fn compress(buf: &[u8]) -> Data { let mut data = Data::new(); let str = std::str::from_utf8(buf).unwrap(); @@ -49,14 +48,42 @@ pub fn compress(path: &str, buf: &[u8]) { i += 1; } - println!("Original length: {}", buf.len()); - let encoded = data.to_bytes(); - println!("Compress length: {}", encoded.len()); + // println!("Original length: {}", buf.len()); + // let encoded = data.to_bytes(); + // println!("Compress length: {}", encoded.len()); + + // std::fs::write(path, encoded).unwrap(); + + return data; +} + +pub fn compress_file(file_path: &str, output_path: &str) -> Result<(), ()>{ + if let Ok(file_data) = std::fs::read(file_path){ + let data = compress(file_data.as_slice()); - std::fs::write(path, encoded).unwrap(); + if let Ok(_) = std::fs::write(output_path, data.to_bytes()) { + Ok(()) + } else { + Err(()) + } + } else { + Err(()) + } } -pub fn decompress(bytes: &[u8]) -> String { - let data: Data = Data::from_bytes(bytes); - return data.decompress(); +pub fn decompress(bytes: &[u8]) -> Data { + return Data::from_bytes(bytes); +} + +pub fn decompress_file(file_path: &str, output_path: &str) -> Result<(), ()>{ + if let Ok(file_data) = std::fs::read(file_path) { + let data = decompress(file_data.as_slice()); + if let Ok(_) = std::fs::write(output_path, data.to_str()) { + Ok(()) + } else { + Err(()) + } + } else { + Err(()) + } } \ No newline at end of file diff --git a/tests/engine.rs b/tests/engine.rs index 5077fe2..01cb9ee 100644 --- a/tests/engine.rs +++ b/tests/engine.rs @@ -61,17 +61,17 @@ mod tests { info!("Compressed path: {}", compressed_path); info!("Decompressed path: {}", decompressed_path); - let test = std::fs::read(original_path).unwrap(); - tc::compress(compressed_path, test.as_slice()); - - let compressed_data = std::fs::read(compressed_path).unwrap(); - let compressed_data = compressed_data.as_slice(); - - let decompressed = tc::decompress(&compressed_data); - - std::fs::write(decompressed_path, decompressed).unwrap(); - - assert!(are_files_equal(original_path, decompressed_path).unwrap()); + let compressed = tc::compress_file(original_path, compressed_path); + if compressed.is_ok() { + let decompressed = tc::decompress_file(compressed_path, decompressed_path); + if decompressed.is_ok() { + assert!(are_files_equal(original_path, decompressed_path).unwrap()); + } else { + assert!(false); + } + } else { + assert!(false); + } } fn is_init() -> bool { @@ -94,10 +94,10 @@ mod tests { info!("Start TEST 1"); + tc::compress_file(test_path, compressed_path).unwrap(); let test = std::fs::read(test_path).unwrap(); - tc::compress(compressed_path, test.as_slice()); - let compressed = std::fs::read(compressed_path).unwrap(); + assert!(compressed.len() < test.len()); }