1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#[cfg(feature = "serialize-json")]
mod json_serializer;
#[cfg(feature = "serialize-json")]
pub use json_serializer::{ButtplugClientJSONSerializer, ButtplugServerJSONSerializer};

use thiserror::Error;
pub type ButtplugSerializerResult<T> = Result<T, ButtplugSerializerError>;

#[derive(Debug, Error, Clone)]
pub enum ButtplugSerializerError {
  // Valico hands back a vector of errors that isn't easy to encase, so we just

  // turn it into a big string and pass that back.

  #[error("JSON Schema Validation Error: {0}")]
  JsonValidatorError(String),
  /// Serialization error.

  #[error("Cannot serialize to JSON: {0}")]
  JsonSerializerError(String),
  #[error("Cannot deserialize binary in a text handler")]
  BinaryDeserializationError,
  #[error("Cannot deserialize text in a binary handler.")]
  TextDeserializationError,
  #[error("Message version not received, can't figure out which spec version to de/serialize to.")]
  MessageSpecVersionNotReceived,
}

#[derive(Debug, PartialEq)]
pub enum ButtplugSerializedMessage {
  Text(String),
  Binary(Vec<u8>),
}

impl From<String> for ButtplugSerializedMessage {
  fn from(msg: String) -> Self {
    ButtplugSerializedMessage::Text(msg)
  }
}

impl From<Vec<u8>> for ButtplugSerializedMessage {
  fn from(msg: Vec<u8>) -> Self {
    ButtplugSerializedMessage::Binary(msg)
  }
}

pub trait ButtplugMessageSerializer: Default + Sync + Send {
  type Inbound;
  type Outbound;
  fn deserialize(
    &mut self,
    msg: ButtplugSerializedMessage,
  ) -> ButtplugSerializerResult<Vec<Self::Inbound>>;
  fn serialize(&mut self, msg: Vec<Self::Outbound>) -> ButtplugSerializedMessage;
}