Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
root-hunter committed Nov 17, 2024
1 parent cf89ffb commit 42b1d62
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
name = "tc"
version = "0.2.0"
edition = "2021"
license-file = "LICENSE"
authors = ["Antonio Ricciardi <dev.roothunter@gmail.com>"]

[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"
env_logger = "0.11"
chrono = "0.4"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2024 Antonio Ricciardi
Copyright (c) 2024 Antonio Ricciardi <dev.roothunter@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
2 changes: 1 addition & 1 deletion src/engine/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ impl Data {
};
}

pub fn decompress(self) -> String {
pub fn to_str(self) -> String {
let mut data = Vec::<String>::new();
data.reserve(self.length);

Expand Down
45 changes: 36 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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(())
}
}
26 changes: 13 additions & 13 deletions tests/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}

Expand Down

0 comments on commit 42b1d62

Please sign in to comment.