AlternatingWordDictionary

Struct AlternatingWordDictionary 

Source
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

Source

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:

  • dictionaries is 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(),
);
Source

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);
Source

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 dictionaries
Source

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 position
Source

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 decode
  • position: 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));
Source

pub fn delimiter(&self) -> &str

Returns the delimiter used between encoded words.

Source

pub fn num_dicts(&self) -> usize

Returns the number of sub-dictionaries.

Trait Implementations§

Source§

impl Clone for AlternatingWordDictionary

Source§

fn clone(&self) -> AlternatingWordDictionary

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlternatingWordDictionary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V