Skip to main content

bamboo_core/
encoder.rs

1//! Provides functions for encoding Vietnamese text into different character sets.
2
3use crate::charset_def::{get_charset_definition, get_charset_definitions};
4
5static UNICODE: &str = "Unicode";
6
7/// Encodes a Vietnamese string into a specific character set (e.g., VNI-Windows, TCVN3).
8///
9/// If the `charset_name` is "Unicode", it returns the input string unchanged.
10pub fn encode(charset_name: &str, input: &str) -> String {
11    if charset_name == UNICODE {
12        return input.to_string();
13    }
14    let mut output = String::new();
15
16    match get_charset_definition(charset_name) {
17        Some(charset_def) => {
18            for char in input.chars() {
19                match charset_def.get(&char) {
20                    Some(encoded) => output.push_str(encoded),
21                    None => output.push(char),
22                }
23            }
24        }
25        None => {
26            output = input.to_string();
27        }
28    }
29    output
30}
31
32/// Returns a list of all supported character set names.
33pub fn get_charset_name() -> Vec<String> {
34    let mut charset_names = Vec::with_capacity(get_charset_definitions().len() + 1);
35
36    charset_names.push(UNICODE.to_string());
37    for (k, _) in get_charset_definitions() {
38        charset_names.push(k.to_string());
39    }
40    charset_names
41}
42
43/// Alias for [`get_charset_name`].
44pub fn get_charset_names() -> Vec<String> {
45    get_charset_name()
46}