mirror of https://github.com/Trivernis/bromine.git
Change serialization to be able to use multiple formats
Signed-off-by: trivernis <trivernis@protonmail.com>pull/26/head
parent
d1b426e10b
commit
6299f9be02
@ -1,23 +1,158 @@
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
mod serialize_rmp;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
pub use serialize_rmp::*;
|
||||
mod serialize_rmp;
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
mod serialize_bincode;
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
pub use serialize_bincode::*;
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
mod serialize_postcard;
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
pub use serialize_postcard::*;
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
mod serialize_json;
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
pub use serialize_json::*;
|
||||
pub type SerializationResult<T> = std::result::Result<T, SerializationError>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializationError {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
#[error("failed to serialize messagepack payload: {0}")]
|
||||
SerializeRmp(#[from] rmp_serde::encode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
#[error("failed to deserialize messagepack payload: {0}")]
|
||||
DeserializeRmp(#[from] rmp_serde::decode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
#[error("failed to de/serialize bincode payload: {0}")]
|
||||
Bincode(#[from] bincode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
#[error("failed to de/serialize postcard payload: {0}")]
|
||||
Postcard(#[from] postcard::Error),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
#[error("failed to de/serialize json payload: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
|
||||
#[error("io error occurred on de/serialization: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("the format {0:?} is not available")]
|
||||
UnavailableFormat(DynamicSerializer),
|
||||
|
||||
#[error("tried to create serializer for unknown format {0}")]
|
||||
UnknownFormat(usize),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub enum DynamicSerializer {
|
||||
Messagepack,
|
||||
Bincode,
|
||||
Postcard,
|
||||
Json,
|
||||
}
|
||||
|
||||
impl DynamicSerializer {
|
||||
pub fn first_available() -> Self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
{
|
||||
Self::Messagepack
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "serialize_bincode", not(feature = "serialize_rmp")))]
|
||||
{
|
||||
Self::Bincode
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "serialize_postcard",
|
||||
not(any(feature = "serialize_rmp", feature = "serialize_bincode"))
|
||||
))]
|
||||
{
|
||||
Self::Postcard
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "serialize_json",
|
||||
not(any(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard"
|
||||
))
|
||||
))]
|
||||
{
|
||||
Self::Json
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_primitive(num: usize) -> SerializationResult<Self> {
|
||||
match num {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
0 => Ok(Self::Messagepack),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
1 => Ok(Self::Bincode),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
2 => Ok(Self::Postcard),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
3 => Ok(Self::Json),
|
||||
|
||||
n => Err(SerializationError::UnknownFormat(n)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize<T: Serialize>(&self, data: T) -> SerializationResult<Vec<u8>> {
|
||||
match self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
DynamicSerializer::Messagepack => serialize_rmp::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
DynamicSerializer::Bincode => serialize_bincode::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
DynamicSerializer::Postcard => serialize_postcard::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
DynamicSerializer::Json => serialize_json::serialize(data),
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard",
|
||||
feature = "serialize_json"
|
||||
)))]
|
||||
_ => Err(SerializationError::UnavailableFormat(self.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize<T: DeserializeOwned, R: Read>(&self, reader: R) -> SerializationResult<T> {
|
||||
match self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
DynamicSerializer::Messagepack => serialize_rmp::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
DynamicSerializer::Bincode => serialize_bincode::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
DynamicSerializer::Postcard => serialize_postcard::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
DynamicSerializer::Json => serialize_json::deserialize(reader),
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard",
|
||||
feature = "serialize_json"
|
||||
)))]
|
||||
_ => Err(SerializationError::UnavailableFormat(self.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,15 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = bincode::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = bincode::serialize(&data)?;
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = bincode::serialize(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = bincode::deserialize_from(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = bincode::deserialize_from(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,29 +1,16 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = serde_json::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = serde_json::to_vec(&data)?;
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = serde_json::to_vec(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = serde_json::from_reader(reader)?;
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = serde_json::from_reader(reader)?;
|
||||
|
||||
Ok(type_data)
|
||||
}
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,32 +1,19 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = postcard::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = postcard::to_allocvec(&data)?.to_vec();
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = postcard::to_allocvec(&self)?.to_vec();
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(mut reader: R) -> IPCResult<Self> {
|
||||
let mut buf = Vec::new();
|
||||
// reading to end means reading the full size of the provided data
|
||||
reader.read_to_end(&mut buf)?;
|
||||
let type_data = postcard::from_bytes(&buf)?;
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(mut reader: R) -> SerializationResult<T> {
|
||||
let mut buf = Vec::new();
|
||||
// reading to end means reading the full size of the provided data
|
||||
reader.read_to_end(&mut buf)?;
|
||||
let type_data = postcard::from_bytes(&buf)?;
|
||||
|
||||
Ok(type_data)
|
||||
}
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,48 +1,15 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::{IPCError, IPCResult};
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializationError {
|
||||
#[error("failed to serialize with rmp: {0}")]
|
||||
Serialize(#[from] rmp_serde::encode::Error),
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = rmp_serde::to_vec(&data)?;
|
||||
|
||||
#[error("failed to deserialize with rmp: {0}")]
|
||||
Deserialize(#[from] rmp_serde::decode::Error),
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl From<rmp_serde::decode::Error> for IPCError {
|
||||
fn from(e: rmp_serde::decode::Error) -> Self {
|
||||
IPCError::Serialization(SerializationError::Deserialize(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp_serde::encode::Error> for IPCError {
|
||||
fn from(e: rmp_serde::encode::Error) -> Self {
|
||||
IPCError::Serialization(SerializationError::Serialize(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = rmp_serde::to_vec(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = rmp_serde::from_read(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = rmp_serde::from_read(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
|
Loading…
Reference in New Issue