1use crate::{CompressionAlgorithm, DictionaryRegistry, HashAlgorithm, compress, encode, hash};
7
8#[derive(Debug, Clone)]
10pub struct HashEncodeResult {
11 pub encoded: String,
13 pub hash_algo: HashAlgorithm,
15 pub dictionary_name: String,
17}
18
19#[derive(Debug, Clone)]
21pub struct CompressEncodeResult {
22 pub encoded: String,
24 pub compress_algo: CompressionAlgorithm,
26 pub dictionary_name: String,
28}
29
30pub fn hash_encode(
43 data: &[u8],
44 registry: &DictionaryRegistry,
45) -> Result<HashEncodeResult, Box<dyn std::error::Error>> {
46 let algo = HashAlgorithm::random();
47 hash_encode_with(data, algo, registry)
48}
49
50pub fn hash_encode_with(
52 data: &[u8],
53 algo: HashAlgorithm,
54 registry: &DictionaryRegistry,
55) -> Result<HashEncodeResult, Box<dyn std::error::Error>> {
56 let hashed = hash(data, algo);
57 let (dict_name, dict) = registry.random()?;
58 let encoded = encode(&hashed, &dict);
59
60 Ok(HashEncodeResult {
61 encoded,
62 hash_algo: algo,
63 dictionary_name: dict_name,
64 })
65}
66
67pub fn compress_encode(
80 data: &[u8],
81 registry: &DictionaryRegistry,
82) -> Result<CompressEncodeResult, Box<dyn std::error::Error>> {
83 let algo = CompressionAlgorithm::random();
84 compress_encode_with(data, algo, registry)
85}
86
87pub fn compress_encode_with(
89 data: &[u8],
90 algo: CompressionAlgorithm,
91 registry: &DictionaryRegistry,
92) -> Result<CompressEncodeResult, Box<dyn std::error::Error>> {
93 let level = algo.default_level();
94 let compressed = compress(data, algo, level)?;
95 let (dict_name, dict) = registry.random()?;
96 let encoded = encode(&compressed, &dict);
97
98 Ok(CompressEncodeResult {
99 encoded,
100 compress_algo: algo,
101 dictionary_name: dict_name,
102 })
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_hash_encode() {
111 let registry = DictionaryRegistry::load_default().unwrap();
112 let result = hash_encode(b"test data", ®istry).unwrap();
113 assert!(!result.encoded.is_empty());
114 assert!(!result.dictionary_name.is_empty());
115 }
116
117 #[test]
118 fn test_compress_encode() {
119 let registry = DictionaryRegistry::load_default().unwrap();
120 let result = compress_encode(b"test data for compression", ®istry).unwrap();
121 assert!(!result.encoded.is_empty());
122 assert!(!result.dictionary_name.is_empty());
123 }
124
125 #[test]
126 fn test_hash_encode_with_specific_algo() {
127 let registry = DictionaryRegistry::load_default().unwrap();
128 let result = hash_encode_with(b"test", HashAlgorithm::Sha256, ®istry).unwrap();
129 assert_eq!(result.hash_algo, HashAlgorithm::Sha256);
130 }
131
132 #[test]
133 fn test_compress_encode_with_specific_algo() {
134 let registry = DictionaryRegistry::load_default().unwrap();
135 let result =
136 compress_encode_with(b"test data", CompressionAlgorithm::Gzip, ®istry).unwrap();
137 assert_eq!(result.compress_algo, CompressionAlgorithm::Gzip);
138 }
139
140 #[test]
141 fn test_random_hash() {
142 for _ in 0..10 {
144 let algo = HashAlgorithm::random();
145 assert!(HashAlgorithm::all().contains(&algo));
146 }
147 }
148
149 #[test]
150 fn test_random_compress() {
151 for _ in 0..10 {
153 let algo = CompressionAlgorithm::random();
154 assert!(CompressionAlgorithm::all().contains(&algo));
155 }
156 }
157}