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
impl AlphabetsConfig
Sourcepub fn from_toml(content: &str) -> Result<Self, Error>
pub fn from_toml(content: &str) -> Result<Self, Error>
Parses alphabet configurations from TOML content.
Sourcepub fn load_default() -> Result<Self, Box<dyn Error>>
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
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}Sourcepub fn load_from_file(path: &Path) -> Result<Self, Box<dyn Error>>
pub fn load_from_file(path: &Path) -> Result<Self, Box<dyn Error>>
Loads configuration from a custom file path.
Sourcepub fn load_with_overrides() -> Result<Self, Box<dyn Error>>
pub fn load_with_overrides() -> Result<Self, Box<dyn Error>>
Loads configuration with user overrides from standard locations.
Searches in priority order:
- Built-in alphabets (from library)
~/.config/base-d/alphabets.toml(user overrides)./alphabets.toml(project-local overrides)
Later configurations override earlier ones for matching alphabet names.
Sourcepub fn merge(&mut self, other: AlphabetsConfig)
pub fn merge(&mut self, other: AlphabetsConfig)
Merges another configuration into this one.
Alphabets from other override alphabets with the same name in self.
Sourcepub fn get_alphabet(&self, name: &str) -> Option<&AlphabetConfig>
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
impl Debug for AlphabetsConfig
Source§impl<'de> Deserialize<'de> for AlphabetsConfig
impl<'de> Deserialize<'de> for AlphabetsConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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§
impl Freeze for AlphabetsConfig
impl RefUnwindSafe for AlphabetsConfig
impl Send for AlphabetsConfig
impl Sync for AlphabetsConfig
impl Unpin for AlphabetsConfig
impl UnwindSafe for AlphabetsConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more