pub struct Alphabet { /* private fields */ }Expand description
Represents an encoding alphabet with its characters and configuration.
An alphabet defines the character set and encoding mode used for converting binary data to text. Supports three modes: mathematical base conversion, chunked (RFC 4648), and byte-range mapping.
Implementations§
Source§impl Alphabet
impl Alphabet
Sourcepub fn new_with_mode(
chars: Vec<char>,
mode: EncodingMode,
padding: Option<char>,
) -> Result<Self, String>
pub fn new_with_mode( chars: Vec<char>, mode: EncodingMode, padding: Option<char>, ) -> Result<Self, String>
Creates a new alphabet with specified encoding mode and optional padding.
§Arguments
chars- Vector of characters to use in the alphabetmode- Encoding mode (BaseConversion, Chunked, or ByteRange)padding- Optional padding character (typically ‘=’ for RFC modes)
§Errors
Returns an error if:
- The alphabet is empty or contains duplicates
- Chunked mode is used with a non-power-of-two alphabet size
Examples found in repository?
3fn main() {
4 let config = AlphabetsConfig::load_default().unwrap();
5 let alphabet_config = config.get_alphabet("cards").expect("cards alphabet not found");
6 let chars: Vec<char> = alphabet_config.chars.chars().collect();
7 let padding = alphabet_config.padding.as_ref().and_then(|s| s.chars().next());
8 let alphabet = Alphabet::new_with_mode(chars, alphabet_config.mode.clone(), padding).unwrap();
9
10 let data = b"Hello, World!";
11
12 println!("Original: {}", String::from_utf8_lossy(data));
13 println!("Alphabet: cards (base-{})", alphabet.base());
14 let encoded = encode(data, &alphabet);
15 println!("Encoded: {}", encoded);
16
17 let decoded = decode(&encoded, &alphabet).unwrap();
18 println!("Decoded: {}", String::from_utf8_lossy(&decoded));
19 println!("\nRoundtrip successful: {}", data == &decoded[..]);
20}Sourcepub fn new_with_mode_and_range(
chars: Vec<char>,
mode: EncodingMode,
padding: Option<char>,
start_codepoint: Option<u32>,
) -> Result<Self, String>
pub fn new_with_mode_and_range( chars: Vec<char>, mode: EncodingMode, padding: Option<char>, start_codepoint: Option<u32>, ) -> Result<Self, String>
Creates a new alphabet with full configuration including byte-range support.
§Arguments
chars- Vector of characters (empty for ByteRange mode)mode- Encoding modepadding- Optional padding characterstart_codepoint- Starting Unicode codepoint for ByteRange mode
§Errors
Returns an error if configuration is invalid for the specified mode.
Sourcepub fn from_str(s: &str) -> Result<Self, String>
pub fn from_str(s: &str) -> Result<Self, String>
Creates an alphabet from a string of characters.
§Arguments
s- String containing the alphabet characters
Examples found in repository?
3fn main() {
4 // Create a custom alphabet with just 4 DNA bases
5 let dna_alphabet = Alphabet::from_str("ACGT").unwrap();
6
7 println!("DNA Alphabet (base-4)");
8 println!("=====================\n");
9
10 // Encode some data
11 let data = b"DNA";
12 let encoded = encode(data, &dna_alphabet);
13
14 println!("Original: {:?}", String::from_utf8_lossy(data));
15 println!("Encoded: {}", encoded);
16 println!("Length: {} bases\n", encoded.len());
17
18 // Decode it back
19 let decoded = decode(&encoded, &dna_alphabet).unwrap();
20 println!("Decoded: {:?}", String::from_utf8_lossy(&decoded));
21 println!("Match: {}", data == &decoded[..]);
22
23 // Try different data
24 println!("\n---\n");
25 let data2 = &[0xFF, 0x00, 0x42];
26 let encoded2 = encode(data2, &dna_alphabet);
27 println!("Binary {:?} encodes to: {}", data2, encoded2);
28}Sourcepub fn base(&self) -> usize
pub fn base(&self) -> usize
Returns the base (radix) of the alphabet.
For ByteRange mode, always returns 256. Otherwise returns the number of characters.
Examples found in repository?
3fn main() {
4 let config = AlphabetsConfig::load_default().unwrap();
5 let alphabet_config = config.get_alphabet("cards").expect("cards alphabet not found");
6 let chars: Vec<char> = alphabet_config.chars.chars().collect();
7 let padding = alphabet_config.padding.as_ref().and_then(|s| s.chars().next());
8 let alphabet = Alphabet::new_with_mode(chars, alphabet_config.mode.clone(), padding).unwrap();
9
10 let data = b"Hello, World!";
11
12 println!("Original: {}", String::from_utf8_lossy(data));
13 println!("Alphabet: cards (base-{})", alphabet.base());
14 let encoded = encode(data, &alphabet);
15 println!("Encoded: {}", encoded);
16
17 let decoded = decode(&encoded, &alphabet).unwrap();
18 println!("Decoded: {}", String::from_utf8_lossy(&decoded));
19 println!("\nRoundtrip successful: {}", data == &decoded[..]);
20}Sourcepub fn mode(&self) -> &EncodingMode
pub fn mode(&self) -> &EncodingMode
Returns the encoding mode of this alphabet.
Sourcepub fn start_codepoint(&self) -> Option<u32>
pub fn start_codepoint(&self) -> Option<u32>
Returns the starting Unicode codepoint for ByteRange mode.
Sourcepub fn encode_digit(&self, digit: usize) -> Option<char>
pub fn encode_digit(&self, digit: usize) -> Option<char>
Encodes a digit (0 to base-1) as a character.
Returns None if the digit is out of range.
Sourcepub fn decode_char(&self, c: char) -> Option<usize>
pub fn decode_char(&self, c: char) -> Option<usize>
Decodes a character back to its digit value.
Returns None if the character is not in the alphabet.