decode

Function decode 

Source
pub fn decode(
    encoded: &str,
    dictionary: &AlternatingWordDictionary,
) -> Result<Vec<u8>, DecodeError>
Expand description

Decodes an alternating word sequence back to binary data.

Splits the input on the dictionary’s delimiter and decodes each word using the appropriate dictionary for that position.

§Parameters

  • encoded: The encoded word sequence
  • dictionary: The alternating word dictionary to use

§Returns

The decoded binary data, or a DecodeError if decoding fails.

§Errors

Returns DecodeError::InvalidCharacter if:

  • A word is not found in the appropriate dictionary for its 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 encoded = "e0 o1 e2";
let decoded = word_alternating::decode(encoded, &dict).unwrap();
assert_eq!(decoded, vec![0x00, 0x01, 0x02]);