I am trying to implement Huffman encoding to compress files (for learning purpouses). I can generate Huffman codes for a given message and use that to build a binary string containing the compressed message. However I am stumped on how to save this “compressed” data. I want to have this data be as compact as possible (since its compression) but I am not sure if serde is the right approach. Even if it is what should I serialize as? (For example, something like Json would probably be bad for my use case since I do not care about human readability). Maybe I should just write the bytes to a file, but then I do not know how to include the lookup dictionary.
use bitvec::prelude::*;
use itertools::Itertools;
use std::{collections::HashMap, fmt, fs::File, io::{BufWriter, Read}, path::Path};
use serde::{Deserialize, Serialize, Serializer}
#[derive(Serialize, Deserialize, Debug)]
struct HuffmanData {
dictionary: HashMap<String, char>,
message: BitVec,
}
fn main() {
let path = Path::new("data").join("romeo-juliet.txt") ;
let file = File::open(path).expect("Could not open file");
let huffman_data: HuffmanData = huffman(file);
let save_path = Path::new("./compressed-data/").join("romeo-juliet.txt");
let save_file = File::create(save_path).unwrap();
}
I have skipped the function definitions since they are not needed and are working as intended.