Skip to main content

msime_engine/vietnamese/
mod.rs

1//! Vietnamese input: Telex or VNI keystrokes transformed into Vietnamese letters with tone marks. The composition is the text itself; there are no candidates and nothing is learned.
2
3pub mod scheme;
4
5pub use scheme::VietnameseScheme;
6
7/// How keystrokes spell diacritics and tones. The ordinal is the host ABI value (`EngineOptions::vietnamese_input_method`).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
9#[repr(u8)]
10pub enum InputMethod {
11    /// Letters spell the marks (`aa` â, `w` ư, `f` grave).
12    #[default]
13    Telex = 0,
14    /// Digits spell the marks (`a6` â, `u7` ư, `a2` à).
15    Vni = 1,
16}
17
18impl InputMethod {
19    pub fn from_u8(value: u8) -> Option<Self> {
20        Some(match value {
21            0 => Self::Telex,
22            1 => Self::Vni,
23            _ => return None,
24        })
25    }
26}
27
28/// Where the tone mark goes on an `oa`, `oe` or `uy` syllable. The ordinal is the host ABI value (`EngineOptions::vietnamese_tone_style`).
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
30#[repr(u8)]
31pub enum ToneStyle {
32    /// On the second vowel: hoà.
33    #[default]
34    Modern = 0,
35    /// On the first vowel: hòa.
36    Classic = 1,
37}
38
39impl ToneStyle {
40    pub fn from_u8(value: u8) -> Option<Self> {
41        Some(match value {
42            0 => Self::Modern,
43            1 => Self::Classic,
44            _ => return None,
45        })
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn ordinals_round_trip_and_out_of_range_values_are_refused() {
55        for method in [InputMethod::Telex, InputMethod::Vni] {
56            assert_eq!(InputMethod::from_u8(method as u8), Some(method));
57        }
58        for style in [ToneStyle::Modern, ToneStyle::Classic] {
59            assert_eq!(ToneStyle::from_u8(style as u8), Some(style));
60        }
61        assert_eq!(InputMethod::from_u8(2), None);
62        assert_eq!(ToneStyle::from_u8(2), None);
63        assert_eq!(InputMethod::default(), InputMethod::Telex);
64        assert_eq!(ToneStyle::default(), ToneStyle::Modern);
65    }
66}