pub struct AlternatingWordDictionary { /* private fields */ }Expand description
A word dictionary that alternates between multiple sub-dictionaries.
Used for PGP biometric word lists where even/odd bytes use different words. Each byte position determines which sub-dictionary is used for encoding/decoding.
Implementations§
Source§impl AlternatingWordDictionary
impl AlternatingWordDictionary
Sourcepub fn new(dictionaries: Vec<WordDictionary>, delimiter: String) -> Self
pub fn new(dictionaries: Vec<WordDictionary>, delimiter: String) -> Self
Creates a new AlternatingWordDictionary.
§Parameters
dictionaries: Vector of WordDictionary instances to alternate between (must be non-empty)delimiter: String to join encoded words (e.g., “-” or “ “)
§Panics
Panics if:
dictionariesis empty- Any dictionary does not have exactly 256 words (required for full byte coverage)
§Example
use base_d::{WordDictionary, AlternatingWordDictionary};
// Create dictionaries with 256 words each for full byte coverage
let dict1 = WordDictionary::builder()
.words((0..256).map(|i| format!("even{}", i)).collect::<Vec<_>>())
.build()
.unwrap();
let dict2 = WordDictionary::builder()
.words((0..256).map(|i| format!("odd{}", i)).collect::<Vec<_>>())
.build()
.unwrap();
let alternating = AlternatingWordDictionary::new(
vec![dict1, dict2],
" ".to_string(),
);Sourcepub fn dict_index(&self, byte_position: usize) -> usize
pub fn dict_index(&self, byte_position: usize) -> usize
Returns which dictionary index to use for a given byte position.
Uses modulo arithmetic to cycle through available dictionaries.
§Example
assert_eq!(alternating.dict_index(0), 0);
assert_eq!(alternating.dict_index(1), 1);
assert_eq!(alternating.dict_index(2), 0);
assert_eq!(alternating.dict_index(3), 1);Sourcepub fn dict_at(&self, position: usize) -> &WordDictionary
pub fn dict_at(&self, position: usize) -> &WordDictionary
Get the dictionary for a given byte position.
§Example
let dict_at_0 = alternating.dict_at(0);
let dict_at_1 = alternating.dict_at(1);
// dict_at_0 and dict_at_1 are different dictionariesSourcepub fn encode_byte(&self, byte: u8, position: usize) -> Option<&str>
pub fn encode_byte(&self, byte: u8, position: usize) -> Option<&str>
Encode a single byte at a given position.
The dictionary used depends on the byte position.
§Parameters
byte: The byte value to encode (0-255)position: The position of this byte in the input stream
§Returns
The word corresponding to this byte at this position, or None if the byte value exceeds the dictionary size.
§Example
assert_eq!(alternating.encode_byte(42, 0), Some("even42")); // Even position
assert_eq!(alternating.encode_byte(42, 1), Some("odd42")); // Odd positionSourcepub fn decode_word(&self, word: &str, position: usize) -> Option<u8>
pub fn decode_word(&self, word: &str, position: usize) -> Option<u8>
Decode a word at a given position back to a byte.
The dictionary used depends on the word position. Case sensitivity is determined by the sub-dictionary’s case_sensitive setting.
§Parameters
word: The word to decodeposition: The position of this word in the encoded sequence
§Returns
The byte value (0-255) corresponding to this word at this position, or None if the word is not found in the appropriate dictionary.
§Example
// "even0" is index 0 in even dictionary (position 0)
assert_eq!(alternating.decode_word("even0", 0), Some(0));
// "odd1" is index 1 in odd dictionary (position 1)
assert_eq!(alternating.decode_word("odd1", 1), Some(1));Trait Implementations§
Source§impl Clone for AlternatingWordDictionary
impl Clone for AlternatingWordDictionary
Source§fn clone(&self) -> AlternatingWordDictionary
fn clone(&self) -> AlternatingWordDictionary
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more