Buttplug sex toy control library
at master 95 lines 2.3 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 8//! Fleshlight FW v1.2 Command (Version 0 Message, Deprecated) 9 10use buttplug_core::{ 11 errors::ButtplugMessageError, 12 message::{ 13 ButtplugDeviceMessage, 14 ButtplugMessage, 15 ButtplugMessageFinalizer, 16 ButtplugMessageValidator, 17 }, 18}; 19use getset::CopyGetters; 20use serde::{Deserialize, Serialize}; 21 22#[derive( 23 Debug, 24 ButtplugDeviceMessage, 25 ButtplugMessageFinalizer, 26 PartialEq, 27 Eq, 28 Clone, 29 CopyGetters, 30 Serialize, 31 Deserialize, 32)] 33pub struct FleshlightLaunchFW12CmdV0 { 34 #[serde(rename = "Id")] 35 id: u32, 36 #[serde(rename = "DeviceIndex")] 37 device_index: u32, 38 #[serde(rename = "Position")] 39 #[getset(get_copy = "pub")] 40 position: u8, 41 #[serde(rename = "Speed")] 42 #[getset(get_copy = "pub")] 43 speed: u8, 44} 45 46impl FleshlightLaunchFW12CmdV0 { 47 pub fn new(device_index: u32, position: u8, speed: u8) -> Self { 48 Self { 49 id: 1, 50 device_index, 51 position, 52 speed, 53 } 54 } 55} 56 57impl ButtplugMessageValidator for FleshlightLaunchFW12CmdV0 { 58 fn is_valid(&self) -> Result<(), ButtplugMessageError> { 59 self.is_not_system_id(self.id)?; 60 if !(0..100).contains(&self.speed) { 61 Err(ButtplugMessageError::InvalidMessageContents(format!( 62 "FleshlightFW12Cmd speed {} invalid, should be between 0 and 99", 63 self.speed 64 ))) 65 } else if !(0..100).contains(&self.position) { 66 Err(ButtplugMessageError::InvalidMessageContents(format!( 67 "FleshlightFW12Cmd position {} invalid, should be between 0 and 99", 68 self.position 69 ))) 70 } else { 71 Ok(()) 72 } 73 } 74} 75 76#[cfg(test)] 77mod test { 78 use super::{ButtplugMessageValidator, FleshlightLaunchFW12CmdV0}; 79 80 #[test] 81 pub fn test_legacy_fleshlight_message_bounds() { 82 assert!(FleshlightLaunchFW12CmdV0::new(0, 0, 0).is_valid().is_ok()); 83 assert!(FleshlightLaunchFW12CmdV0::new(0, 99, 99).is_valid().is_ok()); 84 assert!( 85 FleshlightLaunchFW12CmdV0::new(0, 100, 99) 86 .is_valid() 87 .is_err() 88 ); 89 assert!( 90 FleshlightLaunchFW12CmdV0::new(0, 99, 100) 91 .is_valid() 92 .is_err() 93 ); 94 } 95}