AlphabetsConfig

Struct AlphabetsConfig 

Source
pub struct AlphabetsConfig {
    pub alphabets: HashMap<String, AlphabetConfig>,
}

Fields§

§alphabets: HashMap<String, AlphabetConfig>

Implementations§

Source§

impl AlphabetsConfig

Source

pub fn from_toml(content: &str) -> Result<Self, Error>

Source

pub fn load_default() -> Result<Self, Box<dyn Error>>

Examples found in repository?
examples/list_alphabets.rs (line 4)
3fn main() {
4    let config = AlphabetsConfig::load_default().unwrap();
5    
6    println!("Available alphabets:\n");
7    
8    for (name, alphabet_config) in config.alphabets.iter() {
9        let char_count = alphabet_config.chars.chars().count();
10        let preview: String = alphabet_config.chars.chars().take(10).collect();
11        let mode_str = match alphabet_config.mode {
12            base_d::EncodingMode::BaseConversion => "math",
13            base_d::EncodingMode::Chunked => "chunk",
14        };
15        println!("  {} (base-{}, {}): {}...", name, char_count, mode_str, preview);
16    }
17}
More examples
Hide additional examples
examples/hello_world.rs (line 4)
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 get_alphabet(&self, name: &str) -> Option<&AlphabetConfig>

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

Trait Implementations§

Source§

impl Debug for AlphabetsConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for AlphabetsConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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> 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, 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,