use std::{fs, path::PathBuf};
use clap::{Parser, Subcommand};
use zippopotamus::{huffman::*, zip::*};
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
#[command(alias = "c")]
#[command(about = "Compress a file")]
Compress {
filepath: PathBuf
},
#[command(alias = "d")]
#[command(about = "Decompress a file")]
Decompress {
filepath: PathBuf
},
}
fn main() {
println!("Zippopotamus: version {}", env!("CARGO_PKG_VERSION"));
if let Ok(art) = fs::read_to_string("zipper.txt") {
println!("{art}");
}
let args = Cli::parse();
match args.command {
Command::Compress { filepath } => {
let ratio = compress_file(&filepath).expect("Failed to compress file. Does the file exist?");
println!("Done compressing! Compression ratio: {:.2}%", ratio * 100.0);
}
Command::Decompress { filepath } => {
decompress_file(&filepath).expect("Failed to decompress file");
println!("Done decompressing!");
}
}
}
pub fn print_codes(dict: CodeDict) {
println!("Huffman code dictionary: ");
let mut len_sorted = dict.iter()
.collect::<Vec<_>>();
len_sorted.sort_by(|a, b| { a.1.1.cmp(&b.1.1).then(a.0.cmp(b.0)) });
for (ch, code) in len_sorted {
println!("{}: {:0width$b}", char::from_u32(*ch as u32).unwrap_or('?'), code.0, width = code.1 as usize);
}
}