Move serialization implementation and rename messagepack feature to serialize_rmp

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/25/head
trivernis 3 years ago
parent 38fb1ee16a
commit bf5a593377
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -56,4 +56,5 @@ features = ["macros", "rt-multi-thread"]
[features]
default = []
messagepack = ["serde", "rmp-serde"]
serialize = ["serde"]
serialize_rmp = ["serialize", "rmp-serde"]

@ -9,33 +9,29 @@ pub enum Error {
#[error(transparent)]
IoError(#[from] tokio::io::Error),
#[cfg(feature = "messagepack")]
#[error(transparent)]
Decode(#[from] rmp_serde::decode::Error),
#[cfg(feature = "messagepack")]
#[error(transparent)]
Encode(#[from] rmp_serde::encode::Error),
#[cfg(feature = "serialize")]
#[error("failed to serialize event: {0}")]
Serialization(#[from] crate::payload::SerializationError),
#[error("Build Error: {0}")]
#[error("build Error: {0}")]
BuildError(String),
#[error("{0}")]
Message(String),
#[error("Channel Error: {0}")]
#[error("channel Error: {0}")]
ReceiveError(#[from] oneshot::error::RecvError),
#[error("The received event was corrupted")]
#[error("the received event was corrupted")]
CorruptedEvent,
#[error("Send Error")]
#[error("send Error")]
SendError,
#[error("Error response: {0}")]
#[error("received error response: {0}")]
ErrorEvent(#[from] ErrorEventData),
#[error("Timed out")]
#[error("timed out")]
Timeout,
}

@ -7,6 +7,10 @@ pub mod event;
pub mod event_handler;
pub mod payload;
#[cfg(feature = "serialize")]
pub mod payload_serializer;
/// Generates a new event id
pub(crate) fn generate_event_id() -> u64 {
lazy_static::lazy_static! {

@ -2,6 +2,9 @@ use crate::prelude::IPCResult;
use byteorder::{BigEndian, ReadBytesExt};
use std::io::Read;
#[cfg(feature = "serialize")]
pub use super::payload_serializer::*;
/// Trait to convert event data into sending bytes
/// It is implemented for all types that implement Serialize
pub trait EventSendPayload {
@ -109,36 +112,3 @@ where
})
}
}
#[cfg(feature = "messagepack")]
mod rmp_impl {
use super::{EventReceivePayload, EventSendPayload};
use crate::prelude::IPCResult;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::io::Read;
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)
}
}
}
#[cfg(feature = "messagepack")]
pub use rmp_impl::*;

@ -0,0 +1,4 @@
#[cfg(feature = "serialize_rmp")]
pub mod serialize_rmp;
#[cfg(feature = "serialize_rmp")]
pub use serialize_rmp::*;

@ -0,0 +1,48 @@
use crate::payload::{EventReceivePayload, EventSendPayload};
use crate::prelude::{IPCError, IPCResult};
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),
#[error("failed to deserialize with rmp: {0}")]
Deserialize(#[from] rmp_serde::decode::Error),
}
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)
}
}

@ -88,7 +88,7 @@ async fn handle_pong_event(ctx: &Context, event: Event) -> IPCResult<()> {
Ok(())
}
#[cfg(feature = "messagepack")]
#[cfg(feature = "serialize")]
mod payload_impl {
use serde::{Deserialize, Serialize};
@ -99,7 +99,7 @@ mod payload_impl {
}
}
#[cfg(not(feature = "messagepack"))]
#[cfg(not(feature = "serialize"))]
mod payload_impl {
use bromine::error::Result;
use bromine::payload::{EventReceivePayload, EventSendPayload};

Loading…
Cancel
Save