Skip to main content

chai/
config.rs

1//! 配置文件的定义
2//!
3//! 这部分内容太多,就不一一注释了。后期会写一个「`config.yaml` 详解」来统一解释各种配置文件的字段。
4//!
5
6use crate::optimizers::simulated_annealing::退火方法;
7use indexmap::IndexMap;
8use serde::{Deserialize, Serialize};
9use serde_with::skip_serializing_none;
10
11// config.info begin
12#[skip_serializing_none]
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct 基本信息 {
15    pub name: Option<String>,
16    pub version: Option<String>,
17    pub author: Option<String>,
18    pub description: Option<String>,
19}
20// config.info end
21
22// config.data begin
23#[skip_serializing_none]
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct 数据配置 {
26    pub repertoire: Option<原始字库>,
27    pub glyph_customization: Option<IndexMap<String, 字形>>,
28    pub tags: Option<Vec<String>>,
29    pub transformers: Option<Vec<变换器>>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct 变换器 {
34    pub from: 模式,
35    pub to: 模式,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[allow(non_snake_case)]
40pub struct 模式 {
41    operator: char,
42    operandList: Vec<节点>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(untagged)]
47pub enum 节点 {
48    Pattern(模式),
49    Variable { id: usize },
50    Character(char),
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(tag = "command", rename_all = "snake_case")]
55#[allow(non_snake_case)]
56pub enum 绘制 {
57    H { parameterList: [i16; 1] },
58    V { parameterList: [i16; 1] },
59    C { parameterList: [i16; 6] },
60    Z { parameterList: [i16; 6] },
61    A { parameterList: [i16; 1] },
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65#[serde(untagged)]
66#[allow(non_snake_case)]
67pub enum 笔画 {
68    矢量笔画 {
69        feature: String,
70        start: (i16, i16),
71        curveList: Vec<绘制>,
72    },
73    引用笔画 {
74        feature: String,
75        index: usize,
76    },
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct 笔画块 {
81    pub index: usize,
82    pub strokes: usize,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct 复合体参数 {
87    pub gap2: Option<f64>,
88    pub scale2: Option<f64>,
89    pub gap3: Option<f64>,
90    pub scale3: Option<f64>,
91}
92
93#[skip_serializing_none]
94#[derive(Debug, Clone, Serialize, Deserialize)]
95#[serde(tag = "type", rename_all = "snake_case")]
96#[allow(non_snake_case)]
97pub enum 字形 {
98    BasicComponent {
99        tags: Option<Vec<String>>,
100        strokes: Vec<笔画>,
101    },
102    DerivedComponent {
103        tags: Option<Vec<String>>,
104        source: String,
105        strokes: Vec<笔画>,
106    },
107    SplicedComponent {
108        tags: Option<Vec<String>>,
109        operator: String,
110        operandList: Vec<String>,
111        order: Option<Vec<笔画块>>,
112        parameters: Option<复合体参数>,
113    },
114    Compound {
115        tags: Option<Vec<String>>,
116        operator: String,
117        operandList: Vec<String>,
118        order: Option<Vec<笔画块>>,
119        parameters: Option<复合体参数>,
120    },
121}
122
123#[skip_serializing_none]
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub struct 原始汉字 {
126    pub unicode: usize,
127    pub tygf: u8,
128    pub gb2312: u8,
129    #[serialize_always] // JavaScript null
130    pub name: Option<String>,
131    #[serialize_always] // JavaScript null
132    pub gf0014_id: Option<usize>,
133    #[serialize_always] // JavaScript null
134    pub gf3001_id: Option<usize>,
135    pub glyphs: Vec<字形>,
136    pub ambiguous: bool,
137}
138
139pub type 原始字库 = IndexMap<String, 原始汉字>;
140// config.data end
141
142// config.analysis begin
143#[skip_serializing_none]
144#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct 分析配置 {
146    pub classifier: Option<IndexMap<String, usize>>,
147    pub degenerator: Option<退化配置>,
148    pub selector: Option<Vec<String>>,
149    pub customize: Option<IndexMap<String, Vec<String>>>,
150    pub dynamic_customize: Option<IndexMap<String, Vec<Vec<String>>>>,
151    pub strong: Option<Vec<String>>,
152    pub weak: Option<Vec<String>>,
153    pub component_analyzer: Option<String>,
154    pub compound_analyzer: Option<String>,
155}
156
157#[skip_serializing_none]
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct 退化配置 {
160    pub feature: Option<IndexMap<String, String>>,
161    pub no_cross: Option<bool>,
162}
163// config.analysis end
164
165// config.algebra begin
166type 拼写运算自定义 = IndexMap<String, Vec<运算规则>>;
167
168#[derive(Debug, Clone, Serialize, Deserialize)]
169#[serde(tag = "type", rename_all = "snake_case")]
170pub enum 运算规则 {
171    Xform { from: String, to: String },
172    Xlit { from: String, to: String },
173}
174// config.algebra end
175
176// config.form begin
177#[skip_serializing_none]
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct 键盘配置 {
180    pub alphabet: String,
181    pub mapping_type: Option<usize>,
182    pub mapping: IndexMap<String, 安排>,
183    pub mapping_space: Option<IndexMap<String, Vec<安排描述>>>,
184    pub mapping_variables: Option<IndexMap<String, 变量规则>>,
185    pub mapping_generators: Option<Vec<决策生成器规则>>,
186}
187
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct 变量规则 {
190    pub keys: Vec<char>,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct 决策生成器规则 {
195    pub regex: String,
196    pub value: 安排描述,
197}
198
199#[skip_serializing_none]
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct 安排描述 {
202    pub value: 安排,
203    pub score: f64,
204    pub condition: Option<Vec<条件>>,
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
208pub struct 条件 {
209    pub element: String,
210    pub op: String,
211    pub value: 安排,
212}
213
214#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
215#[serde(untagged)]
216pub enum 广义码位 {
217    Ascii(char),
218    Reference { element: String, index: usize },
219    Variable { variable: String },
220    Placeholder(()),
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
224#[serde(untagged)]
225pub enum 安排 {
226    Basic(String),
227    Advanced(Vec<广义码位>),
228    Grouped { element: String },
229    Unused(()),
230}
231// config.form end
232
233// config.encoder begin
234#[skip_serializing_none]
235#[derive(Debug, Clone, Serialize, Deserialize)]
236pub struct 编码配置 {
237    // 全局
238    pub max_length: usize,
239    pub select_keys: Option<Vec<char>>,
240    pub auto_select_length: Option<usize>,
241    pub auto_select_pattern: Option<String>,
242    // 一字词全码
243    pub sources: Option<IndexMap<String, 源节点配置>>,
244    pub conditions: Option<IndexMap<String, 条件节点配置>>,
245    // 多字词全码
246    pub rules: Option<Vec<构词规则>>,
247    // 简码
248    pub short_code: Option<Vec<简码规则>>,
249    pub short_code_list: Option<Vec<(String, String, usize)>>,
250    // 组装器
251    pub assembler: Option<String>,
252}
253
254pub struct 优先简码 {
255    pub word: String,
256    pub sources: Vec<Vec<String>>,
257    pub level: usize,
258}
259
260#[skip_serializing_none]
261#[derive(Debug, Clone, Serialize, Deserialize)]
262#[allow(non_snake_case)]
263pub struct 取码对象 {
264    pub r#type: String,
265    pub subtype: Option<String>,
266    pub key: Option<String>,
267    pub rootIndex: Option<i64>,
268    pub strokeIndex: Option<i64>,
269}
270
271#[skip_serializing_none]
272#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct 源节点配置 {
274    #[serialize_always] // JavaScript null
275    pub object: Option<取码对象>,
276    pub index: Option<usize>,
277    #[serialize_always] // JavaScript null
278    pub next: Option<String>,
279}
280
281#[skip_serializing_none]
282#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct 条件节点配置 {
284    pub object: 取码对象,
285    pub operator: String,
286    pub value: Option<String>,
287    #[serialize_always] // JavaScript null
288    pub positive: Option<String>,
289    #[serialize_always] // JavaScript null
290    pub negative: Option<String>,
291}
292
293#[derive(Debug, Clone, Serialize, Deserialize)]
294#[serde(untagged)]
295pub enum 简码规则 {
296    Equal {
297        length_equal: usize,
298        schemes: Vec<简码模式>,
299    },
300    Range {
301        length_in_range: (usize, usize),
302        schemes: Vec<简码模式>,
303    },
304}
305
306#[skip_serializing_none]
307#[derive(Debug, Clone, Serialize, Deserialize)]
308pub struct 简码模式 {
309    pub prefix: usize,
310    pub count: Option<usize>,
311    pub select_keys: Option<Vec<char>>,
312}
313
314#[derive(Debug, Clone, Serialize, Deserialize)]
315#[serde(untagged)]
316pub enum 构词规则 {
317    EqualRule {
318        length_equal: usize,
319        formula: String,
320    },
321    RangeRule {
322        length_in_range: (usize, usize),
323        formula: String,
324    },
325}
326// config.encoder end
327
328// config.optimization begin
329#[derive(Debug, Clone, Serialize, Deserialize)]
330pub struct 码长权重 {
331    pub length: usize,
332    pub frequency: f64,
333}
334
335#[skip_serializing_none]
336#[derive(Debug, Clone, Serialize, Deserialize)]
337pub struct 层级权重 {
338    pub top: Option<usize>,
339    pub duplication: Option<f64>,
340    pub levels: Option<Vec<码长权重>>,
341    pub fingering: Option<指法权重>,
342}
343
344// let types = ["同手", "大跨", "小跨", "干扰", "错手", "三连", "备用", "备用"];
345pub type 指法权重 = [Option<f64>; 8];
346
347#[skip_serializing_none]
348#[derive(Debug, Clone, Serialize, Deserialize)]
349pub struct 部分权重 {
350    pub tiers: Option<Vec<层级权重>>,
351    pub duplication: Option<f64>,
352    pub key_distribution: Option<f64>,
353    pub pair_equivalence: Option<f64>,
354    pub extended_pair_equivalence: Option<f64>,
355    pub fingering: Option<指法权重>,
356    pub levels: Option<Vec<码长权重>>,
357}
358
359#[skip_serializing_none]
360#[derive(Debug, Clone, Serialize, Deserialize)]
361pub struct 目标配置 {
362    pub characters_full: Option<部分权重>,
363    pub words_full: Option<部分权重>,
364    pub characters_short: Option<部分权重>,
365    pub words_short: Option<部分权重>,
366    pub regularization_strength: Option<f64>,
367}
368
369#[skip_serializing_none]
370#[derive(Debug, Clone, Serialize, Deserialize)]
371#[serde(tag = "algorithm")]
372pub enum 求解器配置 {
373    SimulatedAnnealing(退火方法),
374    // TODO: Add more algorithms
375}
376
377#[skip_serializing_none]
378#[derive(Debug, Clone, Serialize, Deserialize)]
379pub struct 优化配置 {
380    pub objective: 目标配置,
381    pub metaheuristic: Option<求解器配置>,
382}
383// config.optimization end
384
385// config.diagram begin
386
387#[skip_serializing_none]
388#[derive(Debug, Clone, Serialize, Deserialize)]
389pub struct LayoutRow {
390    pub keys: Vec<char>,
391}
392
393#[skip_serializing_none]
394#[derive(Debug, Clone, Serialize, Deserialize)]
395#[serde(tag = "type", rename_all = "snake_case")]
396pub enum 区块配置 {
397    Key {
398        style: Option<String>,
399    },
400    Uppercase {
401        style: Option<String>,
402    },
403    Element {
404        r#match: Option<String>,
405        style: Option<String>,
406    },
407    Custom {
408        mapping: Option<String>,
409        style: Option<String>,
410    },
411}
412
413#[skip_serializing_none]
414#[derive(Debug, Clone, Serialize, Deserialize)]
415pub struct 图示配置 {
416    pub layout: Vec<LayoutRow>,
417    pub contents: Vec<区块配置>,
418    pub row_style: Option<String>,
419    pub cell_style: Option<String>,
420}
421
422// config.diagram end
423
424#[skip_serializing_none]
425#[derive(Debug, Clone, Serialize, Deserialize)]
426pub struct 配置 {
427    pub version: Option<String>,
428    #[serialize_always] // JavaScript null
429    pub source: Option<String>,
430    pub info: Option<基本信息>,
431    pub data: Option<数据配置>,
432    pub analysis: Option<分析配置>,
433    pub algebra: Option<拼写运算自定义>,
434    pub form: 键盘配置,
435    pub encoder: 编码配置,
436    pub optimization: Option<优化配置>,
437    pub diagram: Option<图示配置>,
438}
439
440impl Default for 配置 {
441    fn default() -> Self {
442        配置 {
443            version: None,
444            source: None,
445            info: None,
446            data: None,
447            analysis: None,
448            algebra: None,
449            form: 键盘配置 {
450                alphabet: "abcdefghijklmnopqrstuvwxyz".to_string(),
451                mapping_type: None,
452                mapping: IndexMap::new(),
453                mapping_space: None,
454                mapping_variables: None,
455                mapping_generators: None,
456            },
457            encoder: 编码配置 {
458                max_length: 1,
459                select_keys: None,
460                auto_select_length: None,
461                auto_select_pattern: None,
462                sources: None,
463                conditions: None,
464                rules: None,
465                short_code: None,
466                short_code_list: None,
467                assembler: None,
468            },
469            optimization: None,
470            diagram: None,
471        }
472    }
473}