pub struct AlphabetsConfig {
pub alphabets: HashMap<String, AlphabetConfig>,
}Fields§
§alphabets: HashMap<String, AlphabetConfig>Implementations§
Source§impl AlphabetsConfig
impl AlphabetsConfig
pub fn from_toml(content: &str) -> Result<Self, Error>
Sourcepub fn load_default() -> Result<Self, Box<dyn Error>>
pub fn load_default() -> Result<Self, Box<dyn Error>>
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>>
Load configuration from custom file path
Sourcepub fn load_with_overrides() -> Result<Self, Box<dyn Error>>
pub fn load_with_overrides() -> Result<Self, Box<dyn Error>>
Load configuration with user overrides from standard locations
- Start with built-in alphabets
- Override with ~/.config/base-d/alphabets.toml if it exists
- Override with ./alphabets.toml if it exists in current directory
Sourcepub fn merge(&mut self, other: AlphabetsConfig)
pub fn merge(&mut self, other: AlphabetsConfig)
Merge another config into this one, overriding existing alphabets
Sourcepub fn get_alphabet(&self, name: &str) -> Option<&AlphabetConfig>
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
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