pub fn encode(
data: &[u8],
dictionary: &AlternatingWordDictionary,
) -> Result<String, DecodeError>Expand description
Encodes binary data using alternating word dictionaries.
Each byte is encoded as a single word, with the dictionary selection alternating based on byte position.
§Parameters
data: The binary data to encodedictionary: The alternating word dictionary to use
§Returns
A string with words joined by the dictionary’s delimiter, or an error if any byte cannot be encoded (e.g., byte value exceeds dictionary size).
§Errors
Returns DecodeError::InvalidCharacter if a byte value exceeds the
dictionary size at that position.
§Example
use base_d::{WordDictionary, AlternatingWordDictionary, word_alternating};
let even_words: Vec<String> = (0..256).map(|i| format!("e{}", i)).collect();
let odd_words: Vec<String> = (0..256).map(|i| format!("o{}", i)).collect();
let even = WordDictionary::builder().words(even_words).build().unwrap();
let odd = WordDictionary::builder().words(odd_words).build().unwrap();
let dict = AlternatingWordDictionary::new(vec![even, odd], " ".to_string());
let data = vec![0x00, 0x01, 0x02];
let encoded = word_alternating::encode(&data, &dict).unwrap();
assert_eq!(encoded, "e0 o1 e2");