vte/
definitions.rs

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