mm_client_common/
input.rs

1// Copyright 2024 Colin Marc <hi@colinmarc.com>
2//
3// SPDX-License-Identifier: MIT
4
5use mm_protocol as protocol;
6
7pub use protocol::gamepad::GamepadLayout;
8pub use protocol::gamepad_input::{GamepadButton, GamepadButtonState};
9pub use protocol::gamepad_motion::GamepadAxis;
10pub use protocol::keyboard_input::{Key, KeyState};
11pub use protocol::pointer_input::{Button, ButtonState};
12pub use protocol::pointer_scroll::ScrollType;
13pub use protocol::update_cursor::CursorIcon;
14
15use crate::validation::ValidationError;
16
17#[derive(Debug, Clone, Copy, uniffi::Record)]
18pub struct Gamepad {
19    pub id: u64,
20    pub layout: GamepadLayout,
21}
22
23impl From<Gamepad> for protocol::Gamepad {
24    fn from(value: Gamepad) -> Self {
25        Self {
26            id: value.id,
27            layout: value.layout.into(),
28        }
29    }
30}
31
32impl TryFrom<protocol::Gamepad> for Gamepad {
33    type Error = ValidationError;
34
35    fn try_from(value: protocol::Gamepad) -> Result<Self, Self::Error> {
36        let layout = value
37            .layout
38            .try_into()
39            .map_err(|_| ValidationError::InvalidEnum("layout".to_string()))?;
40
41        if value.id == 0 {
42            return Err(ValidationError::Required("id".to_string()));
43        }
44
45        Ok(Self {
46            id: value.id,
47            layout,
48        })
49    }
50}