-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_machine_pseudo.rs
108 lines (98 loc) · 2.42 KB
/
state_machine_pseudo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#[derive(Debug)]
enum State {
Typing,
PreeditCommitting,
Interrupting,
Backspacing,
}
use State::*;
#[derive(Debug)]
struct Buffer {
buffer: Vec<char>,
ouput: String,
}
fn process_inner(state: &mut State, buf: &mut Buffer, keyval: guint, key_state: guint) -> bool {
if is_released(key_state) {
return false;
}
match state {
Typing => {
if shift_shift(keyval) {
*state = Interrupting;
true
} else if is_work_seps(keyval) {
*state = PreeditCommitting;
false
} else if is_a_zA_Z(keyval) {
buf.buffer.push(keyval);
translate(buf.buffer, &mut buf.output);
true
} else if is_transparent(keyval) {
false
} else if is_backspace {
state = Backspacing;
true
} else {
*state = PreeditCommitting;
false
}
}
Interrupting => {
if shift_shift(keyval) {
*state = Typing;
true
} else {
false
}
}
_ => false,
}
}
fn process(state: &mut State, buf: &mut Buffer, keyval: guint, key_state: guint) -> bool {
let processed = process_inner(state, buf, keyval, key_state);
match state {
Typing => update_preedit(),
PreeditCommitting => {
commit_preedit();
buf.buffer.clear();
buf.output.clear();
*state = Typing;
}
Interrupting => commit_preedit(),
Backspacing => {
*state = Typing;
return if bs_success(output) {
update_preedit();
true
} else {
false
};
}
}
processed
}
fn push_translate(buf: &mut Buffer) {
unimplemented!()
}
fn translate(buffer: &[char], output: &mut String) {
unimplemented!()
}
fn main() {
let state = State::Typing;
let buf = Buffer {
buffer: Vec::new(),
output: String::new(),
};
for (keyval, key_state) in receive_keyval() {
process(&mut state, &mut buf, keyval, key_state);
}
}
fn receive_keyval() -> impl Iterator<Item = ((keyval, key_state))> {
unimplemented!()
}
fn commit_preedit() {
unimplemented!()
}
fn update_preedit() {
unimplemented!()
}