base_d/
lib.rs

1//! # base-d
2//!
3//! A universal, multi-dictionary encoding library for Rust.
4//!
5//! Encode binary data using numerous dictionaries including RFC standards, ancient scripts,
6//! emoji, playing cards, and more. Supports three encoding modes: mathematical
7//! base conversion, RFC 4648 chunked encoding, and direct byte-range mapping.
8//!
9//! ## Quick Start
10//!
11//! ```
12//! use base_d::{DictionariesConfig, Dictionary, encode, decode};
13//!
14//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
15//! // Load built-in dictionaries
16//! let config = DictionariesConfig::load_default()?;
17//! let base64_config = config.get_dictionary("base64").unwrap();
18//!
19//! // Create dictionary
20//! let chars: Vec<char> = base64_config.chars.chars().collect();
21//! let padding = base64_config.padding.as_ref().and_then(|s| s.chars().next());
22//! let dictionary = Dictionary::new_with_mode(
23//!     chars,
24//!     base64_config.mode.clone(),
25//!     padding
26//! )?;
27//!
28//! // Encode and decode
29//! let data = b"Hello, World!";
30//! let encoded = encode(data, &dictionary);
31//! let decoded = decode(&encoded, &dictionary)?;
32//! assert_eq!(data, &decoded[..]);
33//! # Ok(())
34//! # }
35//! ```
36//!
37//! ## Features
38//!
39//! - **33 Built-in Alphabets**: RFC standards, emoji, ancient scripts, and more
40//! - **3 Encoding Modes**: Mathematical, chunked (RFC-compliant), byte-range
41//! - **Streaming Support**: Memory-efficient processing for large files
42//! - **Custom Alphabets**: Define your own via TOML configuration
43//! - **User Configuration**: Load dictionaries from `~/.config/base-d/dictionaries.toml`
44//!
45//! ## Encoding Modes
46//!
47//! ### Mathematical Base Conversion
48//!
49//! Treats data as a large number. Works with any dictionary size.
50//!
51//! ```
52//! use base_d::{Dictionary, EncodingMode, encode};
53//!
54//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
55//! let chars: Vec<char> = "😀😁😂🤣😃😄😅😆".chars().collect();
56//! let dictionary = Dictionary::new_with_mode(
57//!     chars,
58//!     EncodingMode::BaseConversion,
59//!     None
60//! )?;
61//!
62//! let encoded = encode(b"Hi", &dictionary);
63//! # Ok(())
64//! # }
65//! ```
66//!
67//! ### Chunked Mode (RFC 4648)
68//!
69//! Fixed-size bit groups, compatible with standard base64/base32.
70//!
71//! ```
72//! use base_d::{Dictionary, EncodingMode, encode};
73//!
74//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
75//! let chars: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
76//!     .chars().collect();
77//! let dictionary = Dictionary::new_with_mode(
78//!     chars,
79//!     EncodingMode::Chunked,
80//!     Some('=')
81//! )?;
82//!
83//! let encoded = encode(b"Hello", &dictionary);
84//! assert_eq!(encoded, "SGVsbG8=");
85//! # Ok(())
86//! # }
87//! ```
88//!
89//! ### Byte Range Mode
90//!
91//! Direct 1:1 byte-to-emoji mapping. Zero encoding overhead.
92//!
93//! ```
94//! use base_d::{Dictionary, EncodingMode, encode};
95//!
96//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
97//! let dictionary = Dictionary::new_with_mode_and_range(
98//!     Vec::new(),
99//!     EncodingMode::ByteRange,
100//!     None,
101//!     Some(127991)  // U+1F3F7
102//! )?;
103//!
104//! let data = b"Hi";
105//! let encoded = encode(data, &dictionary);
106//! assert_eq!(encoded.chars().count(), 2);  // 1:1 mapping
107//! # Ok(())
108//! # }
109//! ```
110//!
111//! ## Streaming
112//!
113//! For large files, use streaming to avoid loading entire file into memory:
114//!
115//! ```no_run
116//! use base_d::{DictionariesConfig, StreamingEncoder};
117//! use std::fs::File;
118//!
119//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
120//! let config = DictionariesConfig::load_default()?;
121//! let alphabet_config = config.get_dictionary("base64").unwrap();
122//!
123//! // ... create dictionary from config
124//! # let chars: Vec<char> = alphabet_config.chars.chars().collect();
125//! # let padding = alphabet_config.padding.as_ref().and_then(|s| s.chars().next());
126//! # let dictionary = base_d::Dictionary::new_with_mode(chars, alphabet_config.mode.clone(), padding)?;
127//!
128//! let mut input = File::open("large_file.bin")?;
129//! let output = File::create("encoded.txt")?;
130//!
131//! let mut encoder = StreamingEncoder::new(&dictionary, output);
132//! encoder.encode(&mut input)?;
133//! # Ok(())
134//! # }
135//! ```
136
137mod core;
138mod encoders;
139mod compression;
140mod detection;
141mod hashing;
142
143pub use core::dictionary::Dictionary;
144pub use core::config::{DictionariesConfig, DictionaryConfig, EncodingMode, CompressionConfig, Settings};
145pub use encoders::streaming::{StreamingEncoder, StreamingDecoder};
146pub use encoders::encoding::DecodeError;
147pub use compression::{CompressionAlgorithm, compress, decompress};
148pub use detection::{DictionaryDetector, DictionaryMatch, detect_dictionary};
149pub use hashing::{HashAlgorithm, hash};
150
151/// Encodes binary data using the specified dictionary.
152///
153/// Automatically selects the appropriate encoding strategy based on the
154/// dictionary's mode (BaseConversion, Chunked, or ByteRange).
155///
156/// # Arguments
157///
158/// * `data` - The binary data to encode
159/// * `dictionary` - The dictionary to use for encoding
160///
161/// # Returns
162///
163/// A string containing the encoded data
164///
165/// # Examples
166///
167/// ```
168/// use base_d::{Dictionary, EncodingMode};
169///
170/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
171/// let chars: Vec<char> = "01".chars().collect();
172/// let dictionary = Dictionary::new_with_mode(chars, EncodingMode::BaseConversion, None)?;
173/// let encoded = base_d::encode(b"Hi", &dictionary);
174/// # Ok(())
175/// # }
176/// ```
177pub fn encode(data: &[u8], dictionary: &Dictionary) -> String {
178    match dictionary.mode() {
179        EncodingMode::BaseConversion => encoders::encoding::encode(data, dictionary),
180        EncodingMode::Chunked => encoders::chunked::encode_chunked(data, dictionary),
181        EncodingMode::ByteRange => encoders::byte_range::encode_byte_range(data, dictionary),
182    }
183}
184
185/// Decodes a string back to binary data using the specified dictionary.
186///
187/// Automatically selects the appropriate decoding strategy based on the
188/// dictionary's mode (BaseConversion, Chunked, or ByteRange).
189///
190/// # Arguments
191///
192/// * `encoded` - The encoded string to decode
193/// * `dictionary` - The dictionary used for encoding
194///
195/// # Returns
196///
197/// A `Result` containing the decoded binary data, or a `DecodeError` if
198/// the input is invalid
199///
200/// # Errors
201///
202/// Returns `DecodeError` if:
203/// - The input contains invalid characters
204/// - The input is empty
205/// - The padding is invalid (for chunked mode)
206///
207/// # Examples
208///
209/// ```
210/// use base_d::{Dictionary, EncodingMode, encode, decode};
211///
212/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
213/// let chars: Vec<char> = "01".chars().collect();
214/// let dictionary = Dictionary::new_with_mode(chars, EncodingMode::BaseConversion, None)?;
215/// let data = b"Hi";
216/// let encoded = encode(data, &dictionary);
217/// let decoded = decode(&encoded, &dictionary)?;
218/// assert_eq!(data, &decoded[..]);
219/// # Ok(())
220/// # }
221/// ```
222pub fn decode(encoded: &str, dictionary: &Dictionary) -> Result<Vec<u8>, DecodeError> {
223    match dictionary.mode() {
224        EncodingMode::BaseConversion => encoders::encoding::decode(encoded, dictionary),
225        EncodingMode::Chunked => encoders::chunked::decode_chunked(encoded, dictionary),
226        EncodingMode::ByteRange => encoders::byte_range::decode_byte_range(encoded, dictionary),
227    }
228}
229
230#[cfg(test)]
231mod tests;