Buttplug sex toy control library
1// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2//
3// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4//
5// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6// for full license information.
7
8use crate::message::TryFromDeviceAttributes;
9use buttplug_core::{
10 errors::{ButtplugDeviceError, ButtplugError, ButtplugMessageError},
11 message::{
12 ButtplugDeviceMessage,
13 ButtplugMessage,
14 ButtplugMessageFinalizer,
15 ButtplugMessageValidator,
16 InputCmdV4,
17 InputCommandType,
18 InputType,
19 },
20};
21use getset::CopyGetters;
22use uuid::Uuid;
23
24#[derive(
25 Debug, ButtplugDeviceMessage, ButtplugMessageFinalizer, PartialEq, Eq, Clone, CopyGetters,
26)]
27#[getset(get_copy = "pub")]
28pub struct CheckedInputCmdV4 {
29 id: u32,
30 device_index: u32,
31 feature_index: u32,
32 input_type: InputType,
33 input_command: InputCommandType,
34 feature_id: Uuid,
35}
36
37impl CheckedInputCmdV4 {
38 pub fn new(
39 device_index: u32,
40 feature_index: u32,
41 input_type: InputType,
42 input_command: InputCommandType,
43 feature_id: Uuid,
44 ) -> Self {
45 Self {
46 id: 1,
47 device_index,
48 feature_index,
49 input_type,
50 input_command,
51 feature_id,
52 }
53 }
54}
55
56impl ButtplugMessageValidator for CheckedInputCmdV4 {
57 fn is_valid(&self) -> Result<(), ButtplugMessageError> {
58 self.is_not_system_id(self.id)
59 // TODO Should expected_length always be > 0?
60 }
61}
62
63impl TryFromDeviceAttributes<InputCmdV4> for CheckedInputCmdV4 {
64 fn try_from_device_attributes(
65 msg: InputCmdV4,
66 features: &crate::message::ServerDeviceAttributes,
67 ) -> Result<Self, buttplug_core::errors::ButtplugError> {
68 if let Some(feature) = features.features().get(msg.feature_index() as usize) {
69 if let Some(sensor_map) = feature.input() {
70 if sensor_map.contains(msg.input_type()) {
71 Ok(CheckedInputCmdV4::new(
72 msg.device_index(),
73 msg.feature_index(),
74 msg.input_type(),
75 msg.input_command(),
76 feature.id(),
77 ))
78 } else {
79 Err(ButtplugError::from(
80 ButtplugDeviceError::DeviceNoSensorError("InputCmd".to_string()),
81 ))
82 }
83 } else {
84 Err(ButtplugError::from(
85 ButtplugDeviceError::DeviceNoSensorError("InputCmd".to_string()),
86 ))
87 }
88 } else {
89 Err(ButtplugError::from(
90 ButtplugDeviceError::DeviceFeatureIndexError(
91 features.features().len() as u32,
92 msg.feature_index(),
93 ),
94 ))
95 }
96 }
97}