Alphabet

Struct Alphabet 

Source
pub struct Alphabet { /* private fields */ }

Implementations§

Source§

impl Alphabet

Source

pub fn new(chars: Vec<char>) -> Result<Self, String>

Source

pub fn new_with_mode( chars: Vec<char>, mode: EncodingMode, padding: Option<char>, ) -> Result<Self, String>

Examples found in repository?
examples/hello_world.rs (line 8)
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}
Source

pub fn new_with_mode_and_range( chars: Vec<char>, mode: EncodingMode, padding: Option<char>, start_codepoint: Option<u32>, ) -> Result<Self, String>

Source

pub fn from_str(s: &str) -> Result<Self, String>

Examples found in repository?
examples/custom_alphabet.rs (line 5)
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}
Source

pub fn base(&self) -> usize

Examples found in repository?
examples/hello_world.rs (line 13)
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}
Source

pub fn mode(&self) -> &EncodingMode

Source

pub fn padding(&self) -> Option<char>

Source

pub fn start_codepoint(&self) -> Option<u32>

Source

pub fn encode_digit(&self, digit: usize) -> Option<char>

Source

pub fn decode_char(&self, c: char) -> Option<usize>

Trait Implementations§

Source§

impl Clone for Alphabet

Source§

fn clone(&self) -> Alphabet

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 Alphabet

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> 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.