Skip to main content

golia_pinyin/
engine.rs

1//! `PinyinEngine` — immutable assembly of dict + fuzzy config.
2//!
3//! Designed to be cheap to construct (the dict is from `include_bytes!` so
4//! cloning the engine itself just re-validates the FST header) and shareable
5//! across many concurrent [`Session`](crate::session::Session)s.
6
7use crate::dict::PinyinDict;
8use crate::fuzzy::FuzzyConfig;
9
10/// Immutable bundle of the dict + the user's fuzzy preferences. One per
11/// process; share by reference into [`Session`](crate::session::Session).
12pub struct PinyinEngine {
13    dict: PinyinDict,
14    fuzzy: FuzzyConfig,
15}
16
17impl PinyinEngine {
18    /// Build with the embedded dict and strict (no-fuzzy) defaults.
19    pub fn new() -> Self {
20        Self {
21            dict: PinyinDict::embedded(),
22            fuzzy: FuzzyConfig::strict(),
23        }
24    }
25
26    /// Build with a custom fuzzy config.
27    pub fn with_fuzzy(fuzzy: FuzzyConfig) -> Self {
28        Self {
29            dict: PinyinDict::embedded(),
30            fuzzy,
31        }
32    }
33
34    /// Borrowed view of the underlying dict — sessions and tests use this
35    /// for direct lookups.
36    pub fn dict(&self) -> &PinyinDict {
37        &self.dict
38    }
39
40    /// Current fuzzy config.
41    pub fn fuzzy(&self) -> FuzzyConfig {
42        self.fuzzy
43    }
44}
45
46impl Default for PinyinEngine {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn new_loads_dict() {
58        let e = PinyinEngine::new();
59        assert!(e.dict().len() >= 50);
60    }
61
62    #[test]
63    fn with_fuzzy_keeps_dict() {
64        let e = PinyinEngine::with_fuzzy(FuzzyConfig::permissive());
65        assert!(e.dict().len() >= 50);
66        assert!(e.fuzzy().z_zh);
67    }
68}