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::v2::{DeviceListV2, DeviceMessageInfoV2};
9use buttplug_core::{
10 errors::ButtplugMessageError,
11 message::{ButtplugMessage, ButtplugMessageFinalizer, ButtplugMessageValidator, DeviceListV4},
12};
13use getset::Getters;
14use serde::{Deserialize, Serialize};
15
16use super::DeviceMessageInfoV3;
17
18/// List of all devices currently connected to the server.
19#[derive(Default, Clone, Debug, ButtplugMessage, Getters, Serialize, Deserialize)]
20pub struct DeviceListV3 {
21 #[serde(rename = "Id")]
22 id: u32,
23 #[serde(rename = "Devices")]
24 #[getset(get = "pub")]
25 devices: Vec<DeviceMessageInfoV3>,
26}
27
28impl DeviceListV3 {
29 pub fn new(devices: Vec<DeviceMessageInfoV3>) -> Self {
30 Self { id: 1, devices }
31 }
32}
33
34impl ButtplugMessageValidator for DeviceListV3 {
35 fn is_valid(&self) -> Result<(), ButtplugMessageError> {
36 self.is_not_system_id(self.id)
37 }
38}
39
40impl ButtplugMessageFinalizer for DeviceListV3 {
41 fn finalize(&mut self) {
42 for device in &mut self.devices {
43 device.device_messages_mut().finalize();
44 }
45 }
46}
47
48impl From<DeviceListV3> for DeviceListV2 {
49 fn from(msg: DeviceListV3) -> Self {
50 let mut devices = vec![];
51 for d in msg.devices() {
52 devices.push(DeviceMessageInfoV2::from(d.clone()));
53 }
54 Self {
55 id: msg.id(),
56 devices,
57 }
58 }
59}
60
61impl From<DeviceListV4> for DeviceListV3 {
62 fn from(value: DeviceListV4) -> Self {
63 let mut dl3 = DeviceListV3::new(value.devices().iter().map(|x| x.1.clone().into()).collect());
64 dl3.set_id(value.id());
65 dl3
66 }
67}