1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::*;
#[cfg(feature = "serialize_json")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type MessageAttributesMap = HashMap<ButtplugDeviceMessageType, MessageAttributes>;
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceMessageInfo {
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
pub device_index: u32,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceName"))]
pub device_name: String,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceMessages"))]
pub device_messages: MessageAttributesMap,
}
impl From<&DeviceAdded> for DeviceMessageInfo {
fn from(device_added: &DeviceAdded) -> Self {
Self {
device_index: device_added.device_index,
device_name: device_added.device_name.clone(),
device_messages: device_added.device_messages.clone(),
}
}
}
impl From<DeviceAdded> for DeviceMessageInfo {
fn from(device_added: DeviceAdded) -> Self {
Self {
device_index: device_added.device_index,
device_name: device_added.device_name,
device_messages: device_added.device_messages,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceMessageInfoV1 {
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
pub device_index: u32,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceName"))]
pub device_name: String,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceMessages"))]
pub device_messages: MessageAttributesMap,
}
impl From<DeviceAdded> for DeviceMessageInfoV1 {
fn from(device_added: DeviceAdded) -> Self {
let dmi = DeviceMessageInfo::from(device_added);
DeviceMessageInfoV1::from(dmi)
}
}
impl From<DeviceMessageInfo> for DeviceMessageInfoV1 {
fn from(device_message_info: DeviceMessageInfo) -> Self {
let mut dmi_v1 = Self {
device_index: device_message_info.device_index,
device_name: device_message_info.device_name,
device_messages: device_message_info.device_messages,
};
let v2_message_types = [
ButtplugDeviceMessageType::RawReadCmd,
ButtplugDeviceMessageType::RawWriteCmd,
ButtplugDeviceMessageType::SubscribeCmd,
ButtplugDeviceMessageType::UnsubscribeCmd,
ButtplugDeviceMessageType::BatteryLevelCmd,
ButtplugDeviceMessageType::RSSILevelCmd,
];
for t in &v2_message_types {
dmi_v1.device_messages.remove(t);
}
for (_, mut attributes) in &mut dmi_v1.device_messages {
let fc = attributes.feature_count;
*attributes = MessageAttributes::default();
attributes.feature_count = fc;
}
if dmi_v1
.device_messages
.contains_key(&ButtplugDeviceMessageType::VibrateCmd)
{
dmi_v1.device_messages.insert(
ButtplugDeviceMessageType::SingleMotorVibrateCmd,
MessageAttributes::default(),
);
}
dmi_v1
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceMessageInfoV0 {
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
pub device_index: u32,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceName"))]
pub device_name: String,
#[cfg_attr(feature = "serialize_json", serde(rename = "DeviceMessages"))]
pub device_messages: Vec<ButtplugDeviceMessageType>,
}
impl From<DeviceAdded> for DeviceMessageInfoV0 {
fn from(device_added: DeviceAdded) -> Self {
let dmi = DeviceMessageInfo::from(device_added);
let dmi_v1: DeviceMessageInfoV1 = dmi.into();
dmi_v1.into()
}
}
impl From<DeviceMessageInfoV1> for DeviceMessageInfoV0 {
fn from(device_message_info: DeviceMessageInfoV1) -> Self {
let mut device_messages: Vec<ButtplugDeviceMessageType> = device_message_info
.device_messages
.keys()
.cloned()
.collect();
let v1_message_types = [
ButtplugDeviceMessageType::VibrateCmd,
ButtplugDeviceMessageType::RotateCmd,
ButtplugDeviceMessageType::LinearCmd,
];
device_messages.retain(|x| !v1_message_types.contains(x));
Self {
device_name: device_message_info.device_name,
device_index: device_message_info.device_index,
device_messages,
}
}
}