Skip to main content

compress_huffman_rs/
lib.rs

1//! # compress-huffman-rs
2//!
3//! A pure-Rust Huffman coding compression library providing frequency analysis,
4//! Huffman tree construction, canonical code generation, and round-trip
5//! encode/decode.
6//!
7//! # Modules
8//!
9//! - [`frequency`] — Symbol frequency table construction and entropy computation.
10//! - [`tree`] — Huffman tree building from frequency tables.
11//! - [`canonical`] — Canonical Huffman code assignment and serialization helpers.
12//! - [`encode`] — Bit-level encoding using Huffman codes.
13//! - [`decode`] — Bit-level decoding using Huffman trees.
14//!
15//! # Quick Start
16//!
17//! ```
18//! use compress_huffman_rs::{encode, decode, frequency, tree, canonical};
19//!
20//! let data = b"hello huffman";
21//! let freq = frequency::FrequencyTable::from_data(data);
22//! let htree = tree::HuffmanTree::from_frequency_table(&freq).unwrap();
23//! // Use tree-derived codes for encoding (matches tree for decoding)
24//! let codes = canonical::tree_codes(&htree);
25//! let encoded = encode::encode_with_map(data, &codes);
26//! let decoded = decode::decode(&encoded.bits, encoded.bit_length, &htree).unwrap();
27//! assert_eq!(data.as_slice(), decoded.as_slice());
28//! ```
29
30use std::collections::HashMap;
31
32/// A Huffman code represented as (bits_value, bit_length).
33pub type Code = (u64, u8);
34
35/// A mapping from byte symbols to their Huffman codes.
36pub type CodeMap = HashMap<u8, Code>;
37
38pub mod frequency;
39pub mod tree;
40pub mod canonical;
41pub mod encode;
42pub mod decode;
43
44pub use frequency::FrequencyTable;
45pub use tree::HuffmanTree;
46pub use canonical::CanonicalCodes;
47
48#[cfg(test)]
49mod tests;