Skip to main content

msime_engine/
types.rs

1//! Value types every module shares: candidate rows, query requests, key results and the session option structs.
2//!
3//! Numeric values that cross the host boundary are frozen: `CandidateSource` travels as `u8` in `candidate_sources` and hosts switch on it, `SchemeType` is serialised into the online identity, and `PersonalDictionaryKind` names are stored in the user journal.
4
5/// Where a candidate row came from. The discriminant is the wire value; never reorder.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
7#[repr(u8)]
8pub enum CandidateSource {
9    #[default]
10    Database = 0,
11    UserDatabase = 1,
12    CloudSuggestion = 2,
13    AiSuggestion = 3,
14    EnglishDictionary = 4,
15    QuickPhrase = 5,
16    /// emoji 行,也用于混排的符号行(打拼音出符号,`local::symbol_candidates`):两者选中时都整段上屏、结束组字,不学习、不调频,也不能置顶、固定或删除。
17    Emoji = 6,
18    Kaomoji = 7,
19    Generated = 8,
20    Fallback = 9,
21    /// A lattice path the keyboard (speed) sentence model picked.
22    NeuralKeyboard = 11,
23    /// AI 候选的占位行(#6888),已退役:共享内核不再产生它,AI 回答到达时第一条直接插在第一页最后一格(`.agents/notes/implemented/feature/2026-10-16-ai-candidate-last-slot.md`)。值保留不复用;引擎和宿主对它的处理(不能上屏、不学习、不计入统计、画成暗色)照旧,旧版本留下的行仍按这些规则处理。
24    AiPending = 12,
25    /// 日文混输(实验选项,默认关)混进全拼、双拼候选的那一行:整段输入按罗马字转换出的最佳日文。它不属于下面任何一个谓词,所以选中时整段上屏并结束组字(与混输英文同一条路),不调词频、不记整句、不进个人上下文,也不能置顶、固定或删除;宿主不对它做简繁转换。
26    MixedJapanese = 13,
27    /// 脚本插件在自己的 `/` 指令里给出的行,只在桌面产生(宿主经 `Session::apply_plugin_command_rows` 写进来)。它不属于下面任何一个谓词:选中时整段上屏并结束组字,不调词频、不记整句、不进个人上下文,不能置顶、固定或删除;runtime 不把它的上屏喂进 AI 上文,宿主不为它请求释义或翻译,上屏也不转全角。
28    Plugin = 14,
29    /// 云候选尚在请求中;它占据云候选的固定位置,回答到达时由第一条云候选替换。值追加在已有 ABI 之后,不能复用旧值。
30    CloudPending = 15,
31    /// Android 联系人导入提供的手机号候选;选中后直接上屏,不学习、不管理。
32    Contact = 16,
33    /// octagram `.gram` 语法模型参与词格解码时,解码首选的整句行。句子仍是词格
34    /// 解出来的,搭配强度表只负责逐边加分;落库/学习判定与 `Generated` 同类。
35    Collocation = 17,
36    /// 连续多语言转换的完整路径;整段消费,不调频、不学习跨语言整句,也不参与候选管理。
37    Multilingual = 18,
38}
39
40impl CandidateSource {
41    pub fn from_u8(value: u8) -> Option<Self> {
42        Some(match value {
43            0 => Self::Database,
44            1 => Self::UserDatabase,
45            2 => Self::CloudSuggestion,
46            3 => Self::AiSuggestion,
47            4 => Self::EnglishDictionary,
48            5 => Self::QuickPhrase,
49            6 => Self::Emoji,
50            7 => Self::Kaomoji,
51            8 => Self::Generated,
52            9 => Self::Fallback,
53            11 => Self::NeuralKeyboard,
54            12 => Self::AiPending,
55            13 => Self::MixedJapanese,
56            14 => Self::Plugin,
57            15 => Self::CloudPending,
58            16 => Self::Contact,
59            17 => Self::Collocation,
60            18 => Self::Multilingual,
61            _ => return None,
62        })
63    }
64
65    pub fn is_online(self) -> bool {
66        matches!(self, Self::CloudSuggestion | Self::AiSuggestion)
67    }
68
69    pub fn is_cloud_pending(self) -> bool {
70        self == Self::CloudPending
71    }
72
73    /// AI 行:AI 候选和已退役的占位行。
74    pub fn holds_ai_seat(self) -> bool {
75        matches!(self, Self::AiSuggestion | Self::AiPending)
76    }
77
78    /// 由在线座位规则摆放的行:云候选、AI 候选和已退役的 AI 占位行。
79    pub fn takes_online_seat(self) -> bool {
80        self.is_online() || matches!(self, Self::AiPending | Self::CloudPending)
81    }
82
83    /// 整句猜出来的行:词格 `Generated`/`Collocation`、解码器 `Fallback`。
84    /// 它们的选中都整段上屏、按词边界记进个人上下文、不落词库调频。
85    pub fn is_generated_or_fallback(self) -> bool {
86        matches!(self, Self::Generated | Self::Fallback | Self::Collocation)
87    }
88
89    /// Rows a direct selection stores as a user phrase, because there is no dictionary row whose frequency could be adjusted instead.
90    pub fn is_sentence_learning(self) -> bool {
91        self.is_generated_or_fallback() || self.is_online() || matches!(self, Self::NeuralKeyboard)
92    }
93
94    pub fn is_dictionary(self) -> bool {
95        matches!(self, Self::Database | Self::UserDatabase)
96    }
97}
98
99/// Input scheme. The ordinal is part of the online identity (`"<scheme>:<raw>"`) and of the host ABI.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
101#[repr(u8)]
102pub enum SchemeType {
103    #[default]
104    Quanpin = 0,
105    Shuangpin = 1,
106    Wubi = 2,
107    JapaneseRomaji = 3,
108    /// 韩语两套式或罗马字布局:音节在预编辑中组合,稳定前缀逐步上屏;`Command::ConvertHanja` 打开当前音节的汉字候选。
109    Korean = 4,
110    /// Cantonese in toneless Jyutping read against `msime-cantonese.db`: candidates are Traditional as stored, a candidate covering the leading syllables commits at once and leaves the rest composing, and nothing is learned.
111    Cantonese = 5,
112    /// Zhuyin (bopomofo) on the Dachen layout read against `msime-zhuyin.db`: keys compose syllables that convert to Traditional text as typed, a list the user opens pins a span's text without committing, Enter or any key outside the layout commits the conversion, and nothing is learned.
113    Zhuyin = 6,
114    /// Vietnamese through Telex or VNI: the keystrokes compose into one word in the preedit, which any key outside the spelling commits; there are no candidates.
115    Vietnamese = 7,
116    /// 藏文,在拉丁键盘上按 EWTS(扩展威利转写)拼写:威利原文在组字里组成一个音节串,显示为转换出的藏文;空格带音节点上屏,`/` 带垂符上屏,回车只上屏藏文;没有候选。
117    Tibetan = 8,
118    /// Stroke (笔画) read against `msime-stroke.db`: the keys h s p n z type the five strokes 横竖撇点折 in writing order and x stands for any one stroke; the preedit draws the strokes, candidates are single characters whose stroke code starts with the typed strokes, and nothing is learned.
119    Stroke = 9,
120}
121
122impl SchemeType {
123    pub fn from_u8(value: u8) -> Option<Self> {
124        Some(match value {
125            0 => Self::Quanpin,
126            1 => Self::Shuangpin,
127            2 => Self::Wubi,
128            3 => Self::JapaneseRomaji,
129            4 => Self::Korean,
130            5 => Self::Cantonese,
131            6 => Self::Zhuyin,
132            7 => Self::Vietnamese,
133            8 => Self::Tibetan,
134            9 => Self::Stroke,
135            _ => return None,
136        })
137    }
138
139    /// The name the golden fixtures use.
140    pub fn name(self) -> &'static str {
141        match self {
142            Self::Quanpin => "quanpin",
143            Self::Shuangpin => "shuangpin",
144            Self::Wubi => "wubi",
145            Self::JapaneseRomaji => "japanese",
146            Self::Korean => "korean",
147            Self::Cantonese => "cantonese",
148            Self::Zhuyin => "zhuyin",
149            Self::Vietnamese => "vietnamese",
150            Self::Tibetan => "tibetan",
151            Self::Stroke => "stroke",
152        }
153    }
154
155    pub fn is_pinyin(self) -> bool {
156        matches!(self, Self::Quanpin | Self::Shuangpin)
157    }
158
159    // ---- Scheme traits ----
160    //
161    // The one source of truth for what differs between schemes. Every predicate matches exhaustively, so a new scheme cannot compile until each trait is decided for it. The values for the existing schemes encode today's behaviour; input-runtime and host-api read them through `SchemeType::from_u8`.
162
163    /// A Chinese scheme: what 中文 returns to and what the Chinese statistics count.
164    pub const fn is_chinese(self) -> bool {
165        match self {
166            Self::Quanpin
167            | Self::Shuangpin
168            | Self::Wubi
169            | Self::Cantonese
170            | Self::Stroke
171            | Self::Zhuyin => true,
172            Self::JapaneseRomaji | Self::Korean | Self::Vietnamese | Self::Tibetan => false,
173        }
174    }
175
176    /// The scheme's own text is Traditional Chinese, so no script conversion is wanted.
177    pub const fn outputs_traditional_natively(self) -> bool {
178        match self {
179            Self::Quanpin
180            | Self::Shuangpin
181            | Self::Wubi
182            | Self::JapaneseRomaji
183            | Self::Korean
184            | Self::Vietnamese
185            | Self::Tibetan => false,
186            Self::Cantonese | Self::Stroke | Self::Zhuyin => true,
187        }
188    }
189
190    /// The host's Simplified-to-Traditional conversion applies to the scheme's commits and preedit.
191    pub const fn script_conversion_applies(self) -> bool {
192        match self {
193            Self::Quanpin | Self::Shuangpin | Self::Wubi => true,
194            Self::JapaneseRomaji
195            | Self::Korean
196            | Self::Vietnamese
197            | Self::Tibetan
198            | Self::Cantonese
199            | Self::Stroke
200            | Self::Zhuyin => false,
201        }
202    }
203
204    /// The engine translates punctuation through the Chinese table. Korean writes half-width ASCII marks instead; Japanese goes through the table like the Chinese schemes.
205    pub const fn uses_chinese_punctuation(self) -> bool {
206        match self {
207            Self::Quanpin
208            | Self::Shuangpin
209            | Self::Wubi
210            | Self::JapaneseRomaji
211            | Self::Cantonese
212            | Self::Stroke
213            | Self::Zhuyin => true,
214            Self::Korean | Self::Vietnamese | Self::Tibetan => false,
215        }
216    }
217
218    /// The host's smart punctuation (context-dependent marks) may run.
219    pub const fn host_smart_punctuation(self) -> bool {
220        match self {
221            Self::Quanpin | Self::Shuangpin | Self::Wubi | Self::Cantonese | Self::Stroke => true,
222            Self::JapaneseRomaji
223            | Self::Korean
224            | Self::Vietnamese
225            | Self::Tibetan
226            | Self::Zhuyin => false,
227        }
228    }
229
230    /// Commits are widened when the full-width switch is on.
231    pub const fn widens_full_width(self) -> bool {
232        match self {
233            Self::Quanpin
234            | Self::Shuangpin
235            | Self::Wubi
236            | Self::JapaneseRomaji
237            | Self::Cantonese
238            | Self::Stroke
239            | Self::Zhuyin => true,
240            Self::Korean | Self::Vietnamese | Self::Tibetan => false,
241        }
242    }
243
244    /// English words and emoji or kaomoji may be mixed into the candidate list.
245    pub const fn allows_english_emoji_mixing(self) -> bool {
246        match self {
247            Self::Quanpin | Self::Shuangpin => true,
248            Self::Wubi
249            | Self::JapaneseRomaji
250            | Self::Korean
251            | Self::Vietnamese
252            | Self::Tibetan
253            | Self::Cantonese
254            | Self::Stroke
255            | Self::Zhuyin => false,
256        }
257    }
258
259    /// 没有组字时,Shift+字母打开按拼音拼写查的本地模式(E、M、J、R)和计算模式 V;K、C、U、T、Y、S 和 `/`、`@` 由 `opens_table_modes` 判断(`InputSession::local_mode_for_entry`)。
260    pub const fn opens_local_modes(self) -> bool {
261        match self {
262            Self::Quanpin | Self::Shuangpin => true,
263            Self::Wubi
264            | Self::JapaneseRomaji
265            | Self::Korean
266            | Self::Vietnamese
267            | Self::Tibetan
268            | Self::Cantonese
269            | Self::Stroke
270            | Self::Zhuyin => false,
271        }
272    }
273
274    /// 没有组字时,Shift+K 打开短语模式,`/` 和 `@` 打开指令和提及模式,Shift+C、U、T、Y、S 打开拆字、Unicode、日期时间、临时英文和符号模式;五笔也提供这些不经过方案拼写的入口。
275    pub const fn opens_table_modes(self) -> bool {
276        match self {
277            Self::Quanpin | Self::Shuangpin | Self::Wubi => true,
278            Self::JapaneseRomaji
279            | Self::Korean
280            | Self::Vietnamese
281            | Self::Tibetan
282            | Self::Cantonese
283            | Self::Stroke
284            | Self::Zhuyin => false,
285        }
286    }
287
288    /// 「只出单字」打开时,候选里去掉含汉字的词和整句,只留单个汉字。只用于选一个候选就上屏、剩下的拼写接着组字的方案;注音的候选列表是给组字里的一段改字,笔画本来只出单字。
289    pub const fn filters_to_single_characters(self) -> bool {
290        match self {
291            Self::Quanpin | Self::Shuangpin | Self::Wubi | Self::Cantonese => true,
292            Self::JapaneseRomaji
293            | Self::Korean
294            | Self::Vietnamese
295            | Self::Tibetan
296            | Self::Stroke
297            | Self::Zhuyin => false,
298        }
299    }
300
301    /// 组字中键入 `www.`、`http:` 等时进入网址模式。
302    pub const fn detects_urls(self) -> bool {
303        match self {
304            Self::Quanpin | Self::Shuangpin | Self::Wubi => true,
305            Self::JapaneseRomaji
306            | Self::Korean
307            | Self::Vietnamese
308            | Self::Tibetan
309            | Self::Cantonese
310            | Self::Stroke
311            | Self::Zhuyin => false,
312        }
313    }
314
315    /// Selections adjust frequencies and store sentences in the main dictionary and journal.
316    pub const fn learns_into_main_dictionary(self) -> bool {
317        match self {
318            Self::Quanpin | Self::Shuangpin | Self::Wubi => true,
319            Self::JapaneseRomaji
320            | Self::Korean
321            | Self::Vietnamese
322            | Self::Tibetan
323            | Self::Cantonese
324            | Self::Stroke
325            | Self::Zhuyin => false,
326        }
327    }
328
329    /// Letters committed raw that are not a complete spelling are learned as an English word.
330    pub const fn learns_english_words(self) -> bool {
331        match self {
332            Self::Quanpin | Self::Shuangpin => true,
333            Self::Wubi
334            | Self::JapaneseRomaji
335            | Self::Korean
336            | Self::Vietnamese
337            | Self::Tibetan
338            | Self::Cantonese
339            | Self::Stroke
340            | Self::Zhuyin => false,
341        }
342    }
343
344    /// The engine raises cloud queries for the composition.
345    pub const fn cloud_eligible(self) -> bool {
346        match self {
347            Self::Quanpin | Self::Shuangpin | Self::JapaneseRomaji => true,
348            Self::Wubi
349            | Self::Korean
350            | Self::Vietnamese
351            | Self::Tibetan
352            | Self::Cantonese
353            | Self::Stroke
354            | Self::Zhuyin => false,
355        }
356    }
357
358    /// Candidates may carry translation glosses. Korean's are the Hanja rows, glossed and translated like Chinese ones under their 훈음.
359    pub const fn shows_glosses(self) -> bool {
360        match self {
361            Self::Quanpin | Self::Shuangpin | Self::Wubi | Self::Korean => true,
362            Self::JapaneseRomaji
363            | Self::Vietnamese
364            | Self::Tibetan
365            | Self::Cantonese
366            | Self::Stroke
367            | Self::Zhuyin => false,
368        }
369    }
370
371    /// Focus loss or a scheme switch commits the composition instead of discarding it.
372    pub const fn commits_on_blur(self) -> bool {
373        match self {
374            Self::Korean | Self::Vietnamese | Self::Tibetan | Self::Zhuyin => true,
375            Self::Quanpin
376            | Self::Shuangpin
377            | Self::Wubi
378            | Self::JapaneseRomaji
379            | Self::Cantonese
380            | Self::Stroke => false,
381        }
382    }
383
384    /// A partial selection is held as phrase progress rather than committed at once.
385    pub const fn holds_phrase_progress(self) -> bool {
386        match self {
387            Self::Quanpin | Self::Shuangpin | Self::Wubi | Self::JapaneseRomaji => true,
388            Self::Korean
389            | Self::Vietnamese
390            | Self::Tibetan
391            | Self::Cantonese
392            | Self::Stroke
393            | Self::Zhuyin => false,
394        }
395    }
396
397    /// The candidate list may be reordered by the sentence model and the personal context.
398    pub const fn reranks_with_sentence_model(self) -> bool {
399        match self {
400            Self::Quanpin | Self::Shuangpin => true,
401            Self::Wubi
402            | Self::JapaneseRomaji
403            | Self::Korean
404            | Self::Vietnamese
405            | Self::Tibetan
406            | Self::Cantonese
407            | Self::Stroke
408            | Self::Zhuyin => false,
409        }
410    }
411
412    /// The snapshot's `reading` carries the composed text (kana, Hangul, the Zhuyin conversion, the Stroke glyphs 一丨丿丶乛) for the host to draw.
413    pub const fn draws_reading(self) -> bool {
414        match self {
415            Self::JapaneseRomaji | Self::Korean | Self::Zhuyin | Self::Stroke => true,
416            Self::Quanpin
417            | Self::Shuangpin
418            | Self::Wubi
419            | Self::Vietnamese
420            | Self::Tibetan
421            | Self::Cantonese => false,
422        }
423    }
424
425    /// Candidates appear only in a list the user opens (`Command::ConvertHanja`: the Korean Hanja list, the Zhuyin list), and the list can be closed again.
426    pub const fn has_openable_candidate_list(self) -> bool {
427        match self {
428            Self::Korean | Self::Zhuyin => true,
429            Self::Quanpin
430            | Self::Shuangpin
431            | Self::Wubi
432            | Self::JapaneseRomaji
433            | Self::Vietnamese
434            | Self::Tibetan
435            | Self::Cantonese
436            | Self::Stroke => false,
437        }
438    }
439
440    /// 第一次 Cancel 保留组字:关闭打开的候选列表,或把越南文、藏文的组字切回按键原文。第二次 Cancel 才丢弃它。
441    pub const fn cancel_keeps_composition(self) -> bool {
442        match self {
443            Self::Korean | Self::Zhuyin | Self::Vietnamese | Self::Tibetan => true,
444            Self::Quanpin
445            | Self::Shuangpin
446            | Self::Wubi
447            | Self::JapaneseRomaji
448            | Self::Cantonese
449            | Self::Stroke => false,
450        }
451    }
452
453    /// Selecting any of the scheme's candidates finishes the composition. Native wubi rows also finish, which is decided per row because a mixed wubi list holds pinyin rows too.
454    pub const fn selection_completes(self) -> bool {
455        match self {
456            Self::JapaneseRomaji | Self::Korean | Self::Vietnamese | Self::Tibetan => true,
457            Self::Quanpin
458            | Self::Shuangpin
459            | Self::Wubi
460            | Self::Cantonese
461            | Self::Stroke
462            | Self::Zhuyin => false,
463        }
464    }
465
466    /// The candidate list reads the composition as pinyin, so selections advance and learn as pinyin.
467    pub const fn follows_pinyin_candidates(self) -> bool {
468        match self {
469            Self::Quanpin | Self::Shuangpin => true,
470            Self::Wubi
471            | Self::JapaneseRomaji
472            | Self::Korean
473            | Self::Vietnamese
474            | Self::Tibetan
475            | Self::Cantonese
476            | Self::Stroke
477            | Self::Zhuyin => false,
478        }
479    }
480
481    /// An apostrophe typed inside the composition is a syllable boundary the scheme keeps.
482    pub const fn accepts_apostrophe(self) -> bool {
483        match self {
484            Self::Quanpin | Self::Shuangpin | Self::JapaneseRomaji | Self::Cantonese => true,
485            Self::Wubi
486            | Self::Korean
487            | Self::Vietnamese
488            | Self::Tibetan
489            | Self::Zhuyin
490            | Self::Stroke => false,
491        }
492    }
493
494    /// The caret stays at the end of the composition.
495    pub const fn locks_caret(self) -> bool {
496        match self {
497            Self::Korean | Self::Vietnamese | Self::Tibetan | Self::Zhuyin => true,
498            Self::Quanpin
499            | Self::Shuangpin
500            | Self::Wubi
501            | Self::JapaneseRomaji
502            | Self::Cantonese
503            | Self::Stroke => false,
504        }
505    }
506
507    /// Fuzzy pinyin rules apply.
508    pub const fn supports_fuzzy(self) -> bool {
509        match self {
510            Self::Quanpin | Self::Shuangpin => true,
511            Self::Wubi
512            | Self::JapaneseRomaji
513            | Self::Korean
514            | Self::Vietnamese
515            | Self::Tibetan
516            | Self::Cantonese
517            | Self::Stroke
518            | Self::Zhuyin => false,
519        }
520    }
521
522    /// Typo autocorrection applies and its outcomes are learned.
523    pub const fn supports_autocorrect(self) -> bool {
524        match self {
525            Self::Quanpin => true,
526            Self::Shuangpin
527            | Self::Wubi
528            | Self::JapaneseRomaji
529            | Self::Korean
530            | Self::Vietnamese
531            | Self::Tibetan
532            | Self::Cantonese
533            | Self::Stroke
534            | Self::Zhuyin => false,
535        }
536    }
537
538    /// 九键网格能拼出该方案的音节:全拼用拼音九宫格,注音用注音九键。
539    pub const fn nine_key(self) -> bool {
540        match self {
541            Self::Quanpin | Self::Zhuyin => true,
542            Self::Shuangpin
543            | Self::Wubi
544            | Self::JapaneseRomaji
545            | Self::Korean
546            | Self::Vietnamese
547            | Self::Tibetan
548            | Self::Cantonese
549            | Self::Stroke => false,
550        }
551    }
552
553    /// The scheme has a helpcode switch and its candidates carry helpcode annotations.
554    pub const fn helpcode(self) -> bool {
555        match self {
556            Self::Quanpin | Self::Shuangpin => true,
557            Self::Wubi
558            | Self::JapaneseRomaji
559            | Self::Korean
560            | Self::Vietnamese
561            | Self::Tibetan
562            | Self::Cantonese
563            | Self::Stroke
564            | Self::Zhuyin => false,
565        }
566    }
567}
568
569/// 会话允许运行的方案集合,每个方案占 `SchemeType` 序号对应的那一位。缺省是 [`SchemeSet::ALL`],即全部方案,行为与没有这个集合时完全相同。
570///
571/// 收窄后,`ProviderRegistry` 只为集合里的方案构造 provider(全拼在全拼或五笔任一在集合里时构造,五笔混拼要用它),切换到集合外的方案报 `INPUT_SCHEME_NOT_ENABLED`。临时日文切到的也是日文方案,所以要用临时日文,集合里就得有 `JapaneseRomaji`;没有时临时日文的触发键不会进入这个模式。
572#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
573pub struct SchemeSet(u16);
574
575impl SchemeSet {
576    /// 序号最大的方案。加方案时改这里,`scheme_set_all_covers_every_scheme` 会在漏改时失败。
577    const LAST: SchemeType = SchemeType::Stroke;
578    /// 全部方案:序号 0 到 `Self::LAST` 的每一位。
579    pub const ALL: Self = Self((1 << (Self::LAST as u16 + 1)) - 1);
580    pub const EMPTY: Self = Self(0);
581
582    pub const fn contains(self, scheme: SchemeType) -> bool {
583        self.0 & (1 << scheme as u16) != 0
584    }
585
586    #[must_use]
587    pub const fn with(self, scheme: SchemeType) -> Self {
588        Self(self.0 | (1 << scheme as u16))
589    }
590
591    /// 由方案列表组成的集合,重复的方案只算一次。
592    pub fn of(schemes: &[SchemeType]) -> Self {
593        schemes
594            .iter()
595            .fold(Self::EMPTY, |set, scheme| set.with(*scheme))
596    }
597
598    /// 集合里是否有读 `msime-pinyin.db` 的方案:全拼、双拼和五笔的候选、学习和用户词都在它里面(五笔混拼的拼音行也是)。没有这三个方案的集合(例如只有日文、越南文或藏文的版本)随包不带 `msime-pinyin.db`:代次里没有它的工作副本,用户词库只剩英文词。
599    pub const fn reads_main_dictionary(self) -> bool {
600        self.contains(SchemeType::Quanpin)
601            || self.contains(SchemeType::Shuangpin)
602            || self.contains(SchemeType::Wubi)
603    }
604
605    /// 集合里是否有离不开 `msime-english.db` 的方案:全拼、双拼的英文混输、九键英文和回车学英文词,全拼、双拼、五笔的临时英文(Shift+Y)都读它,所以有这三个方案时准备代次要求资源目录里有它。其余方案(日文、韩语、越南语、藏文、粤语、注音、笔画)自己不读它,只有宿主打开专用英文(`Session::set_dedicated_english`)时才查英文补全、学回车上屏的词:这样的集合不要求它,资源目录里带了就照样复制进代次,没带时代次里没有它,专用英文只出键入的字母、回车不学词,个人词典的英文词也无处可存(编辑以 `NO_ENGLISH_DICTIONARY` 失败)。读 `msime-pinyin.db` 的集合([`Self::reads_main_dictionary`])一定要求它。
606    pub const fn requires_english_dictionary(self) -> bool {
607        self.contains(SchemeType::Quanpin)
608            || self.contains(SchemeType::Shuangpin)
609            || self.contains(SchemeType::Wubi)
610    }
611}
612
613impl Default for SchemeSet {
614    fn default() -> Self {
615        Self::ALL
616    }
617}
618
619/// 双拼键位方案,序号是宿主 ABI 值(`EngineOptions::shuangpin_profile`),新增方案只追加。
620#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
621#[repr(u8)]
622pub enum ShuangpinProfileKind {
623    #[default]
624    Xiaohe = 0,
625    Ziranma = 1,
626    Shoudao = 2,
627    Microsoft = 3,
628    /// 用户自定义的键位,表在 [`ShuangpinCustomTable`] 里(`EngineOptions::shuangpin_custom_profile`、`SessionOptions::shuangpin_custom_profile`),没有内置的表。
629    Custom = 4,
630    SmartAbc = 5,
631}
632
633impl ShuangpinProfileKind {
634    pub fn from_u8(value: u8) -> Option<Self> {
635        Some(match value {
636            0 => Self::Xiaohe,
637            1 => Self::Ziranma,
638            2 => Self::Shoudao,
639            3 => Self::Microsoft,
640            4 => Self::Custom,
641            5 => Self::SmartAbc,
642            _ => return None,
643        })
644    }
645
646    /// Exact-name lookup. The C++ `GetShuangpinProfile` fell back to xiaohe for an unknown name; callers that want that do it themselves, so an unknown name stays visible here.
647    pub fn from_name(name: &str) -> Option<Self> {
648        Some(match name {
649            "xiaohe" => Self::Xiaohe,
650            "ziranma" => Self::Ziranma,
651            "shoudao" => Self::Shoudao,
652            "microsoft" => Self::Microsoft,
653            "custom" => Self::Custom,
654            "smart_abc" => Self::SmartAbc,
655            _ => return None,
656        })
657    }
658
659    pub fn name(self) -> &'static str {
660        match self {
661            Self::Xiaohe => "xiaohe",
662            Self::Ziranma => "ziranma",
663            Self::Shoudao => "shoudao",
664            Self::Microsoft => "microsoft",
665            Self::Custom => "custom",
666            Self::SmartAbc => "smart_abc",
667        }
668    }
669}
670
671/// 用户自定义双拼方案的键位表(共享偏好 `shuangpin_custom_profile`)。三张表与内置方案同构:`initials` 只写三个多字母声母 `zh` `ch` `sh`,单字母声母仍在自己的字母键上;`finals` 把 33 个韵母各放到一个键上;`zero_initials` 给 12 个零声母音节各一个两键编码。键是小写字母或 `;`,`ü` 写作 `v`。表合不合法由 Engine 判定(见 `shuangpin::custom`),顺序无关。
672#[derive(Debug, Clone, PartialEq, Eq, Default)]
673pub struct ShuangpinCustomTable {
674    pub initials: Vec<(String, String)>,
675    pub finals: Vec<(String, String)>,
676    pub zero_initials: Vec<(String, String)>,
677}
678
679/// 九键双拼方案(#6572)。序号是宿主 ABI 值,名字是共享偏好 `nine_key_shuangpin_profile` 的取值。内置方案只收录能找到完整、前后一致的公开描述的那几个,来源见 `.agents/notes/implemented/feature/2026-10-10-nine-key-shuangpin.md`。
680#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
681#[repr(u8)]
682pub enum NineKeyShuangpinKind {
683    /// 米旮旯 2。
684    Migala2 = 0,
685    /// 米旮旯 3(用 0 键的那一版)。
686    #[default]
687    Migala3 = 1,
688    /// 辜氏 2 低重码。
689    Gushi2Low = 2,
690    /// 用户自己的表,在 [`NineKeyShuangpinTable`] 里,没有内置的表。
691    Custom = 3,
692}
693
694impl NineKeyShuangpinKind {
695    /// 有内置表的方案,按序号。
696    pub const BUILTIN: [Self; 3] = [Self::Migala2, Self::Migala3, Self::Gushi2Low];
697
698    pub fn from_u8(value: u8) -> Option<Self> {
699        Some(match value {
700            0 => Self::Migala2,
701            1 => Self::Migala3,
702            2 => Self::Gushi2Low,
703            3 => Self::Custom,
704            _ => return None,
705        })
706    }
707
708    /// 只认确切的名字,不认识的名字返回 `None`,由调用方决定怎么处理。
709    pub fn from_name(name: &str) -> Option<Self> {
710        Some(match name {
711            "migala2" => Self::Migala2,
712            "migala3" => Self::Migala3,
713            "gushi2_low" => Self::Gushi2Low,
714            "custom" => Self::Custom,
715            _ => return None,
716        })
717    }
718
719    pub fn name(self) -> &'static str {
720        match self {
721            Self::Migala2 => "migala2",
722            Self::Migala3 => "migala3",
723            Self::Gushi2Low => "gushi2_low",
724            Self::Custom => "custom",
725        }
726    }
727}
728
729/// 九键双拼的键位表(共享偏好 `nine_key_shuangpin_custom_profile`),形状与 [`ShuangpinCustomTable`] 相同,键换成数字:`initials` 给 23 个声母(含 `zh` `ch` `sh`)各一个数字,`finals` 给 33 个韵母各一个数字,`zero_initials` 给 12 个零声母音节各一个两位数字编码。数字可以是 `0`–`9`,`ü` 写作 `v`。表合不合法由 Engine 判定(见 `nine_key_shuangpin`),顺序无关。
730#[derive(Debug, Clone, PartialEq, Eq, Default)]
731pub struct NineKeyShuangpinTable {
732    pub initials: Vec<(String, String)>,
733    pub finals: Vec<(String, String)>,
734    pub zero_initials: Vec<(String, String)>,
735}
736
737/// 五笔码表版本。序号即宿主 ABI 值(`EngineOptions::wubi_profile`)。
738#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
739#[repr(u8)]
740pub enum WubiProfileKind {
741    #[default]
742    Wubi86 = 0,
743    Wubi98 = 1,
744}
745
746impl WubiProfileKind {
747    pub fn from_u8(value: u8) -> Option<Self> {
748        Some(match value {
749            0 => Self::Wubi86,
750            1 => Self::Wubi98,
751            _ => return None,
752        })
753    }
754
755    pub fn from_name(name: &str) -> Option<Self> {
756        Some(match name {
757            "wubi86" => Self::Wubi86,
758            "wubi98" => Self::Wubi98,
759            _ => return None,
760        })
761    }
762
763    pub fn name(self) -> &'static str {
764        match self {
765            Self::Wubi86 => "wubi86",
766            Self::Wubi98 => "wubi98",
767        }
768    }
769
770    /// `msime-pinyin.db` 里这一版的码表,表名与 `name` 相同。
771    pub fn table(self) -> &'static str {
772        self.name()
773    }
774
775    /// 这一版的个人词条和学习记录归入的词库种类。
776    pub fn dictionary_kind(self) -> PersonalDictionaryKind {
777        match self {
778            Self::Wubi86 => PersonalDictionaryKind::Wubi,
779            Self::Wubi98 => PersonalDictionaryKind::Wubi98,
780        }
781    }
782}
783
784pub use crate::shared_string::SharedString;
785
786/// One candidate row. Every field the goldens record is here from the start, including the ones the overlays added.
787#[derive(Debug, Clone, PartialEq, Default)]
788pub struct WordItem {
789    /// The code the row matched, as typed. Composition advance consumes exactly this.
790    pub pinyin: SharedString,
791    /// The complete quanpin key from the dictionary, used when a phrase is persisted.
792    pub canonical_pinyin: SharedString,
793    pub word: SharedString,
794    /// Dictionary weight; for lattice rows `trunc(log_prob * 1000)`, which is not a sort key.
795    pub weight: i64,
796    pub source: CandidateSource,
797    /// The scheme whose provider produced the row; in mixed wubi it routes learning and removal to the right table.
798    pub scheme: SchemeType,
799    /// 0 when unfixed, otherwise the fixed slot 1..=5.
800    pub fixed_position: i32,
801    pub fuzzy: bool,
802    /// Non-empty for a row read from an autocorrected spelling; holds the folded letters the user typed.
803    pub corrected_from: SharedString,
804    /// Only whole-sentence association rows (lattice and neural sentences).
805    pub sentence_association: bool,
806    /// Word boundaries of a decoded sentence, for personal context learning.
807    pub sentence_words: Vec<String>,
808    /// 26 键双拼按触点计价的纠错整句,而且落点支持把它留在引擎给的座位上(分数不比字面整句低出 `TouchTuning::show_margin`):runtime 不把它当整句的次选读法挪到表尾。其余行永远为假。
809    pub touch_supported: bool,
810    /// 26 键双拼按触点计价的纠错整句,触点支持而且明显比字面整句好,按首选规则(`shuangpin::dictionary` 的 `confident_correction`)坐了第一位。宿主据此给首选画强调色、读屏念「已纠正」(视图里的 `confident_correction`)。带它的行同时 `touch_supported`;其余行永远为假。
811    pub confident_correction: bool,
812}
813
814impl WordItem {
815    /// 只把真实词网格整句的分交给独立模型;字典权重和其它来源没有同一计分单位。产品门面 `ordering` 的输入,和它一样不进文档。
816    #[doc(hidden)]
817    pub fn static_evidence(&self) -> Option<crate::ordering::StaticEvidence> {
818        if !self.sentence_association
819            || !matches!(
820                self.source,
821                CandidateSource::Generated
822                    | CandidateSource::NeuralKeyboard
823                    | CandidateSource::Collocation
824            )
825            || self.canonical_pinyin.is_empty()
826            || self.word.is_empty()
827        {
828            return None;
829        }
830        let syllables = self.canonical_pinyin.split('\'').count();
831        let bonus =
832            (crate::lattice::decode::PHRASE_LENGTH_BONUS * 1000.0 * syllables as f64) as i64;
833        Some(crate::ordering::StaticEvidence {
834            log_prob_milli_nats: self.weight.saturating_sub(bonus),
835            characters: self.word.chars().count(),
836        })
837    }
838
839    pub fn new(
840        pinyin: impl Into<String>,
841        word: impl Into<String>,
842        weight: i64,
843        source: CandidateSource,
844        canonical_pinyin: impl Into<String>,
845    ) -> Self {
846        Self {
847            pinyin: SharedString::from(Into::<String>::into(pinyin)),
848            canonical_pinyin: SharedString::from(Into::<String>::into(canonical_pinyin)),
849            word: SharedString::from(Into::<String>::into(word)),
850            weight,
851            source,
852            ..Self::default()
853        }
854    }
855}
856
857/// Fuzzy pinyin rule bits. The values are shared with platform preferences and must not change.
858pub mod fuzzy_rule {
859    pub const Z_ZH: u32 = 1 << 0;
860    pub const C_CH: u32 = 1 << 1;
861    pub const S_SH: u32 = 1 << 2;
862    pub const N_L: u32 = 1 << 3;
863    pub const F_H: u32 = 1 << 4;
864    pub const R_L: u32 = 1 << 5;
865    pub const AN_ANG: u32 = 1 << 6;
866    pub const EN_ENG: u32 = 1 << 7;
867    pub const IN_ING: u32 = 1 << 8;
868    pub const IAN_IANG: u32 = 1 << 9;
869    pub const UAN_UANG: u32 = 1 << 10;
870    /// Every defined rule; the host masks its preference with this.
871    pub const ALL: u32 = 0x7ff;
872}
873
874#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
875pub struct FuzzyPinyinOptions {
876    pub rules: u32,
877}
878
879impl FuzzyPinyinOptions {
880    pub fn enabled(self, rule: u32) -> bool {
881        self.rules & rule != 0
882    }
883}
884
885/// Quanpin autocorrect type bits (`SessionOptions::autocorrect_types`). Either of the first two also enables deletion and insertion, see `request_autocorrect_mask`.
886pub mod autocorrect_type {
887    pub const TRANSPOSITION: u32 = 1 << 0;
888    pub const NEIGHBOR: u32 = 1 << 1;
889    pub const DELETION: u32 = 1 << 2;
890    pub const INSERTION: u32 = 1 << 3;
891    pub const MISSING_OR_EXTRA: u32 = DELETION | INSERTION;
892}
893
894/// The mask the quanpin dictionary runs with for a request: the two user switches, and when either is on, also missing and extra letters.
895pub fn request_autocorrect_mask(transposition: bool, neighbor: bool) -> u32 {
896    let legacy = if transposition {
897        autocorrect_type::TRANSPOSITION
898    } else {
899        0
900    } | if neighbor {
901        autocorrect_type::NEIGHBOR
902    } else {
903        0
904    };
905    if legacy == 0 {
906        0
907    } else {
908        legacy | autocorrect_type::MISSING_OR_EXTRA
909    }
910}
911
912/// Neural sentence association switches. The desktop-model switch is not among them: that model runs only as the input runtime's settled reranker, which the host gates on the preference.
913#[derive(Debug, Clone, PartialEq)]
914pub struct SentenceAssociationOptions {
915    pub word_lattice: bool,
916    /// The Engine's own keyboard-model rerank: nine-key reranks once across its segmentations, the quanpin and shuangpin dictionaries rerank inside the lattice, and the model's pick becomes a `NeuralKeyboard` row after the lattice best. Hosts leave it off: 整句联想「增强」 (the `neural_keyboard` preference) mounts the input runtime's per-keystroke reranker instead (host-api `keyboard_reranker_enabled`). It stays for evals and experiments that build the options directly (`convert_eval --keyboard-neural`).
917    pub neural_keyboard: bool,
918    pub show_next_on_duplicate: bool,
919    /// octagram `.gram` 语法模型的路径(宿主按模型包解析好后传入);空 = 关闭。
920    /// 路径无效时词典静默退回无搭配打分,与 `msime-bigram.bin` 缺失的回退一致。
921    pub collocation_model: std::path::PathBuf,
922    /// 搭配项的标定系数(ln 域结果由装配方换算后按它缩放);0 = 关闭。
923    pub collocation_weight: f64,
924    /// 解码最优路径是否作为 `Collocation` 整句行显示;关掉时搭配分仍参与解码。
925    pub collocation_rerank: bool,
926    /// octagram 六参数;默认对齐万象现行,见 `crate::lattice::octagram::GramConfig`。
927    pub collocation_grammar: crate::lattice::octagram::GramConfig,
928}
929
930impl Default for SentenceAssociationOptions {
931    fn default() -> Self {
932        Self {
933            word_lattice: true,
934            neural_keyboard: false,
935            show_next_on_duplicate: false,
936            collocation_model: std::path::PathBuf::new(),
937            collocation_weight: 1.0,
938            collocation_rerank: false,
939            collocation_grammar: crate::lattice::octagram::GramConfig::default(),
940        }
941    }
942}
943
944/// 默认的首页候选行数:宿主每页候选数(`client-core` 的 `candidate_page_size`)的缺省值。引擎只把它当「第一页能放几行」用。
945pub const DEFAULT_FIRST_PAGE_ROWS: usize = 6;
946
947/// 一次请求带的首页行数。`QueryRequest` 派生 `Default`,裸 `usize` 会默认成 0 从而把补全词整组关掉,所以用带默认值的新类型;`new` 把 0 归一成 1。
948#[derive(Debug, Clone, Copy, PartialEq, Eq)]
949pub struct FirstPageRows(usize);
950
951impl FirstPageRows {
952    pub const fn new(rows: usize) -> Self {
953        Self(if rows == 0 { 1 } else { rows })
954    }
955
956    pub const fn get(self) -> usize {
957        self.0
958    }
959}
960
961impl Default for FirstPageRows {
962    fn default() -> Self {
963        Self(DEFAULT_FIRST_PAGE_ROWS)
964    }
965}
966
967/// 双拼辅助码的触发方式开关(Windows v0.11.1 `[helpcode]` 组的双拼专属键),随 `QueryRequest` 交给双拼引擎。默认值与 Windows 出厂配置一致;`direct` 控制末尾直接,`trigger_*` 控制末尾间接,`mid_sentence*` 控制句中间接,`direct_helpcode*` 控制逐音节直接。
968#[derive(Debug, Clone, Copy, PartialEq, Eq)]
969pub struct ShuangpinHelpcodeModes {
970    /// 只在 Tab 后输入辅助码;保留其它触发方式的设置,关闭后恢复。
971    pub manual_tab: bool,
972    /// 末尾直接:完整音节基座后的裸字母按辅助码读。关闭后只认触发键开启的段。
973    pub direct: bool,
974    /// 末尾间接触发键:反引号段。
975    pub trigger_backtick: bool,
976    /// 末尾间接触发键:分号段。
977    pub trigger_semicolon: bool,
978    /// 末尾间接触发键:段首大写。
979    pub trigger_uppercase: bool,
980    /// 句中(间接)辅助码总开关。
981    pub mid_sentence: bool,
982    pub mid_sentence_backtick: bool,
983    pub mid_sentence_semicolon: bool,
984    pub mid_sentence_uppercase: bool,
985    /// 句中(直接)「万象式」总开关。
986    pub direct_helpcode: bool,
987    pub direct_helpcode_slash: bool,
988    pub direct_helpcode_uppercase: bool,
989}
990
991impl Default for ShuangpinHelpcodeModes {
992    /// Windows v0.11.1 出厂值:末尾直接开、末尾间接的分号开,句中间接与万象式直接关。
993    fn default() -> Self {
994        Self {
995            manual_tab: false,
996            direct: true,
997            trigger_backtick: false,
998            trigger_semicolon: true,
999            trigger_uppercase: false,
1000            mid_sentence: false,
1001            mid_sentence_backtick: true,
1002            mid_sentence_semicolon: false,
1003            mid_sentence_uppercase: false,
1004            direct_helpcode: false,
1005            direct_helpcode_slash: true,
1006            direct_helpcode_uppercase: false,
1007        }
1008    }
1009}
1010
1011/// 一个音节上的辅助码约束(`core/syllable_helpcode.h` 的 `SyllableHelpcode`)。
1012#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1013pub struct SyllableHelpcode {
1014    /// 受约束的音节,按去掉辅助码段之后的双拼切分计,0 起。
1015    pub syllable: usize,
1016    /// 第一码,小写字母;0 表示这一段还没敲码。
1017    pub first: u8,
1018    /// 第二码,小写字母;0 表示没有第二码,只按第一码筛。
1019    pub second: u8,
1020}
1021
1022/// What a scheme turns its composition into for the providers. Built fresh on every refresh; providers never mutate it.
1023#[derive(Debug, Clone, PartialEq, Default)]
1024pub struct QueryRequest {
1025    pub scheme: SchemeType,
1026    /// Lowercased, apostrophes kept.
1027    pub raw_input: String,
1028    pub raw_input_with_cases: String,
1029    /// 输入串里带辅助码段('`' 句中、'|' 末尾)时,`raw_input` / `raw_input_with_cases` 是每段换成
1030    /// `'`(末尾的末尾段去掉)的去段串,原串(用户敲的原样)在这里。没有段时为空。
1031    pub raw_input_with_helpcode_marks: String,
1032    /// 句中辅助码约束;段敲进串里就带着,是否参与筛查看 `shuangpin_helpcode_modes.mid_sentence`。
1033    pub syllable_helpcodes: Vec<SyllableHelpcode>,
1034    /// 末尾段敲下的辅码(小写,至多两个),只在段位于输入串末尾时非空;查询时一码重排、两码筛选。
1035    pub end_helpcodes: String,
1036    /// 去段串(`raw_input_with_cases`)到原串(`raw_input_with_helpcode_marks`)的下标映射,末尾
1037    /// 多一项等于原串长度;选词推进与音节边界用它把去段位置换回显示位置。
1038    pub helpcode_source_index: Vec<usize>,
1039    /// 每段原样的文本及它挂在哪个音节后面,预编辑据此还原显示。
1040    pub helpcode_decorations: Vec<(usize, String)>,
1041    /// The dictionary key: no apostrophes.
1042    pub normalized_input: String,
1043    pub raw_segmentation: String,
1044    pub normalized_segmentation: String,
1045    pub segmentation: String,
1046    pub enable_shuangpin_helpcode: bool,
1047    /// 双拼辅助码的触发方式;`enable_shuangpin_helpcode` 为真时才读。
1048    pub shuangpin_helpcode_modes: ShuangpinHelpcodeModes,
1049    pub enable_quanpin_helpcode: bool,
1050    /// 一码辅助码是否严格排除未命中的候选;默认关闭以保持历史排序。
1051    pub strict_quanpin_helpcode: bool,
1052    /// 会话的「拼音纠错」开关。名字带 quanpin 是历史原因:双拼也按它决定是否给纠错整句。
1053    pub enable_quanpin_autocorrect_transposition: bool,
1054    pub enable_quanpin_autocorrect_neighbor: bool,
1055    pub fuzzy_pinyin: FuzzyPinyinOptions,
1056    /// Hand back every whole-sentence reading rather than only the best.
1057    pub sentence_alternatives: bool,
1058    pub sentence_association: SentenceAssociationOptions,
1059    /// 首页能放下的行数:全长度组的精确同音词够这么多就不再并入更长的补全词。
1060    pub first_page_rows: FirstPageRows,
1061    /// Committed text the neural sentence models condition on.
1062    pub rescoring_context: String,
1063    /// Korean only: the Hanja list of the composing syllable is open, so the request is answered with its Hanja.
1064    pub korean_hanja: bool,
1065    /// 五笔通配:码里确实有 `z`,provider 改用模式匹配而不是前缀范围扫描(见 [`WubiInputOptions::z_wildcard`])。
1066    pub wubi_z_wildcard: bool,
1067    /// 五笔 14 键:`normalized_input` 每一位是一组字母的代表字母(`wubi::grid`),provider 展开成这几组字母的每种组合再查码表。
1068    pub wubi_grid: bool,
1069    /// 26 键双拼每个按键的触点,与 `raw_input` 逐字节对齐,没有触点的位置为 `None`。只要不是双拼、没有注册几何、一个触点都没有、输入里有 `'` 或句中辅助码生效,就必须是空 Vec 而不是全 `None`:桌面请求因此与没有这个字段时逐字节相同。
1070    pub key_touches: Vec<Option<crate::touch::KeyTouch>>,
1071    /// 触点所在的那份几何;`key_touches` 为空时为 `None`。
1072    pub key_geometry: Option<std::sync::Arc<crate::touch::KeyGeometry>>,
1073    /// 按触点计价的参数。只有 Rust 侧(评测示例)能改,见 [`crate::host::Session::set_touch_tuning`]。
1074    pub touch_tuning: crate::touch::TouchTuning,
1075    /// False means no query at all.
1076    pub valid: bool,
1077}
1078
1079/// A key a scheme can consume. The session maps host characters onto these, so schemes never see virtual-key codes.
1080#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1081pub enum SchemeKey {
1082    /// An ASCII letter, case preserved.
1083    Letter(u8),
1084    Apostrophe,
1085    /// The Microsoft shuangpin `ing` final.
1086    Semicolon,
1087    /// The Japanese long vowel mark.
1088    Minus,
1089    /// Any other printable non-letter ASCII key a scheme claims through its spelling symbols (Zhuyin's `;`, `-`, digits and space). Kept apart from `Semicolon` and `Minus` so the shuangpin and Japanese meanings of those keys never change; the existing schemes ignore it.
1090    Symbol(u8),
1091    Backspace,
1092    /// Re-run the query without changing the composition.
1093    Requery,
1094}
1095
1096/// Engine-level composition commands (the C++ `metasequoia::Command`, plus `CommitRawWithoutLearning`). The host facade numbers them its own way (`host::Command`).
1097#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1098#[repr(u8)]
1099pub enum Command {
1100    Backspace = 0,
1101    CommitCandidate = 1,
1102    CommitRaw = 2,
1103    /// 丢弃组字。越南文和藏文的第一次只把显示切回按键原文,下一次才丢弃。
1104    Cancel = 3,
1105    MoveLeft = 4,
1106    MoveRight = 5,
1107    MoveHome = 6,
1108    MoveEnd = 7,
1109    DeleteForward = 8,
1110    /// Japanese only.
1111    CycleKanaVariant = 9,
1112    /// Japanese only: commit the kana reading.
1113    CommitReading = 10,
1114    /// Open the active scheme's candidate list, or close it when it is open: the composing syllable's Hanja in Korean, the conversion's alternatives in Zhuyin. Schemes without an openable list leave it unhandled. The name stays for the wire and the goldens.
1115    ConvertHanja = 11,
1116    /// 整句改字的光标左移一个字:组字的首选是一个字对一个音节的全拼或双拼整句时,第一次进入改字、光标停在最后一个字前,之后每次左移一个字;进不了改字时与 `MoveLeft` 相同。
1117    ConversionLeft = 12,
1118    /// 整句改字的光标右移一个字,最远到句末;不在改字里时与 `MoveRight` 相同。
1119    ConversionRight = 13,
1120    /// 像 `CommitRaw` 一样原样上屏,但不把上屏的字母学成英文词:专用英文里上屏预编辑原文并结束组字(`CommitRaw` 会学这个词),其余情况与 `CommitRaw` 相同,`SessionOptions::learn_typed_spellings_on_enter` 也不起作用。拼写纠错的「回车上屏原文就关掉这段输入的纠错」照常记下。由 [`crate::Session::command`] 处理,组字的各条内部路径收不到它。
1121    CommitRawWithoutLearning = 14,
1122}
1123
1124impl Command {
1125    pub fn from_u8(value: u8) -> Option<Self> {
1126        Some(match value {
1127            0 => Self::Backspace,
1128            1 => Self::CommitCandidate,
1129            2 => Self::CommitRaw,
1130            3 => Self::Cancel,
1131            4 => Self::MoveLeft,
1132            5 => Self::MoveRight,
1133            6 => Self::MoveHome,
1134            7 => Self::MoveEnd,
1135            8 => Self::DeleteForward,
1136            9 => Self::CycleKanaVariant,
1137            10 => Self::CommitReading,
1138            11 => Self::ConvertHanja,
1139            12 => Self::ConversionLeft,
1140            13 => Self::ConversionRight,
1141            14 => Self::CommitRawWithoutLearning,
1142            _ => return None,
1143        })
1144    }
1145
1146    pub fn name(self) -> &'static str {
1147        match self {
1148            Self::Backspace => "Backspace",
1149            Self::CommitCandidate => "CommitCandidate",
1150            Self::CommitRaw => "CommitRaw",
1151            Self::Cancel => "Cancel",
1152            Self::MoveLeft => "MoveLeft",
1153            Self::MoveRight => "MoveRight",
1154            Self::MoveHome => "MoveHome",
1155            Self::MoveEnd => "MoveEnd",
1156            Self::DeleteForward => "DeleteForward",
1157            Self::CycleKanaVariant => "CycleKanaVariant",
1158            Self::CommitReading => "CommitReading",
1159            Self::ConvertHanja => "ConvertHanja",
1160            Self::ConversionLeft => "ConversionLeft",
1161            Self::ConversionRight => "ConversionRight",
1162            Self::CommitRawWithoutLearning => "CommitRawWithoutLearning",
1163        }
1164    }
1165
1166    pub fn from_name(name: &str) -> Option<Self> {
1167        (0..=14)
1168            .filter_map(Self::from_u8)
1169            .find(|command| command.name() == name)
1170    }
1171}
1172
1173/// Outcome of one key or action. A result can be handled and still carry a diagnostic, for example when learning could not be persisted.
1174#[derive(Debug, Clone, PartialEq, Eq, Default)]
1175pub struct KeyResult {
1176    pub handled: bool,
1177    pub commit: Option<String>,
1178    pub diagnostic: Option<String>,
1179}
1180
1181impl KeyResult {
1182    pub fn unhandled() -> Self {
1183        Self::default()
1184    }
1185
1186    pub fn handled() -> Self {
1187        Self {
1188            handled: true,
1189            ..Self::default()
1190        }
1191    }
1192
1193    pub fn committed(text: impl Into<String>) -> Self {
1194        Self {
1195            handled: true,
1196            commit: Some(text.into()),
1197            diagnostic: None,
1198        }
1199    }
1200
1201    pub fn with_diagnostic(mut self, diagnostic: Option<String>) -> Self {
1202        self.diagnostic = diagnostic;
1203        self
1204    }
1205
1206    /// `diagnostic` 的稳定数字码(映射表见 `diagnostics::DIAGNOSTIC_CODES`);没有诊断时为 None。
1207    pub fn diagnostic_code(&self) -> Option<crate::diagnostics::DiagnosticCode> {
1208        self.diagnostic
1209            .as_deref()
1210            .map(crate::diagnostics::DiagnosticCode::from_message)
1211    }
1212}
1213
1214#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1215#[repr(u8)]
1216pub enum CandidateEdge {
1217    FirstHan = 0,
1218    LastHan = 1,
1219}
1220
1221impl CandidateEdge {
1222    /// 引擎以词定字使用的首个或末个汉字;没有汉字时返回空串。
1223    pub fn character(self, text: &str) -> &str {
1224        match self {
1225            Self::FirstHan => crate::text::first_han_char(text),
1226            Self::LastHan => crate::text::last_han_char(text),
1227        }
1228    }
1229}
1230
1231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1232pub enum LocalInputMode {
1233    #[default]
1234    None,
1235    Unicode,
1236    DateTime,
1237    QuickPhrase,
1238    Emoji,
1239    Kaomoji,
1240    SuperJianpin,
1241    TemporaryEnglish,
1242    TemporaryJapanese,
1243    /// `V`: a calculator, Chinese numerals and dates.
1244    Expression,
1245    /// `/`: built-in commands and the host's command table.
1246    Command,
1247    /// `@`: the names and places of the host's mention list.
1248    Mention,
1249    /// 在全拼、双拼、五笔的组字中键入 `www.`、`http:` 等之后:原样输入的 ASCII 网址。
1250    Url,
1251    /// `C`:拆字反查,按部件的拼音找字(`huohuohuo` 得 焱)。
1252    Chaizi,
1253    /// `S`:符号模式,按分组码(`xl`、`sx`、`szq`、`jt`)或符号名(`alpha`、`infinity`)查符号。
1254    Symbol,
1255}
1256
1257impl LocalInputMode {
1258    /// The name hosts and goldens use (`EngineSnapshot::local_mode`).
1259    pub fn name(self) -> &'static str {
1260        match self {
1261            Self::None => "none",
1262            Self::Unicode => "unicode",
1263            Self::DateTime => "date_time",
1264            Self::QuickPhrase => "quick_phrase",
1265            Self::Emoji => "emoji",
1266            Self::Kaomoji => "kaomoji",
1267            Self::SuperJianpin => "super_jianpin",
1268            Self::TemporaryEnglish => "temporary_english",
1269            Self::TemporaryJapanese => "temporary_japanese",
1270            Self::Expression => "expression",
1271            Self::Command => "command",
1272            Self::Mention => "mention",
1273            Self::Url => "url",
1274            Self::Chaizi => "chaizi",
1275            Self::Symbol => "symbol",
1276        }
1277    }
1278
1279    /// The mode `name` returns the name of; `None` for a name no mode has.
1280    pub fn from_name(name: &str) -> Option<Self> {
1281        [
1282            Self::None,
1283            Self::Unicode,
1284            Self::DateTime,
1285            Self::QuickPhrase,
1286            Self::Emoji,
1287            Self::Kaomoji,
1288            Self::SuperJianpin,
1289            Self::TemporaryEnglish,
1290            Self::TemporaryJapanese,
1291            Self::Expression,
1292            Self::Command,
1293            Self::Mention,
1294            Self::Url,
1295            Self::Chaizi,
1296            Self::Symbol,
1297        ]
1298        .into_iter()
1299        .find(|mode| mode.name() == name)
1300    }
1301
1302    /// The non-letter characters the mode spells with. The runtime hands these to `character` even when a host reports them as punctuation, because finishing the composition on them would commit a half-typed spelling; hosts read digits here as input rather than candidate shortcuts.
1303    pub fn spelling_symbols(self) -> &'static str {
1304        match self {
1305            Self::Unicode => "0123456789",
1306            Self::Expression => crate::local::expression::SPELLING_SYMBOLS,
1307            Self::Url => crate::local::url::SPELLING_SYMBOLS,
1308            Self::TemporaryJapanese => "-",
1309            _ => "",
1310        }
1311    }
1312
1313    /// Whether the mode's rows are text the engine generated rather than words the user spelled. Their commits are never learned and never counted as typing.
1314    pub fn generates_text(self) -> bool {
1315        matches!(self, Self::Expression | Self::Command | Self::Mention)
1316    }
1317
1318    /// 去掉临时模式预编辑开头的入口字母(`Y`、`R`)。它是模式标记、不是用户打的字,所以进词库的词不能带它;回车原样上屏时它算文本(#7616),那一处不走这里。
1319    pub fn without_entry_marker(self, text: &str) -> &str {
1320        match self {
1321            Self::TemporaryEnglish | Self::TemporaryJapanese => text.get(1..).unwrap_or_default(),
1322            _ => text,
1323        }
1324    }
1325
1326    /// 候选注释由 Engine 自己给出、不补辅助码的模式:V、`/`、`@` 的行不是按拼音拼出来的,辅助码说明不了它们;拆字的行注释的是字的读音,用户正是不会读这个字才来查,辅助码夹在读音里只会添乱。和 `generates_text` 分开,是因为拆字上屏的字是用户挑出来的,照常计入打字统计。
1327    pub fn annotates_rows(self) -> bool {
1328        matches!(
1329            self,
1330            Self::Expression | Self::Command | Self::Mention | Self::Chaizi
1331        )
1332    }
1333}
1334
1335#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1336pub enum FrequencyAdjustmentMode {
1337    #[default]
1338    Disabled,
1339    Pin,
1340    Halve,
1341    Linear,
1342    Promote,
1343}
1344
1345impl FrequencyAdjustmentMode {
1346    /// The mode name the ranking writers and the host options use.
1347    pub fn name(self) -> &'static str {
1348        match self {
1349            Self::Disabled => "disabled",
1350            Self::Pin => "pin",
1351            Self::Halve => "halve",
1352            Self::Linear => "linear",
1353            Self::Promote => "promote",
1354        }
1355    }
1356
1357    pub fn from_name(name: &str) -> Option<Self> {
1358        Some(match name {
1359            "disabled" => Self::Disabled,
1360            "pin" => Self::Pin,
1361            "halve" => Self::Halve,
1362            "linear" => Self::Linear,
1363            "promote" => Self::Promote,
1364            _ => return None,
1365        })
1366    }
1367}
1368
1369#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1370pub struct FrequencyAdjustmentOptions {
1371    pub mode: FrequencyAdjustmentMode,
1372    /// 1..=10.
1373    pub trigger_count: i32,
1374    /// 1..=10.
1375    pub linear_step: i32,
1376}
1377
1378impl Default for FrequencyAdjustmentOptions {
1379    fn default() -> Self {
1380        Self {
1381            mode: FrequencyAdjustmentMode::Disabled,
1382            trigger_count: 1,
1383            linear_step: 1,
1384        }
1385    }
1386}
1387
1388/// Which local modes may be entered. The Shift+letter modes of the reference are on by default, symbol mode (`S`) among them, as the reference's factory configuration ships it on; expression (`V`) follows the reference's `v_mode` factory and is on, command (`/`) and mention (`@`) are off, because entering them takes a key that used to reach the host as text. 拆字 (`C`) 在共享偏好层默认开(对齐参考的 `radical_mode` 出厂值),host-api 把未写偏好的文档读作开。
1389#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1390pub struct LocalModeOptions {
1391    pub unicode: bool,
1392    /// Shift+T 日期时间快捷输入模式(参考 `utility.date_time_mode`)。行内日期时间混入普通候选由 `date_time_inline` 单独开关(参考 `utility.date_time_candidates`,出厂关)。
1393    pub date_time: bool,
1394    /// 全拼输入 `riqi`/`sj`/`xq`/`nongli` 时把当前日期、时间、星期或农历接在对应的词后面(参考 `utility.date_time_candidates`)。与 T 模式独立:T 模式关着时行内混入仍可开。
1395    pub date_time_inline: bool,
1396    /// 「📅日期」入口:日期时间混进普通候选时,第三位放一个入口,选中后候选窗换成这一组的全部格式(参考 `utility.date_time_menu`,出厂开)。依附 `date_time_inline`。
1397    pub date_time_menu: bool,
1398    pub quick_phrase: bool,
1399    pub emoji: bool,
1400    pub kaomoji: bool,
1401    pub super_jianpin: bool,
1402    pub temporary_english: bool,
1403    pub temporary_japanese: bool,
1404    pub expression: bool,
1405    pub command: bool,
1406    pub mention: bool,
1407    /// `C`:拆字反查。
1408    pub chaizi: bool,
1409    /// `S`:符号模式。
1410    pub symbol: bool,
1411    /// 总开关:没有组字时 Shift+字母(U/T/K/E/M/J/Y/R/V/C/S)能否进入对应的快捷模式。关掉后这些键照常打出大写字母,各模式自己的开关不变;`/` 和 `@` 不经过它。
1412    pub shift_letter_entry: bool,
1413}
1414
1415impl Default for LocalModeOptions {
1416    fn default() -> Self {
1417        Self {
1418            unicode: true,
1419            date_time: true,
1420            date_time_inline: true,
1421            date_time_menu: true,
1422            quick_phrase: true,
1423            emoji: true,
1424            kaomoji: true,
1425            super_jianpin: true,
1426            temporary_english: true,
1427            temporary_japanese: true,
1428            expression: false,
1429            command: false,
1430            mention: false,
1431            chaizi: false,
1432            symbol: true,
1433            shift_letter_entry: true,
1434        }
1435    }
1436}
1437
1438/// One command of a host-supplied command table (`/` mode). The host validates the table it read; the engine still skips a row it cannot use rather than trusting it.
1439///
1440/// `template` is literal text with three kinds of placeholder: `{date}` or `{date:FORMAT}`, `{time}` or `{time:FORMAT}`, and `{weekday}`. FORMAT is a strftime pattern (`%Y-%m-%d`), the defaults are `%Y-%m-%d` and `%H:%M`, and `{weekday}` is the Chinese day name (星期四). Any other brace, including one left unclosed, makes the row unusable.
1441#[derive(Debug, Clone, PartialEq, Eq)]
1442pub struct CommandTableEntry {
1443    /// 1..=32 lowercase ASCII letters, what the user types after `/`.
1444    pub trigger: String,
1445    /// Shown beside the row.
1446    pub title: String,
1447    pub template: String,
1448}
1449
1450/// 一条脚本插件指令(`/` 模式):插件在清单里声明的触发词和标题。宿主把会运行的插件声明的指令按执行顺序交给引擎,引擎按 `local::command::usable_plugin_commands` 决定哪些生效(用户指令表赢、插件先到先得),不照单全收。
1451#[derive(Debug, Clone, PartialEq, Eq)]
1452pub struct PluginCommand {
1453    /// 2..=16 个小写 ASCII 字母,即 `/` 之后输入的触发词。
1454    pub trigger: String,
1455    /// 1..=8 个字符,由 runtime 画在插件注释格里,不进引擎的注释。
1456    pub title: String,
1457    /// 清单里写了 `arguments = "extended"`:完整打出触发词之后,参数除了小写字母还收数字和 [`crate::local::command::EXTENDED_ARGUMENT_SYMBOLS`],这时宿主把数字当输入、不当选词键(见 `InputSession::spelling_symbols`)。
1458    pub extended: bool,
1459}
1460
1461/// 宿主提供的一条 K 模式短语(来自已启用的短语表插件)。宿主已经校验过;Engine 仍然跳过它用不了的行,而不是照单全收。
1462#[derive(Debug, Clone, PartialEq, Eq)]
1463pub struct QuickPhraseEntry {
1464    /// 1..=32 个小写 ASCII 字母,即 K 之后输入的编码。
1465    pub key: String,
1466    /// 上屏的文本。
1467    pub text: String,
1468}
1469
1470/// One name or place of the host's mention list (`@` mode).
1471#[derive(Debug, Clone, PartialEq, Eq)]
1472pub struct MentionEntry {
1473    /// The committed text.
1474    pub text: String,
1475    /// Lowercase pinyin syllables joined by `'` (`zhang'san`), matched by prefix and by initials; empty to match an ASCII `text` by its own letters only.
1476    pub key: String,
1477    /// 联系人手机号;普通名单为空,联系人候选命中姓名拼音时提交这一栏。
1478    pub phone: String,
1479}
1480
1481#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1482pub struct EnglishInputOptions {
1483    pub mixed_candidates: bool,
1484    /// 补全与英文领先的触发字符数,范围 1..=8;26 键中更短的完整词可选,但不抢占中文首选。
1485    pub minimum_prefix: usize,
1486    /// 中文状态下首页最多展示几个英文候选(Windows `general.cn_en_mixed_input_first_page_count`),范围 1..=3:第一行英文进优先位置之后,其余英文紧跟它继续占首页的空位,放不下的照旧接在列表末尾。
1487    pub first_page_count: usize,
1488}
1489
1490impl Default for EnglishInputOptions {
1491    fn default() -> Self {
1492        Self {
1493            mixed_candidates: false,
1494            minimum_prefix: 2,
1495            first_page_count: 1,
1496        }
1497    }
1498}
1499
1500#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1501pub struct MixedExpressiveOptions {
1502    pub emoji_candidates: bool,
1503    pub kaomoji_candidates: bool,
1504    /// 打拼音出符号:整段组字正好拼出某个符号的名称时混入符号行(`local::symbol_candidates`)。
1505    pub symbol_candidates: bool,
1506}
1507
1508#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1509pub struct WubiInputOptions {
1510    /// Answer an unmatched wubi code with quanpin candidates for the same letters.
1511    pub mixed_pinyin: bool,
1512    /// 查询、学习和删除所用的码表版本。
1513    pub profile: WubiProfileKind,
1514    /// `z` 通配:码里的 `z` 匹配码表该位置的任意一个字母(含首位)。
1515    ///
1516    /// 与混输无关:混输里 `z` 是普通拼音字母,这里它是未知码位。两者都关时 `z` 不进码。
1517    pub z_wildcard: bool,
1518}
1519
1520/// 在线请求的输入语义;缺省沿用原有的拼音及方案转换路径。
1521#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
1522#[serde(rename_all = "snake_case")]
1523pub enum OnlineInputMode {
1524    #[default]
1525    Pinyin,
1526    English,
1527}
1528
1529impl OnlineInputMode {
1530    pub fn is_pinyin(&self) -> bool {
1531        *self == Self::Pinyin
1532    }
1533}
1534
1535/// 宿主交给云或 AI 服务的查询,回包必须原样带回;引擎在插入候选前核对当前组字的每个字段。
1536#[derive(Debug, Clone, PartialEq, Eq, Default)]
1537pub struct OnlineQuery {
1538    pub mode: OnlineInputMode,
1539    pub scheme: SchemeType,
1540    pub generation: u64,
1541    pub identity: String,
1542    pub query_text: String,
1543    pub cache_key: String,
1544    pub pinyin_segments: Vec<String>,
1545    pub cloud_eligible: bool,
1546    pub ai_eligible: bool,
1547    pub session_id: u64,
1548}
1549
1550/// What `/fy` asks the user's translation service: the English typed after the trigger, to be translated into Chinese. Unlike `OnlineQuery` it is never raised by spelling; only this command in the `/` mode produces one, and the answer is refused unless the session and the text are still the ones that asked.
1551#[derive(Debug, Clone, PartialEq, Eq, Default)]
1552pub struct CommandTranslationQuery {
1553    pub session_id: u64,
1554    pub text: String,
1555}
1556
1557/// 插件指令要宿主去问的那一次:`/` 模式里输入以某个生效的插件触发词开头、这份预编辑串还没有缓存的行时才有。宿主把触发词和参数交给触发词的主人,再把结果连同这个值交回 `Session::apply_plugin_command_rows`;会话或预编辑串已经变了的回答被拒绝。
1558#[derive(Debug, Clone, PartialEq, Eq, Default)]
1559pub struct PluginCommandQuery {
1560    pub session_id: u64,
1561    /// 整段本地预编辑串,含开头的 `/`。
1562    pub preedit: String,
1563    pub trigger: String,
1564    /// 触发词后面的小写字母,可以为空(`/uuid`)。
1565    pub argument: String,
1566}
1567
1568/// Which dictionary a personal entry or journal row belongs to. The ordinal is the host ABI value.
1569///
1570/// `non_exhaustive` because the host facade re-exports this as the bridge's `DictionaryKind`, a cxx shared enum that callers had to match with a wildcard arm (host-api `dictionary.rs`); a closed enum would turn those arms into `unreachable_patterns` warnings. Matches inside this crate stay exhaustive.
1571#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
1572#[repr(u8)]
1573#[non_exhaustive]
1574pub enum PersonalDictionaryKind {
1575    #[default]
1576    Pinyin = 0,
1577    Wubi = 1,
1578    QuickPhrase = 2,
1579    English = 3,
1580    /// 98 五笔,写入 `wubi98` 表。序号追加在末尾,已有的值不变。
1581    Wubi98 = 4,
1582}
1583
1584impl PersonalDictionaryKind {
1585    pub fn from_u8(value: u8) -> Option<Self> {
1586        Some(match value {
1587            0 => Self::Pinyin,
1588            1 => Self::Wubi,
1589            2 => Self::QuickPhrase,
1590            3 => Self::English,
1591            4 => Self::Wubi98,
1592            _ => return None,
1593        })
1594    }
1595
1596    /// 两版五笔共用编码规则(1..=4 个 a..=y 字母)与按码排序的写法,只是表不同。
1597    pub fn is_wubi(self) -> bool {
1598        matches!(self, Self::Wubi | Self::Wubi98)
1599    }
1600
1601    /// 五笔种类对应的 `msime-pinyin.db` 码表。
1602    pub fn wubi_table(self) -> Option<&'static str> {
1603        match self {
1604            Self::Wubi => Some(WubiProfileKind::Wubi86.table()),
1605            Self::Wubi98 => Some(WubiProfileKind::Wubi98.table()),
1606            Self::Pinyin | Self::QuickPhrase | Self::English => None,
1607        }
1608    }
1609
1610    /// The `dictionary` column value in `user_dictionary_operations`. Quick phrases are stored as `quick`.
1611    pub fn journal_name(self) -> &'static str {
1612        match self {
1613            Self::Pinyin => "pinyin",
1614            Self::Wubi => "wubi",
1615            Self::QuickPhrase => "quick",
1616            Self::English => "english",
1617            Self::Wubi98 => "wubi98",
1618        }
1619    }
1620
1621    pub fn from_journal_name(name: &str) -> Option<Self> {
1622        Some(match name {
1623            "pinyin" => Self::Pinyin,
1624            "wubi" => Self::Wubi,
1625            "quick" => Self::QuickPhrase,
1626            "english" => Self::English,
1627            "wubi98" => Self::Wubi98,
1628            _ => return None,
1629        })
1630    }
1631}
1632
1633/// A user's own dictionary entry. Pinyin keys are complete syllables joined by `'`; wubi 1..=4 letters; quick phrases 1..=32 letters or digits; English 1..=64 letters.
1634#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1635pub struct PersonalDictionaryEntry {
1636    pub kind: PersonalDictionaryKind,
1637    pub key: String,
1638    pub value: String,
1639    pub weight: i64,
1640}
1641
1642impl PersonalDictionaryEntry {
1643    pub const DEFAULT_WEIGHT: i64 = 100_000;
1644}
1645
1646impl Default for PersonalDictionaryEntry {
1647    fn default() -> Self {
1648        Self {
1649            kind: PersonalDictionaryKind::Pinyin,
1650            key: String::new(),
1651            value: String::new(),
1652            weight: Self::DEFAULT_WEIGHT,
1653        }
1654    }
1655}
1656
1657#[cfg(test)]
1658mod tests {
1659    use super::{CandidateSource, SchemeSet, SchemeType, WordItem};
1660
1661    /// 线上的来源编号按值往返;10 空着(原 `NeuralDesktop`,未发布就删了)。日文混输行(13)不属于任何谓词:上屏走整段结束组字的路,不学习,不坐在线座位。
1662    #[test]
1663    fn candidate_sources_round_trip_and_mixed_japanese_matches_no_predicate() {
1664        for value in 0..=u8::MAX {
1665            match CandidateSource::from_u8(value) {
1666                Some(source) => assert_eq!(source as u8, value),
1667                None => assert!(value == 10 || value > 14, "{value}"),
1668            }
1669        }
1670        let japanese = CandidateSource::MixedJapanese;
1671        assert_eq!(CandidateSource::from_u8(13), Some(japanese));
1672        assert!(!japanese.is_dictionary());
1673        assert!(!japanese.is_generated_or_fallback());
1674        assert!(!japanese.is_sentence_learning());
1675        assert!(!japanese.is_online());
1676        assert!(!japanese.takes_online_seat());
1677        assert!(!japanese.holds_ai_seat());
1678    }
1679
1680    /// 插件指令行(14)同样不属于任何谓词:不学习、不坐在线座位。
1681    #[test]
1682    fn plugin_rows_match_no_predicate() {
1683        let plugin = CandidateSource::Plugin;
1684        assert_eq!(CandidateSource::from_u8(14), Some(plugin));
1685        assert_eq!(plugin as u8, 14);
1686        assert!(!plugin.is_dictionary());
1687        assert!(!plugin.is_generated_or_fallback());
1688        assert!(!plugin.is_sentence_learning());
1689        assert!(!plugin.is_online());
1690        assert!(!plugin.takes_online_seat());
1691        assert!(!plugin.holds_ai_seat());
1692    }
1693
1694    type Row = (&'static str, fn(SchemeType) -> bool, [bool; 5]);
1695    type Named = (&'static str, fn(SchemeType) -> bool);
1696
1697    const SCHEMES: [SchemeType; 5] = [
1698        SchemeType::Quanpin,
1699        SchemeType::Shuangpin,
1700        SchemeType::Wubi,
1701        SchemeType::JapaneseRomaji,
1702        SchemeType::Korean,
1703    ];
1704
1705    // Each row lists Quanpin, Shuangpin, Wubi, Japanese and Korean; the values are the behaviour these schemes had before the checks became predicates.
1706    #[test]
1707    fn predicates_keep_the_behaviour_of_the_existing_schemes() {
1708        let table: [Row; 26] = [
1709            (
1710                "is_chinese",
1711                SchemeType::is_chinese,
1712                [true, true, true, false, false],
1713            ),
1714            (
1715                "outputs_traditional_natively",
1716                SchemeType::outputs_traditional_natively,
1717                [false; 5],
1718            ),
1719            (
1720                "script_conversion_applies",
1721                SchemeType::script_conversion_applies,
1722                [true, true, true, false, false],
1723            ),
1724            (
1725                "uses_chinese_punctuation",
1726                SchemeType::uses_chinese_punctuation,
1727                [true, true, true, true, false],
1728            ),
1729            (
1730                "host_smart_punctuation",
1731                SchemeType::host_smart_punctuation,
1732                [true, true, true, false, false],
1733            ),
1734            (
1735                "widens_full_width",
1736                SchemeType::widens_full_width,
1737                [true, true, true, true, false],
1738            ),
1739            (
1740                "allows_english_emoji_mixing",
1741                SchemeType::allows_english_emoji_mixing,
1742                [true, true, false, false, false],
1743            ),
1744            (
1745                "opens_local_modes",
1746                SchemeType::opens_local_modes,
1747                [true, true, false, false, false],
1748            ),
1749            (
1750                "learns_into_main_dictionary",
1751                SchemeType::learns_into_main_dictionary,
1752                [true, true, true, false, false],
1753            ),
1754            (
1755                "learns_english_words",
1756                SchemeType::learns_english_words,
1757                [true, true, false, false, false],
1758            ),
1759            (
1760                "cloud_eligible",
1761                SchemeType::cloud_eligible,
1762                [true, true, false, true, false],
1763            ),
1764            (
1765                "shows_glosses",
1766                SchemeType::shows_glosses,
1767                [true, true, true, false, true],
1768            ),
1769            (
1770                "commits_on_blur",
1771                SchemeType::commits_on_blur,
1772                [false, false, false, false, true],
1773            ),
1774            (
1775                "holds_phrase_progress",
1776                SchemeType::holds_phrase_progress,
1777                [true, true, true, true, false],
1778            ),
1779            (
1780                "reranks_with_sentence_model",
1781                SchemeType::reranks_with_sentence_model,
1782                [true, true, false, false, false],
1783            ),
1784            (
1785                "draws_reading",
1786                SchemeType::draws_reading,
1787                [false, false, false, true, true],
1788            ),
1789            (
1790                "has_openable_candidate_list",
1791                SchemeType::has_openable_candidate_list,
1792                [false, false, false, false, true],
1793            ),
1794            (
1795                "cancel_keeps_composition",
1796                SchemeType::cancel_keeps_composition,
1797                [false, false, false, false, true],
1798            ),
1799            (
1800                "selection_completes",
1801                SchemeType::selection_completes,
1802                [false, false, false, true, true],
1803            ),
1804            (
1805                "follows_pinyin_candidates",
1806                SchemeType::follows_pinyin_candidates,
1807                [true, true, false, false, false],
1808            ),
1809            (
1810                "accepts_apostrophe",
1811                SchemeType::accepts_apostrophe,
1812                [true, true, false, true, false],
1813            ),
1814            (
1815                "locks_caret",
1816                SchemeType::locks_caret,
1817                [false, false, false, false, true],
1818            ),
1819            (
1820                "supports_fuzzy",
1821                SchemeType::supports_fuzzy,
1822                [true, true, false, false, false],
1823            ),
1824            (
1825                "supports_autocorrect",
1826                SchemeType::supports_autocorrect,
1827                [true, false, false, false, false],
1828            ),
1829            (
1830                "nine_key",
1831                SchemeType::nine_key,
1832                [true, false, false, false, false],
1833            ),
1834            (
1835                "helpcode",
1836                SchemeType::helpcode,
1837                [true, true, false, false, false],
1838            ),
1839        ];
1840        for (name, predicate, expected) in table {
1841            for (scheme, want) in SCHEMES.into_iter().zip(expected) {
1842                assert_eq!(predicate(scheme), want, "{name} for {scheme:?}");
1843            }
1844        }
1845    }
1846
1847    #[test]
1848    fn only_the_chinese_typing_schemes_detect_urls() {
1849        for code in 0..=9 {
1850            let scheme = SchemeType::from_u8(code).expect("scheme code");
1851            let expected = matches!(
1852                scheme,
1853                SchemeType::Quanpin | SchemeType::Shuangpin | SchemeType::Wubi
1854            );
1855            assert_eq!(scheme.detects_urls(), expected, "{scheme:?}");
1856        }
1857    }
1858
1859    #[test]
1860    fn local_input_mode_names_round_trip() {
1861        use super::LocalInputMode;
1862        // 穷举 match:新增变体时这里编译不过,提醒把它接进链条,`from_name` 的手写数组漏掉它时下面的断言就会失败。
1863        fn next(mode: LocalInputMode) -> Option<LocalInputMode> {
1864            Some(match mode {
1865                LocalInputMode::None => LocalInputMode::Unicode,
1866                LocalInputMode::Unicode => LocalInputMode::DateTime,
1867                LocalInputMode::DateTime => LocalInputMode::QuickPhrase,
1868                LocalInputMode::QuickPhrase => LocalInputMode::Emoji,
1869                LocalInputMode::Emoji => LocalInputMode::Kaomoji,
1870                LocalInputMode::Kaomoji => LocalInputMode::SuperJianpin,
1871                LocalInputMode::SuperJianpin => LocalInputMode::TemporaryEnglish,
1872                LocalInputMode::TemporaryEnglish => LocalInputMode::TemporaryJapanese,
1873                LocalInputMode::TemporaryJapanese => LocalInputMode::Expression,
1874                LocalInputMode::Expression => LocalInputMode::Command,
1875                LocalInputMode::Command => LocalInputMode::Mention,
1876                LocalInputMode::Mention => LocalInputMode::Url,
1877                LocalInputMode::Url => LocalInputMode::Chaizi,
1878                LocalInputMode::Chaizi => LocalInputMode::Symbol,
1879                LocalInputMode::Symbol => return None,
1880            })
1881        }
1882        let mut names = Vec::new();
1883        let mut mode = Some(LocalInputMode::None);
1884        while let Some(current) = mode {
1885            assert_eq!(
1886                LocalInputMode::from_name(current.name()),
1887                Some(current),
1888                "{current:?}"
1889            );
1890            names.push(current.name());
1891            mode = next(current);
1892        }
1893        assert_eq!(names.len(), 15);
1894        assert!(names.contains(&"url"));
1895        assert!(names.contains(&"chaizi"));
1896        assert!(names.contains(&"symbol"));
1897        assert_eq!(LocalInputMode::from_name("unknown"), None);
1898    }
1899
1900    #[test]
1901    fn existing_scheme_codes_are_unchanged() {
1902        for (code, scheme) in SCHEMES.into_iter().enumerate() {
1903            assert_eq!(scheme as u8, code as u8);
1904            assert_eq!(SchemeType::from_u8(code as u8), Some(scheme));
1905        }
1906    }
1907
1908    #[test]
1909    fn vietnamese_round_trips_through_code_seven() {
1910        assert_eq!(SchemeType::Vietnamese as u8, 7);
1911        assert_eq!(SchemeType::from_u8(7), Some(SchemeType::Vietnamese));
1912        assert_eq!(SchemeType::Vietnamese.name(), "vietnamese");
1913        assert!(!SchemeType::Vietnamese.is_pinyin());
1914    }
1915
1916    #[test]
1917    fn tibetan_round_trips_through_code_eight() {
1918        assert_eq!(SchemeType::Tibetan as u8, 8);
1919        assert_eq!(SchemeType::from_u8(8), Some(SchemeType::Tibetan));
1920        assert_eq!(SchemeType::Tibetan.name(), "tibetan");
1921        assert!(!SchemeType::Tibetan.is_pinyin());
1922    }
1923
1924    #[test]
1925    fn cantonese_round_trips_through_code_five() {
1926        assert_eq!(SchemeType::Cantonese as u8, 5);
1927        assert_eq!(SchemeType::from_u8(5), Some(SchemeType::Cantonese));
1928        assert_eq!(SchemeType::Cantonese.name(), "cantonese");
1929        assert!(!SchemeType::Cantonese.is_pinyin());
1930    }
1931
1932    #[test]
1933    fn zhuyin_round_trips_through_code_six() {
1934        assert_eq!(SchemeType::Zhuyin as u8, 6);
1935        assert_eq!(SchemeType::from_u8(6), Some(SchemeType::Zhuyin));
1936        assert_eq!(SchemeType::Zhuyin.name(), "zhuyin");
1937        assert!(!SchemeType::Zhuyin.is_pinyin());
1938    }
1939
1940    #[test]
1941    fn stroke_round_trips_through_code_nine() {
1942        assert_eq!(SchemeType::Stroke as u8, 9);
1943        assert_eq!(SchemeType::from_u8(9), Some(SchemeType::Stroke));
1944        assert_eq!(SchemeType::Stroke.name(), "stroke");
1945        assert!(!SchemeType::Stroke.is_pinyin());
1946        assert_eq!(SchemeType::from_u8(10), None);
1947    }
1948
1949    // 笔画逐项照抄粤拼的取值,只有两处不同:笔画没有音节,所以不接受 `'` 分隔;预编辑画的是笔画字形,所以和注音一样由 `reading` 带给宿主。
1950    #[test]
1951    fn stroke_predicates() {
1952        let scheme = SchemeType::Stroke;
1953        let on: [Named; 6] = [
1954            ("is_chinese", SchemeType::is_chinese),
1955            (
1956                "outputs_traditional_natively",
1957                SchemeType::outputs_traditional_natively,
1958            ),
1959            (
1960                "uses_chinese_punctuation",
1961                SchemeType::uses_chinese_punctuation,
1962            ),
1963            ("host_smart_punctuation", SchemeType::host_smart_punctuation),
1964            ("widens_full_width", SchemeType::widens_full_width),
1965            ("draws_reading", SchemeType::draws_reading),
1966        ];
1967        let off: [Named; 20] = [
1968            (
1969                "cancel_keeps_composition",
1970                SchemeType::cancel_keeps_composition,
1971            ),
1972            (
1973                "script_conversion_applies",
1974                SchemeType::script_conversion_applies,
1975            ),
1976            (
1977                "allows_english_emoji_mixing",
1978                SchemeType::allows_english_emoji_mixing,
1979            ),
1980            ("opens_local_modes", SchemeType::opens_local_modes),
1981            (
1982                "learns_into_main_dictionary",
1983                SchemeType::learns_into_main_dictionary,
1984            ),
1985            ("learns_english_words", SchemeType::learns_english_words),
1986            ("cloud_eligible", SchemeType::cloud_eligible),
1987            ("shows_glosses", SchemeType::shows_glosses),
1988            ("commits_on_blur", SchemeType::commits_on_blur),
1989            ("holds_phrase_progress", SchemeType::holds_phrase_progress),
1990            (
1991                "reranks_with_sentence_model",
1992                SchemeType::reranks_with_sentence_model,
1993            ),
1994            (
1995                "has_openable_candidate_list",
1996                SchemeType::has_openable_candidate_list,
1997            ),
1998            ("selection_completes", SchemeType::selection_completes),
1999            (
2000                "follows_pinyin_candidates",
2001                SchemeType::follows_pinyin_candidates,
2002            ),
2003            ("accepts_apostrophe", SchemeType::accepts_apostrophe),
2004            ("locks_caret", SchemeType::locks_caret),
2005            ("supports_fuzzy", SchemeType::supports_fuzzy),
2006            ("supports_autocorrect", SchemeType::supports_autocorrect),
2007            ("nine_key", SchemeType::nine_key),
2008            ("helpcode", SchemeType::helpcode),
2009        ];
2010        for (name, predicate) in on {
2011            assert!(predicate(scheme), "{name}");
2012        }
2013        for (name, predicate) in off {
2014            assert!(!predicate(scheme), "{name}");
2015        }
2016        // 除上面两处外,每个谓词都与粤拼相同。
2017        for (name, predicate) in on.into_iter().chain(off) {
2018            if name == "accepts_apostrophe" || name == "draws_reading" {
2019                assert_ne!(
2020                    predicate(scheme),
2021                    predicate(SchemeType::Cantonese),
2022                    "{name}"
2023                );
2024            } else {
2025                assert_eq!(
2026                    predicate(scheme),
2027                    predicate(SchemeType::Cantonese),
2028                    "{name}"
2029                );
2030            }
2031        }
2032    }
2033
2034    // Zhuyin is a Chinese scheme writing Traditional text, with only its Shift overlay as Chinese punctuation; its converted text is the reading the host draws, the caret stays at the end, candidates appear only in the list the user opens, the conversion commits on blur, and it learns nothing. 注音九键也拼它的音节,所以 `nine_key` 为真。
2035    #[test]
2036    fn zhuyin_predicates() {
2037        let scheme = SchemeType::Zhuyin;
2038        let on: [Named; 10] = [
2039            ("is_chinese", SchemeType::is_chinese),
2040            (
2041                "cancel_keeps_composition",
2042                SchemeType::cancel_keeps_composition,
2043            ),
2044            (
2045                "outputs_traditional_natively",
2046                SchemeType::outputs_traditional_natively,
2047            ),
2048            (
2049                "uses_chinese_punctuation",
2050                SchemeType::uses_chinese_punctuation,
2051            ),
2052            ("widens_full_width", SchemeType::widens_full_width),
2053            ("commits_on_blur", SchemeType::commits_on_blur),
2054            ("draws_reading", SchemeType::draws_reading),
2055            (
2056                "has_openable_candidate_list",
2057                SchemeType::has_openable_candidate_list,
2058            ),
2059            ("locks_caret", SchemeType::locks_caret),
2060            ("nine_key", SchemeType::nine_key),
2061        ];
2062        let off: [Named; 16] = [
2063            (
2064                "script_conversion_applies",
2065                SchemeType::script_conversion_applies,
2066            ),
2067            ("host_smart_punctuation", SchemeType::host_smart_punctuation),
2068            (
2069                "allows_english_emoji_mixing",
2070                SchemeType::allows_english_emoji_mixing,
2071            ),
2072            ("opens_local_modes", SchemeType::opens_local_modes),
2073            (
2074                "learns_into_main_dictionary",
2075                SchemeType::learns_into_main_dictionary,
2076            ),
2077            ("learns_english_words", SchemeType::learns_english_words),
2078            ("cloud_eligible", SchemeType::cloud_eligible),
2079            ("shows_glosses", SchemeType::shows_glosses),
2080            ("holds_phrase_progress", SchemeType::holds_phrase_progress),
2081            (
2082                "reranks_with_sentence_model",
2083                SchemeType::reranks_with_sentence_model,
2084            ),
2085            ("selection_completes", SchemeType::selection_completes),
2086            (
2087                "follows_pinyin_candidates",
2088                SchemeType::follows_pinyin_candidates,
2089            ),
2090            ("accepts_apostrophe", SchemeType::accepts_apostrophe),
2091            ("supports_fuzzy", SchemeType::supports_fuzzy),
2092            ("supports_autocorrect", SchemeType::supports_autocorrect),
2093            ("helpcode", SchemeType::helpcode),
2094        ];
2095        for (name, predicate) in on {
2096            assert!(predicate(scheme), "{name}");
2097        }
2098        for (name, predicate) in off {
2099            assert!(!predicate(scheme), "{name}");
2100        }
2101    }
2102
2103    // Cantonese is a Chinese scheme writing Traditional text as stored, with Chinese punctuation and `'` boundaries; it learns nothing, holds no phrase progress and has none of the pinyin machinery.
2104    #[test]
2105    fn cantonese_predicates() {
2106        let scheme = SchemeType::Cantonese;
2107        let on: [Named; 6] = [
2108            ("is_chinese", SchemeType::is_chinese),
2109            (
2110                "outputs_traditional_natively",
2111                SchemeType::outputs_traditional_natively,
2112            ),
2113            (
2114                "uses_chinese_punctuation",
2115                SchemeType::uses_chinese_punctuation,
2116            ),
2117            ("host_smart_punctuation", SchemeType::host_smart_punctuation),
2118            ("widens_full_width", SchemeType::widens_full_width),
2119            ("accepts_apostrophe", SchemeType::accepts_apostrophe),
2120        ];
2121        let off: [Named; 20] = [
2122            (
2123                "cancel_keeps_composition",
2124                SchemeType::cancel_keeps_composition,
2125            ),
2126            (
2127                "script_conversion_applies",
2128                SchemeType::script_conversion_applies,
2129            ),
2130            (
2131                "allows_english_emoji_mixing",
2132                SchemeType::allows_english_emoji_mixing,
2133            ),
2134            ("opens_local_modes", SchemeType::opens_local_modes),
2135            (
2136                "learns_into_main_dictionary",
2137                SchemeType::learns_into_main_dictionary,
2138            ),
2139            ("learns_english_words", SchemeType::learns_english_words),
2140            ("cloud_eligible", SchemeType::cloud_eligible),
2141            ("shows_glosses", SchemeType::shows_glosses),
2142            ("commits_on_blur", SchemeType::commits_on_blur),
2143            ("holds_phrase_progress", SchemeType::holds_phrase_progress),
2144            (
2145                "reranks_with_sentence_model",
2146                SchemeType::reranks_with_sentence_model,
2147            ),
2148            ("draws_reading", SchemeType::draws_reading),
2149            (
2150                "has_openable_candidate_list",
2151                SchemeType::has_openable_candidate_list,
2152            ),
2153            ("selection_completes", SchemeType::selection_completes),
2154            (
2155                "follows_pinyin_candidates",
2156                SchemeType::follows_pinyin_candidates,
2157            ),
2158            ("locks_caret", SchemeType::locks_caret),
2159            ("supports_fuzzy", SchemeType::supports_fuzzy),
2160            ("supports_autocorrect", SchemeType::supports_autocorrect),
2161            ("nine_key", SchemeType::nine_key),
2162            ("helpcode", SchemeType::helpcode),
2163        ];
2164        for (name, predicate) in on {
2165            assert!(predicate(scheme), "{name}");
2166        }
2167        for (name, predicate) in off {
2168            assert!(!predicate(scheme), "{name}");
2169        }
2170    }
2171
2172    // 越南文和藏文都在组字里拼出一个词(音节串),没有候选也不学习:失焦时上屏,光标停在末尾,第一次 Esc 显示原文,中文相关的特性全部关闭。
2173    #[test]
2174    fn vietnamese_and_tibetan_predicates() {
2175        let on: [Named; 4] = [
2176            (
2177                "cancel_keeps_composition",
2178                SchemeType::cancel_keeps_composition,
2179            ),
2180            ("commits_on_blur", SchemeType::commits_on_blur),
2181            ("locks_caret", SchemeType::locks_caret),
2182            ("selection_completes", SchemeType::selection_completes),
2183        ];
2184        let off: [Named; 22] = [
2185            ("is_chinese", SchemeType::is_chinese),
2186            (
2187                "outputs_traditional_natively",
2188                SchemeType::outputs_traditional_natively,
2189            ),
2190            (
2191                "script_conversion_applies",
2192                SchemeType::script_conversion_applies,
2193            ),
2194            (
2195                "uses_chinese_punctuation",
2196                SchemeType::uses_chinese_punctuation,
2197            ),
2198            ("host_smart_punctuation", SchemeType::host_smart_punctuation),
2199            ("widens_full_width", SchemeType::widens_full_width),
2200            (
2201                "allows_english_emoji_mixing",
2202                SchemeType::allows_english_emoji_mixing,
2203            ),
2204            ("opens_local_modes", SchemeType::opens_local_modes),
2205            (
2206                "learns_into_main_dictionary",
2207                SchemeType::learns_into_main_dictionary,
2208            ),
2209            ("learns_english_words", SchemeType::learns_english_words),
2210            ("cloud_eligible", SchemeType::cloud_eligible),
2211            ("shows_glosses", SchemeType::shows_glosses),
2212            ("holds_phrase_progress", SchemeType::holds_phrase_progress),
2213            (
2214                "reranks_with_sentence_model",
2215                SchemeType::reranks_with_sentence_model,
2216            ),
2217            ("draws_reading", SchemeType::draws_reading),
2218            (
2219                "has_openable_candidate_list",
2220                SchemeType::has_openable_candidate_list,
2221            ),
2222            (
2223                "follows_pinyin_candidates",
2224                SchemeType::follows_pinyin_candidates,
2225            ),
2226            ("accepts_apostrophe", SchemeType::accepts_apostrophe),
2227            ("supports_fuzzy", SchemeType::supports_fuzzy),
2228            ("supports_autocorrect", SchemeType::supports_autocorrect),
2229            ("nine_key", SchemeType::nine_key),
2230            ("helpcode", SchemeType::helpcode),
2231        ];
2232        for scheme in [SchemeType::Vietnamese, SchemeType::Tibetan] {
2233            for (name, predicate) in on {
2234                assert!(predicate(scheme), "{name} for {scheme:?}");
2235            }
2236            for (name, predicate) in off {
2237                assert!(!predicate(scheme), "{name} for {scheme:?}");
2238            }
2239        }
2240    }
2241
2242    /// `SchemeSet::ALL` 正好是 `from_u8` 认得的全部方案:加了方案却没改 `SchemeSet::LAST` 时,新方案会落在 `ALL` 之外,full 版的会话就切不到它。
2243    #[test]
2244    fn scheme_set_all_covers_every_scheme() {
2245        for value in 0..=u8::MAX {
2246            match SchemeType::from_u8(value) {
2247                Some(scheme) => assert!(
2248                    SchemeSet::ALL.contains(scheme),
2249                    "{scheme:?} is outside SchemeSet::ALL"
2250                ),
2251                None => assert!(
2252                    u32::from(value) >= u16::BITS || SchemeSet::ALL.0 & (1 << value) == 0,
2253                    "SchemeSet::ALL sets bit {value}, which names no scheme"
2254                ),
2255            }
2256        }
2257    }
2258
2259    #[test]
2260    fn only_pinyin_and_wubi_read_the_main_dictionary() {
2261        assert!(SchemeSet::ALL.reads_main_dictionary());
2262        assert!(!SchemeSet::EMPTY.reads_main_dictionary());
2263        for value in 0..=u8::MAX {
2264            let Some(scheme) = SchemeType::from_u8(value) else {
2265                continue;
2266            };
2267            let reads = matches!(
2268                scheme,
2269                SchemeType::Quanpin | SchemeType::Shuangpin | SchemeType::Wubi
2270            );
2271            assert_eq!(
2272                SchemeSet::of(&[scheme]).reads_main_dictionary(),
2273                reads,
2274                "{scheme:?}"
2275            );
2276        }
2277        assert!(!SchemeSet::of(&[
2278            SchemeType::JapaneseRomaji,
2279            SchemeType::Vietnamese,
2280            SchemeType::Tibetan,
2281        ])
2282        .reads_main_dictionary());
2283    }
2284
2285    /// 只有全拼、双拼、五笔离不开 `msime-english.db`;读主库的集合一定也要求它,准备代次时据此先查英文库再查主库。
2286    #[test]
2287    fn only_pinyin_and_wubi_require_the_english_dictionary() {
2288        assert!(SchemeSet::ALL.requires_english_dictionary());
2289        assert!(!SchemeSet::EMPTY.requires_english_dictionary());
2290        for value in 0..=u8::MAX {
2291            let Some(scheme) = SchemeType::from_u8(value) else {
2292                continue;
2293            };
2294            let set = SchemeSet::of(&[scheme]);
2295            let requires = matches!(
2296                scheme,
2297                SchemeType::Quanpin | SchemeType::Shuangpin | SchemeType::Wubi
2298            );
2299            assert_eq!(set.requires_english_dictionary(), requires, "{scheme:?}");
2300            assert!(!set.reads_main_dictionary() || set.requires_english_dictionary());
2301        }
2302        assert!(!SchemeSet::of(&[
2303            SchemeType::JapaneseRomaji,
2304            SchemeType::Korean,
2305            SchemeType::Vietnamese,
2306            SchemeType::Tibetan,
2307            SchemeType::Cantonese,
2308            SchemeType::Zhuyin,
2309            SchemeType::Stroke,
2310        ])
2311        .requires_english_dictionary());
2312    }
2313
2314    #[test]
2315    fn lattice_static_evidence_removes_syllable_bonus_and_counts_unicode_characters() {
2316        let mut item = WordItem::new("xian", "西安", 36000, CandidateSource::Generated, "xi'an");
2317        assert_eq!(item.static_evidence(), None);
2318        item.sentence_association = true;
2319        let expected = Some(crate::ordering::StaticEvidence {
2320            log_prob_milli_nats: -4000,
2321            characters: 2,
2322        });
2323        assert_eq!(item.static_evidence(), expected);
2324        item.source = CandidateSource::NeuralKeyboard;
2325        assert_eq!(item.static_evidence(), expected);
2326        item.source = CandidateSource::Database;
2327        assert_eq!(item.static_evidence(), None);
2328        item.source = CandidateSource::Generated;
2329        item.canonical_pinyin.clear();
2330        assert_eq!(item.static_evidence(), None);
2331        item.canonical_pinyin = "xi'an".into();
2332        item.word.clear();
2333        assert_eq!(item.static_evidence(), None);
2334    }
2335}