AlphabetsConfig

Struct AlphabetsConfig 

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

Collection of alphabet configurations loaded from TOML files.

Fields§

§alphabets: HashMap<String, AlphabetConfig>

Map of alphabet names to their configurations

Implementations§

Source§

impl AlphabetsConfig

Source

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

Parses alphabet configurations from TOML content.

Source

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

Loads the built-in alphabet configurations.

Returns the default alphabets bundled with the library.

Examples found in repository?
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}
More examples
Hide additional examples
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, preview) = match alphabet_config.mode {
10            base_d::EncodingMode::ByteRange => {
11                if let Some(start) = alphabet_config.start_codepoint {
12                    let preview_chars: String = (0..10)
13                        .filter_map(|i| std::char::from_u32(start + i))
14                        .collect();
15                    (256, preview_chars)
16                } else {
17                    (256, String::from("(invalid)"))
18                }
19            }
20            _ => {
21                let count = alphabet_config.chars.chars().count();
22                let preview: String = alphabet_config.chars.chars().take(10).collect();
23                (count, preview)
24            }
25        };
26        let mode_str = match alphabet_config.mode {
27            base_d::EncodingMode::BaseConversion => "math",
28            base_d::EncodingMode::Chunked => "chunk",
29            base_d::EncodingMode::ByteRange => "range",
30        };
31        println!("  {} (base-{}, {}): {}...", name, char_count, mode_str, preview);
32    }
33}
Source

pub fn load_from_file(path: &Path) -> Result<Self, Box<dyn Error>>

Loads configuration from a custom file path.

Source

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

Loads configuration with user overrides from standard locations.

Searches in priority order:

  1. Built-in alphabets (from library)
  2. ~/.config/base-d/alphabets.toml (user overrides)
  3. ./alphabets.toml (project-local overrides)

Later configurations override earlier ones for matching alphabet names.

Source

pub fn merge(&mut self, other: AlphabetsConfig)

Merges another configuration into this one.

Alphabets from other override alphabets with the same name in self.

Source

pub fn get_alphabet(&self, name: &str) -> Option<&AlphabetConfig>

Retrieves an alphabet configuration by name.

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