Skip to content

Commit

Permalink
Finished generic compression algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusTreloar committed Nov 4, 2022
1 parent 5fc7afe commit 9204129
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 32 deletions.
10 changes: 0 additions & 10 deletions someStuff/someFile.txt

This file was deleted.

4 changes: 2 additions & 2 deletions src/bin/zap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use clap::{Parser, Subcommand, ValueEnum};
use zapf::{pack_files, unpack_files};

use zap::compression::algorithms::lz4_encoder;
#[derive(Debug, Parser)]
#[command(
author,
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Command {
zap::compress_directory(
&input,
"/tmp/stuff",
zap::compression::lz4_writer
lz4_encoder
).await?;

let out_file = File::create(&output).expect("Could not create file");
Expand Down
54 changes: 54 additions & 0 deletions src/compression/algorithms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Internal
use super::Cleanup;

// External
use std::io::{
Error,
ErrorKind,
Write,
};
use lz4_flex::frame::{
FrameEncoder,
FrameDecoder
};

pub fn lz4_encoder<T>(input: Result<T, std::io::Error>) -> Result<Lz4_Encoder<T>, std::io::Error>
where T: Write
{
Ok( Lz4_Encoder {
inner: (FrameEncoder::new(input?))
})
}

pub struct Lz4_Encoder<T>
where T: Write
{
inner: FrameEncoder<T>
}

impl<T> Write for Lz4_Encoder<T>
where T: Write
{
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.inner.write(buf)
}
}

impl<T> Cleanup<T> for Lz4_Encoder<T>
where T: Write
{
fn cleanup(self) -> Result<T, Error>
{
match self.inner.finish()
{
Ok(w) => Ok(w),
Err(e) => Err(Error::from(
ErrorKind::Interrupted
))
}
}
}
19 changes: 12 additions & 7 deletions src/compression.rs → src/compression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
pub mod algorithms;

use std::{
fs,
fs::{self, File},
io::{self},
};
use std::io::Write;


pub fn lz4_writer<T>(
input: Result<T, io::Error>
) -> Result<lz4_flex::frame::FrameEncoder<T>, io::Error>
where
T: io::Write
pub trait Cleanup<T>
{
Ok(lz4_flex::frame::FrameEncoder::new(input?))
fn cleanup(self) -> Result<T, io::Error>;
}

pub fn lz4<T>(input: Result<T, io::Error>) -> Result<lz4_flex::frame::FrameEncoder<T>, io::Error>
where T: io::Write
{
Ok(lz4_flex::frame::FrameEncoder::new(input?))
}
/// Compress the input file and write it to the output file.
/// The output file is encrypted if the keys are supplied.
pub fn compress_lz4(input_file: fs::File, output_file: fs::File) {
Expand Down
Empty file added src/encryption/algo.rs
Empty file.
4 changes: 0 additions & 4 deletions src/encryption.rs → src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ use openssl::{

};

pub fn encrypt(input: String)
{

}
10 changes: 6 additions & 4 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ use openssl::symm::{
Cipher,
encrypt
};
use std::fmt::Debug;

pub fn compress<T, U>(mut input: T, mut output: Result<U, Error>) -> Result<(), Error>
use crate::compression::Cleanup;

pub fn compress<T, U, V>(mut input: T, output: Result<U, Error>) -> Result<(), Error>
where
T: Read,
U: Write+Debug
U: Write+Cleanup<V>,
V: Write
{
let mut out = output?;
let len = copy(&mut input, &mut out)?;
dbg!(len);
out.flush().unwrap();
out.cleanup();
Ok(())
}

Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ use std::{
path,
};

use compression::{decompress_lz4};
use compression::{decompress_lz4, Cleanup};
use walkdir::WalkDir;
use rpassword::prompt_password;

pub async fn compress_directory(
pub async fn compress_directory<T: 'static>(
input_folder_path: &str,
output_folder_path: &str,
compression_algorithm: fn(Result<fs::File, io::Error>) -> Result<lz4_flex::frame::FrameEncoder<fs::File>, io::Error>
compression_algorithm: fn(Result<fs::File, io::Error>) -> Result<T, io::Error>
) -> io::Result<()>
where T: io::Write+Cleanup<fs::File>
{
let mut task_list = Vec::with_capacity(800);

Expand Down Expand Up @@ -53,13 +54,13 @@ pub async fn compress_directory(
let func = compression_algorithm.clone();
// Rewrite to return errors
let compress_task = tokio::spawn(async move {
dbg!(internal::compress(
internal::compress(
fs::File::open(entry_path).expect("Failed to open input file"),
internal::process_unit(
fs::File::create(output_path),
func
)
));
);
});

task_list.push(compress_task);
Expand Down

0 comments on commit 9204129

Please sign in to comment.