Skip to content

Commit 8d96cc5

Browse files
committed
feat (client): Client commands should use embedded values
Just embed int/float in the value enum and have users use that.
1 parent f5c9810 commit 8d96cc5

File tree

6 files changed

+159
-225
lines changed

6 files changed

+159
-225
lines changed

crates/buttplug_client/src/device/command.rs

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use buttplug_core::message::OutputType;
22

33
use crate::ButtplugClientError;
44

5+
#[derive(Debug, Clone, Copy)]
56
pub enum ClientDeviceCommandValue {
67
Int(i32),
78
Float(f64),
@@ -27,41 +28,31 @@ impl From<f64> for ClientDeviceCommandValue {
2728

2829
pub enum ClientDeviceOutputCommand {
2930
// u32 types use steps, need to compare before sending
30-
Vibrate(u32),
31-
Rotate(i32),
32-
Oscillate(u32),
33-
Constrict(u32),
34-
Temperature(i32),
35-
Led(u32),
36-
Spray(u32),
37-
Position(u32),
38-
PositionWithDuration(u32, u32),
39-
// f64 types are old style float, will need to convert before sending
40-
VibrateFloat(f64),
41-
RotateFloat(f64),
42-
OscillateFloat(f64),
43-
ConstrictFloat(f64),
44-
TemperatureFloat(f64),
45-
LedFloat(f64),
46-
SprayFloat(f64),
47-
PositionFloat(f64),
48-
PositionWithDurationFloat(f64, u32),
31+
Vibrate(ClientDeviceCommandValue),
32+
Rotate(ClientDeviceCommandValue),
33+
Oscillate(ClientDeviceCommandValue),
34+
Constrict(ClientDeviceCommandValue),
35+
Temperature(ClientDeviceCommandValue),
36+
Led(ClientDeviceCommandValue),
37+
Spray(ClientDeviceCommandValue),
38+
Position(ClientDeviceCommandValue),
39+
PositionWithDuration(ClientDeviceCommandValue, u32),
4940
}
5041

5142
impl ClientDeviceOutputCommand {
52-
pub fn from_command_value_float(
43+
pub fn from_command_value(
5344
output_type: OutputType,
54-
value: f64,
45+
value: &ClientDeviceCommandValue,
5546
) -> Result<Self, ButtplugClientError> {
5647
match output_type {
57-
OutputType::Vibrate => Ok(ClientDeviceOutputCommand::VibrateFloat(value)),
58-
OutputType::Oscillate => Ok(ClientDeviceOutputCommand::OscillateFloat(value)),
59-
OutputType::Rotate => Ok(ClientDeviceOutputCommand::RotateFloat(value)),
60-
OutputType::Constrict => Ok(ClientDeviceOutputCommand::ConstrictFloat(value)),
61-
OutputType::Temperature => Ok(ClientDeviceOutputCommand::TemperatureFloat(value)),
62-
OutputType::Led => Ok(ClientDeviceOutputCommand::LedFloat(value)),
63-
OutputType::Spray => Ok(ClientDeviceOutputCommand::SprayFloat(value)),
64-
OutputType::Position => Ok(ClientDeviceOutputCommand::PositionFloat(value)),
48+
OutputType::Vibrate => Ok(ClientDeviceOutputCommand::Vibrate(*value)),
49+
OutputType::Oscillate => Ok(ClientDeviceOutputCommand::Oscillate(*value)),
50+
OutputType::Rotate => Ok(ClientDeviceOutputCommand::Rotate(*value)),
51+
OutputType::Constrict => Ok(ClientDeviceOutputCommand::Constrict(*value)),
52+
OutputType::Temperature => Ok(ClientDeviceOutputCommand::Temperature(*value)),
53+
OutputType::Led => Ok(ClientDeviceOutputCommand::Led(*value)),
54+
OutputType::Spray => Ok(ClientDeviceOutputCommand::Spray(*value)),
55+
OutputType::Position => Ok(ClientDeviceOutputCommand::Position(*value)),
6556
_ => Err(ButtplugClientError::ButtplugOutputCommandConversionError(
6657
"Cannot use PositionWithDuration with this method".to_owned(),
6758
)),
@@ -72,31 +63,15 @@ impl ClientDeviceOutputCommand {
7263
impl From<&ClientDeviceOutputCommand> for OutputType {
7364
fn from(val: &ClientDeviceOutputCommand) -> Self {
7465
match val {
75-
ClientDeviceOutputCommand::Vibrate(_) | ClientDeviceOutputCommand::VibrateFloat(_) => {
76-
OutputType::Vibrate
77-
}
78-
ClientDeviceOutputCommand::Oscillate(_) | ClientDeviceOutputCommand::OscillateFloat(_) => {
79-
OutputType::Oscillate
80-
}
81-
ClientDeviceOutputCommand::Rotate(_) | ClientDeviceOutputCommand::RotateFloat(_) => {
82-
OutputType::Rotate
83-
}
84-
ClientDeviceOutputCommand::Constrict(_) | ClientDeviceOutputCommand::ConstrictFloat(_) => {
85-
OutputType::Constrict
86-
}
87-
ClientDeviceOutputCommand::Temperature(_)
88-
| ClientDeviceOutputCommand::TemperatureFloat(_) => OutputType::Temperature,
89-
ClientDeviceOutputCommand::Led(_) | ClientDeviceOutputCommand::LedFloat(_) => OutputType::Led,
90-
ClientDeviceOutputCommand::Spray(_) | ClientDeviceOutputCommand::SprayFloat(_) => {
91-
OutputType::Spray
92-
}
93-
ClientDeviceOutputCommand::Position(_) | ClientDeviceOutputCommand::PositionFloat(_) => {
94-
OutputType::Position
95-
}
96-
ClientDeviceOutputCommand::PositionWithDuration(_, _)
97-
| ClientDeviceOutputCommand::PositionWithDurationFloat(_, _) => {
98-
OutputType::PositionWithDuration
99-
}
66+
ClientDeviceOutputCommand::Vibrate(_) => OutputType::Vibrate,
67+
ClientDeviceOutputCommand::Oscillate(_) => OutputType::Oscillate,
68+
ClientDeviceOutputCommand::Rotate(_) => OutputType::Rotate,
69+
ClientDeviceOutputCommand::Constrict(_) => OutputType::Constrict,
70+
ClientDeviceOutputCommand::Temperature(_) => OutputType::Temperature,
71+
ClientDeviceOutputCommand::Led(_) => OutputType::Led,
72+
ClientDeviceOutputCommand::Spray(_) => OutputType::Spray,
73+
ClientDeviceOutputCommand::Position(_) => OutputType::Position,
74+
ClientDeviceOutputCommand::PositionWithDuration(_, _) => OutputType::PositionWithDuration,
10075
}
10176
}
10277
}

crates/buttplug_client/src/device/device.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@
1010
use crate::device::{ClientDeviceCommandValue, ClientDeviceOutputCommand};
1111

1212
use crate::{
13-
ButtplugClientMessageSender,
14-
ButtplugClientResultFuture,
15-
create_boxed_future_client_error,
13+
ButtplugClientMessageSender, ButtplugClientResultFuture, create_boxed_future_client_error,
1614
device::ClientDeviceFeature,
1715
};
1816
use buttplug_core::message::InputType;
1917
use buttplug_core::{
2018
errors::ButtplugDeviceError,
2119
message::{
22-
ButtplugServerMessageV4,
23-
DeviceFeature,
24-
DeviceMessageInfoV4,
25-
OutputType,
26-
StopDeviceCmdV0,
20+
ButtplugServerMessageV4, DeviceFeature, DeviceMessageInfoV4, OutputType, StopDeviceCmdV0,
2721
},
2822
util::stream::convert_broadcast_receiver_to_stream,
2923
};
@@ -211,11 +205,44 @@ impl ButtplugClientDevice {
211205

212206
/// Commands device to vibrate, assuming it has the features to do so.
213207
pub fn vibrate(&self, level: impl Into<ClientDeviceCommandValue>) -> ButtplugClientResultFuture {
214-
let val = level.into();
215-
self.set_client_value(&match val {
216-
ClientDeviceCommandValue::Int(v) => ClientDeviceOutputCommand::Vibrate(v as u32),
217-
ClientDeviceCommandValue::Float(f) => ClientDeviceOutputCommand::VibrateFloat(f),
218-
})
208+
self.set_client_value(&ClientDeviceOutputCommand::Vibrate(level.into()))
209+
}
210+
211+
pub fn oscillate(
212+
&self,
213+
level: impl Into<ClientDeviceCommandValue>,
214+
) -> ButtplugClientResultFuture {
215+
self.set_client_value(&ClientDeviceOutputCommand::Oscillate(level.into()))
216+
}
217+
218+
pub fn rotate(&self, level: impl Into<ClientDeviceCommandValue>) -> ButtplugClientResultFuture {
219+
self.set_client_value(&ClientDeviceOutputCommand::Rotate(level.into()))
220+
}
221+
222+
pub fn spray(&self, level: impl Into<ClientDeviceCommandValue>) -> ButtplugClientResultFuture {
223+
self.set_client_value(&ClientDeviceOutputCommand::Spray(level.into()))
224+
}
225+
226+
pub fn constrict(
227+
&self,
228+
level: impl Into<ClientDeviceCommandValue>,
229+
) -> ButtplugClientResultFuture {
230+
self.set_client_value(&ClientDeviceOutputCommand::Constrict(level.into()))
231+
}
232+
233+
pub fn position(&self, level: impl Into<ClientDeviceCommandValue>) -> ButtplugClientResultFuture {
234+
self.set_client_value(&ClientDeviceOutputCommand::Position(level.into()))
235+
}
236+
237+
pub fn position_with_duration(
238+
&self,
239+
position: impl Into<ClientDeviceCommandValue>,
240+
duration_in_ms: u32,
241+
) -> ButtplugClientResultFuture {
242+
self.set_client_value(&ClientDeviceOutputCommand::PositionWithDuration(
243+
position.into(),
244+
duration_in_ms,
245+
))
219246
}
220247

221248
pub fn has_battery_level(&self) -> bool {
@@ -306,8 +333,7 @@ impl ButtplugClientDevice {
306333
}
307334
}
308335

309-
impl Eq for ButtplugClientDevice {
310-
}
336+
impl Eq for ButtplugClientDevice {}
311337

312338
impl PartialEq for ButtplugClientDevice {
313339
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)