Skip to main content

zellij_utils/ipc/
enum_conversions.rs

1use crate::{
2    client_server_contract::client_server_contract::{
3        BareKey as ProtoBareKey, KeyModifier as ProtoKeyModifier,
4    },
5    data::{BareKey, KeyModifier},
6    errors::prelude::*,
7};
8
9// BareKey conversions
10impl From<BareKey> for ProtoBareKey {
11    fn from(key: BareKey) -> Self {
12        match key {
13            BareKey::PageDown => ProtoBareKey::PageDown,
14            BareKey::PageUp => ProtoBareKey::PageUp,
15            BareKey::Left => ProtoBareKey::Left,
16            BareKey::Down => ProtoBareKey::Down,
17            BareKey::Up => ProtoBareKey::Up,
18            BareKey::Right => ProtoBareKey::Right,
19            BareKey::Home => ProtoBareKey::Home,
20            BareKey::End => ProtoBareKey::End,
21            BareKey::Backspace => ProtoBareKey::Backspace,
22            BareKey::Delete => ProtoBareKey::Delete,
23            BareKey::Insert => ProtoBareKey::Insert,
24            BareKey::F(1) => ProtoBareKey::F1,
25            BareKey::F(2) => ProtoBareKey::F2,
26            BareKey::F(3) => ProtoBareKey::F3,
27            BareKey::F(4) => ProtoBareKey::F4,
28            BareKey::F(5) => ProtoBareKey::F5,
29            BareKey::F(6) => ProtoBareKey::F6,
30            BareKey::F(7) => ProtoBareKey::F7,
31            BareKey::F(8) => ProtoBareKey::F8,
32            BareKey::F(9) => ProtoBareKey::F9,
33            BareKey::F(10) => ProtoBareKey::F10,
34            BareKey::F(11) => ProtoBareKey::F11,
35            BareKey::F(12) => ProtoBareKey::F12,
36            BareKey::F(_) => ProtoBareKey::Unspecified, // Unsupported F-key
37            BareKey::Char(_) => ProtoBareKey::Char,     // Character stored separately
38            BareKey::Tab => ProtoBareKey::Tab,
39            BareKey::Esc => ProtoBareKey::Esc,
40            BareKey::Enter => ProtoBareKey::Enter,
41            BareKey::CapsLock => ProtoBareKey::CapsLock,
42            BareKey::ScrollLock => ProtoBareKey::ScrollLock,
43            BareKey::NumLock => ProtoBareKey::NumLock,
44            BareKey::PrintScreen => ProtoBareKey::PrintScreen,
45            BareKey::Pause => ProtoBareKey::Pause,
46            BareKey::Menu => ProtoBareKey::Menu,
47        }
48    }
49}
50
51impl TryFrom<ProtoBareKey> for BareKey {
52    type Error = anyhow::Error;
53
54    fn try_from(key: ProtoBareKey) -> Result<Self> {
55        match key {
56            ProtoBareKey::PageDown => Ok(BareKey::PageDown),
57            ProtoBareKey::PageUp => Ok(BareKey::PageUp),
58            ProtoBareKey::Left => Ok(BareKey::Left),
59            ProtoBareKey::Down => Ok(BareKey::Down),
60            ProtoBareKey::Up => Ok(BareKey::Up),
61            ProtoBareKey::Right => Ok(BareKey::Right),
62            ProtoBareKey::Home => Ok(BareKey::Home),
63            ProtoBareKey::End => Ok(BareKey::End),
64            ProtoBareKey::Backspace => Ok(BareKey::Backspace),
65            ProtoBareKey::Delete => Ok(BareKey::Delete),
66            ProtoBareKey::Insert => Ok(BareKey::Insert),
67            ProtoBareKey::F1 => Ok(BareKey::F(1)),
68            ProtoBareKey::F2 => Ok(BareKey::F(2)),
69            ProtoBareKey::F3 => Ok(BareKey::F(3)),
70            ProtoBareKey::F4 => Ok(BareKey::F(4)),
71            ProtoBareKey::F5 => Ok(BareKey::F(5)),
72            ProtoBareKey::F6 => Ok(BareKey::F(6)),
73            ProtoBareKey::F7 => Ok(BareKey::F(7)),
74            ProtoBareKey::F8 => Ok(BareKey::F(8)),
75            ProtoBareKey::F9 => Ok(BareKey::F(9)),
76            ProtoBareKey::F10 => Ok(BareKey::F(10)),
77            ProtoBareKey::F11 => Ok(BareKey::F(11)),
78            ProtoBareKey::F12 => Ok(BareKey::F(12)),
79            ProtoBareKey::Char => Err(anyhow!("Character key needs character data")),
80            ProtoBareKey::Tab => Ok(BareKey::Tab),
81            ProtoBareKey::Esc => Ok(BareKey::Esc),
82            ProtoBareKey::Enter => Ok(BareKey::Enter),
83            ProtoBareKey::CapsLock => Ok(BareKey::CapsLock),
84            ProtoBareKey::ScrollLock => Ok(BareKey::ScrollLock),
85            ProtoBareKey::NumLock => Ok(BareKey::NumLock),
86            ProtoBareKey::PrintScreen => Ok(BareKey::PrintScreen),
87            ProtoBareKey::Pause => Ok(BareKey::Pause),
88            ProtoBareKey::Menu => Ok(BareKey::Menu),
89            ProtoBareKey::Unspecified => Err(anyhow!("Unspecified bare key")),
90        }
91    }
92}
93
94// KeyModifier conversions
95impl From<KeyModifier> for ProtoKeyModifier {
96    fn from(modifier: KeyModifier) -> Self {
97        match modifier {
98            KeyModifier::Ctrl => ProtoKeyModifier::Ctrl,
99            KeyModifier::Alt => ProtoKeyModifier::Alt,
100            KeyModifier::Shift => ProtoKeyModifier::Shift,
101            KeyModifier::Super => ProtoKeyModifier::Super,
102        }
103    }
104}
105
106impl TryFrom<ProtoKeyModifier> for KeyModifier {
107    type Error = anyhow::Error;
108
109    fn try_from(modifier: ProtoKeyModifier) -> Result<Self> {
110        match modifier {
111            ProtoKeyModifier::Ctrl => Ok(KeyModifier::Ctrl),
112            ProtoKeyModifier::Alt => Ok(KeyModifier::Alt),
113            ProtoKeyModifier::Shift => Ok(KeyModifier::Shift),
114            ProtoKeyModifier::Super => Ok(KeyModifier::Super),
115            ProtoKeyModifier::Unspecified => Err(anyhow!("Unspecified key modifier")),
116        }
117    }
118}
119
120// Helper functions for converting between protobuf i32 and enum types
121pub fn bare_key_to_proto_i32(key: BareKey) -> i32 {
122    ProtoBareKey::from(key) as i32
123}
124
125pub fn bare_key_from_proto_i32(value: i32) -> Result<BareKey> {
126    let proto_key =
127        ProtoBareKey::from_i32(value).ok_or_else(|| anyhow!("Invalid BareKey value: {}", value))?;
128    proto_key.try_into()
129}
130
131pub fn key_modifier_to_proto_i32(modifier: KeyModifier) -> i32 {
132    ProtoKeyModifier::from(modifier) as i32
133}
134
135pub fn key_modifier_from_proto_i32(value: i32) -> Result<KeyModifier> {
136    let proto_modifier = ProtoKeyModifier::from_i32(value)
137        .ok_or_else(|| anyhow!("Invalid KeyModifier value: {}", value))?;
138    proto_modifier.try_into()
139}