1 #[allow(dead_code)]
2 #[derive(Debug, Copy, Clone)]
3 pub enum State {
4    Anywhere = 0,
5    CsiEntry = 1,
6    CsiIgnore = 2,
7    CsiIntermediate = 3,
8    CsiParam = 4,
9    DcsEntry = 5,
10    DcsIgnore = 6,
11    DcsIntermediate = 7,
12    DcsParam = 8,
13    DcsPassthrough = 9,
14    Escape = 10,
15    EscapeIntermediate = 11,
16    Ground = 12,
17    OscString = 13,
18    SosPmApcString = 14,
19    Utf8 = 15,
20 }
21 
22 #[allow(dead_code)]
23 #[derive(Debug, Clone, Copy)]
24 pub enum Action {
25    None = 0,
26    Clear = 1,
27    Collect = 2,
28    CsiDispatch = 3,
29    EscDispatch = 4,
30    Execute = 5,
31    Hook = 6,
32    Ignore = 7,
33    OscEnd = 8,
34    OscPut = 9,
35    OscStart = 10,
36    Param = 11,
37    Print = 12,
38    Put = 13,
39    Unhook = 14,
40    BeginUtf8 = 15,
41 }
42 
43 /// Unpack a u8 into a State and Action
44 ///
45 /// The implementation of this assumes that there are *precisely* 16 variants for both Action and
46 /// State. Furthermore, it assumes that the enums are tag-only; that is, there is no data in any
47 /// variant.
48 ///
49 /// Bad things will happen if those invariants are violated.
50 #[inline(always)]
unpack(delta: u8) -> (State, Action)51 pub fn unpack(delta: u8) -> (State, Action) {
52     (
53         // State is stored in bottom 4 bits
54         unsafe { ::core::mem::transmute(delta & 0x0f) },
55 
56         // Action is stored in top 4 bits
57         unsafe { ::core::mem::transmute(delta >> 4) },
58     )
59 }
60 
61 #[cfg(test)]
62 mod tests {
63     use super::{State, Action, unpack};
64     #[test]
unpack_state_action()65     fn unpack_state_action() {
66         match unpack(0xee) {
67             (State::SosPmApcString, Action::Unhook) => (),
68             _ => panic!("unpack failed"),
69         }
70 
71         match unpack(0x0f) {
72             (State::Utf8, Action::None) => (),
73             _ => panic!("unpack failed"),
74         }
75 
76         match unpack(0xff) {
77             (State::Utf8, Action::BeginUtf8) => (),
78             _ => panic!("unpack failed"),
79         }
80     }
81 }
82