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 8//! Message de/serialization handling 9pub mod json_serializer; 10 11use serde::{Deserialize, Serialize}; 12use thiserror::Error; 13pub type ButtplugSerializerResult<T> = Result<T, ButtplugSerializerError>; 14 15#[derive(Debug, Error, Clone, Serialize, Deserialize, PartialEq, Eq)] 16pub enum ButtplugSerializerError { 17 // jsonschema hands back a vector of errors that isn't easy to encase, so we just 18 // turn it into a big string and pass that back. 19 #[error("JSON Schema Validation Error: {0}")] 20 JsonValidatorError(String), 21 /// Serialization error. 22 #[error("Cannot serialize to JSON: {0}")] 23 JsonSerializerError(String), 24 #[error("Cannot deserialize binary in a text handler")] 25 BinaryDeserializationError, 26 #[error("Cannot deserialize text in a binary handler.")] 27 TextDeserializationError, 28 #[error("Message version not received, can't figure out which spec version to de/serialize to.")] 29 MessageSpecVersionNotReceived, 30} 31 32#[derive(Debug, Clone, PartialEq, Eq)] 33pub enum ButtplugSerializedMessage { 34 Text(String), 35 Binary(Vec<u8>), 36} 37 38impl From<String> for ButtplugSerializedMessage { 39 fn from(msg: String) -> Self { 40 ButtplugSerializedMessage::Text(msg) 41 } 42} 43 44impl From<Vec<u8>> for ButtplugSerializedMessage { 45 fn from(msg: Vec<u8>) -> Self { 46 ButtplugSerializedMessage::Binary(msg) 47 } 48} 49 50pub trait ButtplugMessageSerializer: Default + Sync + Send { 51 type Inbound; 52 type Outbound; 53 fn deserialize( 54 &self, 55 msg: &ButtplugSerializedMessage, 56 ) -> ButtplugSerializerResult<Vec<Self::Inbound>>; 57 fn serialize(&self, msg: &[Self::Outbound]) -> ButtplugSerializedMessage; 58}