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