Buttplug sex toy control library
at dev 2.0 kB view raw
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 buttplug_core::{ 9 errors::ButtplugMessageError, 10 message::{ 11 ButtplugDeviceMessage, 12 ButtplugMessage, 13 ButtplugMessageFinalizer, 14 ButtplugMessageValidator, 15 }, 16}; 17use getset::{CopyGetters, Getters}; 18use serde::{Deserialize, Serialize}; 19 20/// Move device to a certain position in a certain amount of time 21#[derive(Debug, PartialEq, Clone, CopyGetters, Serialize, Deserialize)] 22#[getset(get_copy = "pub")] 23pub struct VectorSubcommandV1 { 24 #[serde(rename = "Index")] 25 index: u32, 26 #[serde(rename = "Duration")] 27 duration: u32, 28 #[serde(rename = "Position")] 29 position: f64, 30} 31 32impl VectorSubcommandV1 { 33 pub fn new(index: u32, duration: u32, position: f64) -> Self { 34 Self { 35 index, 36 duration, 37 position, 38 } 39 } 40} 41 42#[derive( 43 Debug, 44 ButtplugDeviceMessage, 45 ButtplugMessageFinalizer, 46 PartialEq, 47 Clone, 48 Getters, 49 Serialize, 50 Deserialize, 51)] 52pub struct LinearCmdV1 { 53 #[serde(rename = "Id")] 54 id: u32, 55 #[serde(rename = "DeviceIndex")] 56 device_index: u32, 57 #[serde(rename = "Vectors")] 58 #[getset(get = "pub")] 59 vectors: Vec<VectorSubcommandV1>, 60} 61 62impl LinearCmdV1 { 63 pub fn new(device_index: u32, vectors: Vec<VectorSubcommandV1>) -> Self { 64 Self { 65 id: 1, 66 device_index, 67 vectors, 68 } 69 } 70} 71 72impl ButtplugMessageValidator for LinearCmdV1 { 73 fn is_valid(&self) -> Result<(), ButtplugMessageError> { 74 self.is_not_system_id(self.id)?; 75 for vec in &self.vectors { 76 self.is_in_command_range( 77 vec.position, 78 format!( 79 "VectorSubcommand position {} for index {} is invalid, should be between 0.0 and 1.0", 80 vec.position, vec.index 81 ), 82 )?; 83 } 84 Ok(()) 85 } 86}