working on moving the 'TestCompression' code into its own crate
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
/// - Uncompressed files must only contain printable ASCII characters
|
||||
/// - Stuff I don't know about yet
|
||||
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use clap::{Parser, Subcommand};
|
||||
@@ -33,6 +32,7 @@ use std::io::prelude::*;
|
||||
use std::io;
|
||||
use std::ptr::write;
|
||||
use log::{debug, info};
|
||||
use gemma::test_compression::TestCompression;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "Compress or decompress a string", long_about = None)]
|
||||
@@ -56,52 +56,6 @@ enum Commands {
|
||||
}
|
||||
}
|
||||
|
||||
fn compress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let compressed_bytes= compress_file_to_array(input_path);
|
||||
let output_filename = input_path.with_extension("deflated");
|
||||
let mut output_file = File::create(&output_filename)?;
|
||||
output_file.write_all(&compressed_bytes.unwrap())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compress_file_to_array(input_path: &Path) -> Result<Vec<u8>, String> {
|
||||
let mut 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 read_buffer = Vec::new();
|
||||
let mut write_buffer = Vec::new();
|
||||
file.unwrap().read_to_end(&mut read_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to read input file [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut encoder = DeflateEncoder::new(read_buffer, Compression::default());
|
||||
encoder.write_all(&write_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to compress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Compressed file from path: {}", input_path.display());
|
||||
Ok(write_buffer)
|
||||
}
|
||||
|
||||
fn decompress_file_to_array(input_path: &Path) -> Vec<u8> {
|
||||
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
|
||||
}
|
||||
|
||||
fn decompress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let decompressed_bytes = decompress_file_to_array(input_path);
|
||||
let output_path = input_path.with_extension("inflated");
|
||||
let mut output_file = File::create(&output_path)?;
|
||||
output_file.write_all(&decompressed_bytes).expect(format!("Unable to write compressed data to [{}]", output_path.display()).as_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pretty_env_logger::init();
|
||||
|
||||
@@ -109,10 +63,10 @@ fn main() {
|
||||
|
||||
match cli.command {
|
||||
Commands::Compress { input } => {
|
||||
compress_file(&input).expect(format!("Unable to compress {}", input.display()).as_str());
|
||||
TestCompression::compress_file(&input).expect(format!("Unable to compress {}", input.display()).as_str());
|
||||
}
|
||||
Commands::Decompress { input } => {
|
||||
decompress_file(&input).expect(format!("Unable to decompress {}", input.display()).as_str());
|
||||
TestCompression::decompress_file(&input).expect(format!("Unable to decompress {}", input.display()).as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -15,4 +15,5 @@ pub mod chip8 {
|
||||
pub mod quirk_modes;
|
||||
}
|
||||
|
||||
pub mod constants;
|
||||
pub mod constants;
|
||||
pub mod test_compression;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use flate2::Compression;
|
||||
use flate2::read::DeflateDecoder;
|
||||
use flate2::write::DeflateEncoder;
|
||||
use log::debug;
|
||||
|
||||
pub struct TestCompression {}
|
||||
|
||||
impl TestCompression {
|
||||
|
||||
pub fn compress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let compressed_bytes= TestCompression::compress_file_to_array(input_path);
|
||||
let output_filename = input_path.with_extension("deflated");
|
||||
let mut output_file = File::create(&output_filename)?;
|
||||
output_file.write_all(&compressed_bytes.unwrap())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compress_file_to_array(input_path: &Path) -> Result<Vec<u8>, String> {
|
||||
let mut 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 read_buffer = Vec::new();
|
||||
let mut write_buffer = Vec::new();
|
||||
file.unwrap().read_to_end(&mut read_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to read input file [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut encoder = DeflateEncoder::new(read_buffer, Compression::default());
|
||||
encoder.write_all(&write_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to compress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Compressed file from path: {}", input_path.display());
|
||||
Ok(write_buffer)
|
||||
}
|
||||
|
||||
pub fn decompress_file_to_array(input_path: &Path) -> Vec<u8> {
|
||||
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
|
||||
}
|
||||
|
||||
pub fn decompress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let decompressed_bytes = TestCompression::decompress_file_to_array(input_path);
|
||||
let output_path = input_path.with_extension("inflated");
|
||||
let mut output_file = File::create(&output_path)?;
|
||||
output_file.write_all(&decompressed_bytes).expect(format!("Unable to write compressed data to [{}]", output_path.display()).as_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compress_string(input: &str) -> Vec<u8> {
|
||||
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
||||
encoder
|
||||
.write_all(input.as_bytes())
|
||||
.expect("Failed to write data");
|
||||
encoder.finish().expect("Failed to finish compression")
|
||||
}
|
||||
pub fn decompress_to_string(compressed_data: &[u8]) -> Result<String, std::io::Error> {
|
||||
let mut decoder = DeflateDecoder::new(compressed_data);
|
||||
let mut decompressed = String::new();
|
||||
decoder.read_to_string(&mut decompressed)?;
|
||||
Ok(decompressed)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user