Skip to main content

bamboo_core/
config.rs

1/// Configuration options for the Bamboo engine.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct Config {
4    /// If true, allows typing tone marks at any position in the word (Free Tone Marking).
5    /// Default: true.
6    pub free_tone_marking: bool,
7    /// If true, uses the standard (new) tone placement (e.g., "hòa", "khỏe").
8    /// If false, uses the old style (e.g., "hoà", "khoẻ").
9    /// Default: true.
10    pub std_tone_style: bool,
11    /// If true, enables automatic spelling correction to ensure valid Vietnamese syllables.
12    /// Default: true.
13    pub auto_correct: bool,
14}
15
16impl Default for Config {
17    fn default() -> Self {
18        Self { free_tone_marking: true, std_tone_style: true, auto_correct: true }
19    }
20}
21
22impl Config {
23    /// Creates a new configuration with default values.
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    pub(crate) fn to_flags(self) -> u32 {
29        let mut flags = 0;
30        if self.free_tone_marking {
31            flags |= 1 << 0;
32        }
33        if self.std_tone_style {
34            flags |= 1 << 1;
35        }
36        if self.auto_correct {
37            flags |= 1 << 2;
38        }
39        flags
40    }
41
42    /// Creates a configuration from a bitmask of flags.
43    pub fn from_flags(flags: u32) -> Self {
44        Self {
45            free_tone_marking: (flags & (1 << 0)) != 0,
46            std_tone_style: (flags & (1 << 1)) != 0,
47            auto_correct: (flags & (1 << 2)) != 0,
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn to_flags_from_flags_roundtrip() {
58        let configs = [
59            Config { free_tone_marking: true, std_tone_style: true, auto_correct: true },
60            Config { free_tone_marking: false, std_tone_style: false, auto_correct: false },
61            Config { free_tone_marking: true, std_tone_style: false, auto_correct: true },
62            Config { free_tone_marking: false, std_tone_style: true, auto_correct: false },
63        ];
64        for original in configs {
65            let flags = original.to_flags();
66            let restored = Config::from_flags(flags);
67            assert_eq!(original, restored, "Round-trip failed for {original:?}");
68        }
69    }
70
71    #[test]
72    fn default_config_flags() {
73        let cfg = Config::default();
74        // Default: all three enabled → flags = 0b111 = 7
75        assert_eq!(cfg.to_flags(), 7);
76    }
77}