Buttplug sex toy control library
at dev 157 lines 5.1 kB view raw
1use serde::{ 2 Deserialize, 3 Deserializer, 4 Serialize, 5 Serializer, 6 de::{self, Visitor}, 7}; 8use std::{ 9 fmt::{self, Debug}, 10 str::FromStr, 11 string::ToString, 12}; 13 14use core::hash::Hash; 15 16/// Endpoint names for device communication. 17/// 18/// Endpoints denote different contextual communication targets on a device. For instance, for a 19/// device that uses UART style communication (serial, a lot of Bluetooth LE devices, etc...) most 20/// devices will just have a Tx and Rx endpoint. However, on other devices that can have varying 21/// numbers of endpoints and configurations (USB, Bluetooth LE, etc...) we add some names with more 22/// context. These names are used in [Device Configuration](crate::server::device::configuration) 23/// and the [Device Configuration File](crate::util::device_configuration), and are expected to 24/// de/serialize to lowercase versions of their names. 25#[derive(EnumString, Clone, Debug, PartialEq, Eq, Hash, Display, Copy)] 26#[strum(serialize_all = "lowercase")] 27pub enum Endpoint { 28 /// Expect to take commands, when multiple receive endpoints may be available 29 Command, 30 /// Firmware updates (Buttplug does not update firmware, but some firmware endpoints are used for 31 /// mode setting) 32 Firmware, 33 /// Common receive endpoint name 34 Rx, 35 /// Receive endpoint for accelerometer data 36 RxAccel, 37 /// Receive endpoint for battery levels (usually expected to be BLE standard profile) 38 RxBLEBattery, 39 /// Receive endpoint for BLE model (usually expected to be BLE standard profile) 40 RxBLEModel, 41 /// Receive endpoint for pressure sensors 42 RxPressure, 43 /// Receive endpoint for touch sensors 44 RxTouch, 45 /// Common transmit endpoint name 46 Tx, 47 /// Transmit endpoint for hardware mode setting. 48 TxMode, 49 /// Transmit endpoint for shock setting (unused) 50 TxShock, 51 /// Transmit endpoint for vibration setting 52 TxVibrate, 53 /// Transmit endpoint for vendor (proprietary) control 54 TxVendorControl, 55 /// Transmit endpoint for whitelist updating 56 Whitelist, 57 /// Generic endpoint (available for user configurations) 58 Generic0, 59 /// Generic endpoint (available for user configurations) 60 Generic1, 61 /// Generic endpoint (available for user configurations) 62 Generic2, 63 /// Generic endpoint (available for user configurations) 64 Generic3, 65 /// Generic endpoint (available for user configurations) 66 Generic4, 67 /// Generic endpoint (available for user configurations) 68 Generic5, 69 /// Generic endpoint (available for user configurations) 70 Generic6, 71 /// Generic endpoint (available for user configurations) 72 Generic7, 73 /// Generic endpoint (available for user configurations) 74 Generic8, 75 /// Generic endpoint (available for user configurations) 76 Generic9, 77 /// Generic endpoint (available for user configurations) 78 Generic10, 79 /// Generic endpoint (available for user configurations) 80 Generic11, 81 /// Generic endpoint (available for user configurations) 82 Generic12, 83 /// Generic endpoint (available for user configurations) 84 Generic13, 85 /// Generic endpoint (available for user configurations) 86 Generic14, 87 /// Generic endpoint (available for user configurations) 88 Generic15, 89 /// Generic endpoint (available for user configurations) 90 Generic16, 91 /// Generic endpoint (available for user configurations) 92 Generic17, 93 /// Generic endpoint (available for user configurations) 94 Generic18, 95 /// Generic endpoint (available for user configurations) 96 Generic19, 97 /// Generic endpoint (available for user configurations) 98 Generic20, 99 /// Generic endpoint (available for user configurations) 100 Generic21, 101 /// Generic endpoint (available for user configurations) 102 Generic22, 103 /// Generic endpoint (available for user configurations) 104 Generic23, 105 /// Generic endpoint (available for user configurations) 106 Generic24, 107 /// Generic endpoint (available for user configurations) 108 Generic25, 109 /// Generic endpoint (available for user configurations) 110 Generic26, 111 /// Generic endpoint (available for user configurations) 112 Generic27, 113 /// Generic endpoint (available for user configurations) 114 Generic28, 115 /// Generic endpoint (available for user configurations) 116 Generic29, 117 /// Generic endpoint (available for user configurations) 118 Generic30, 119 /// Generic endpoint (available for user configurations) 120 Generic31, 121} 122 123// Implement to/from string serialization for Endpoint struct 124impl Serialize for Endpoint { 125 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 126 where 127 S: Serializer, 128 { 129 serializer.serialize_str(&self.to_string()) 130 } 131} 132 133struct EndpointVisitor; 134 135impl Visitor<'_> for EndpointVisitor { 136 type Value = Endpoint; 137 138 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 139 formatter.write_str("a string representing an endpoint") 140 } 141 142 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> 143 where 144 E: de::Error, 145 { 146 Endpoint::from_str(value).map_err(|e| E::custom(format!("{e}"))) 147 } 148} 149 150impl<'de> Deserialize<'de> for Endpoint { 151 fn deserialize<D>(deserializer: D) -> Result<Endpoint, D::Error> 152 where 153 D: Deserializer<'de>, 154 { 155 deserializer.deserialize_str(EndpointVisitor) 156 } 157}