Rename Message to Event and add from_bytes implementation

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 7f6450be48
commit 3139745590
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -10,5 +10,4 @@ edition = "2018"
rmp = "0.8.9"
rmp-serde = "0.14.4"
serde = "1.0.117"
crc = "1.8.1"
byteorder = "1.3.4"

@ -0,0 +1,60 @@
use serde::{Serialize};
use crate::result::{VentedResult, VentedError};
use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use std::io::Read;
use serde::de::DeserializeOwned;
/// A single event that has a name and payload.
/// The payload is encoded with message pack
#[derive(Clone, Debug)]
pub struct Event<T> {
event_name: String,
payload: T,
}
impl<T> Event<T> where T: Serialize + DeserializeOwned {
/// Returns the byte representation for the message
/// the format is
/// `name-length`: `u16`,
/// `name`: `name-length`,
/// `payload-length`: `u64`,
/// `payload`: `payload-length`,
pub fn to_bytes(&self) -> VentedResult<Vec<u8>> {
let mut payload_raw = rmp_serde::to_vec(&self.payload)?;
let mut name_raw = self.event_name.as_bytes().to_vec();
let name_length = name_raw.len();
let mut name_length_raw = [0u8; 2];
BigEndian::write_u16(&mut name_length_raw, name_length as u16);
let payload_length = payload_raw.len();
let mut payload_length_raw = [0u8; 8];
BigEndian::write_u64(&mut payload_length_raw, payload_length as u64);
let mut data = Vec::new();
data.append(&mut name_length_raw.to_vec());
data.append(&mut name_raw);
data.append(&mut payload_length_raw.to_vec());
data.append(&mut payload_raw);
Ok(data)
}
/// Deserializes the message from bytes that can be read from the given reader
/// The result will be the Message with the specific message payload type
pub fn from_bytes<R: Read>(bytes: &mut R) -> VentedResult<Self> {
let name_length = bytes.read_u16::<BigEndian>()?;
let mut name_buf = vec![0u8; name_length as usize];
bytes.read_exact(&mut name_buf)?;
let event_name = String::from_utf8(name_buf).map_err(|_| VentedError::NameDecodingError)?;
let payload_length = bytes.read_u64::<BigEndian>()?;
let payload = rmp_serde::from_read(bytes.take(payload_length))?;
Ok(Self {
event_name,
payload,
})
}
}

@ -1,4 +1,4 @@
pub mod message;
pub mod event;
pub mod result;
#[cfg(test)]

@ -1,45 +0,0 @@
use serde::Serialize;
use crate::result::VentedResult;
use byteorder::{BigEndian, ByteOrder};
use crc::crc32;
#[derive(Clone, Debug)]
pub struct Message<T> {
event_name: String,
payload: T,
}
impl<T> Message<T> where T: Serialize {
/// Returns the byte representation for the message
/// the format is
/// name-length: u16,
/// name: name-length,
/// payload-length: u64,
/// payload: payload-length,
/// crc: u32
pub fn to_bytes(&self) -> VentedResult<Vec<u8>> {
let mut payload_raw = rmp_serde::to_vec(&self.payload)?;
let mut name_raw = self.event_name.as_bytes().to_vec();
let name_length = name_raw.len();
let mut name_length_raw = [0u8; 2];
BigEndian::write_u16(&mut name_length_raw, name_length as u16);
let payload_length = payload_raw.len();
let mut payload_length_raw = [0u8; 8];
BigEndian::write_u64(&mut payload_length_raw, payload_length as u64);
let mut data = Vec::new();
data.append(&mut name_length_raw.to_vec());
data.append(&mut name_raw);
data.append(&mut payload_length_raw.to_vec());
data.append(&mut payload_raw);
let crc = crc32::checksum_ieee(&data);
let mut crc_raw = [0u8; 4];
BigEndian::write_u32(&mut crc_raw, crc);
data.append(&mut crc_raw.to_vec());
Ok(data)
}
}

@ -3,15 +3,19 @@ use std::{fmt, io};
pub type VentedResult<T> = Result<T, VentedError>;
pub enum VentedError {
NameDecodingError,
IOError(io::Error),
SerializeError(rmp_serde::encode::Error)
SerializeError(rmp_serde::encode::Error),
DeserializeError(rmp_serde::decode::Error),
}
impl fmt::Display for VentedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NameDecodingError => write!(f, "Failed to decode event name"),
Self::IOError(e) => write!(f, "IO Error: {}", e.to_string()),
Self::SerializeError(e) => write!(f, "Serialization Error: {}", e.to_string()),
Self::DeserializeError(e) => write!(f, "Deserialization Error: {}", e.to_string()),
}
}
}
@ -26,4 +30,10 @@ impl From<rmp_serde::encode::Error> for VentedError {
fn from(other: rmp_serde::encode::Error) -> Self {
Self::SerializeError(other)
}
}
impl From<rmp_serde::decode::Error> for VentedError {
fn from(other: rmp_serde::decode::Error) -> Self {
Self::DeserializeError(other)
}
}
Loading…
Cancel
Save