Add tests for serialization and deserialization

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

@ -9,5 +9,5 @@ edition = "2018"
[dependencies]
rmp = "0.8.9"
rmp-serde = "0.14.4"
serde = "1.0.117"
serde = {version = "1.0.117", features = ["serde_derive"]}
byteorder = "1.3.4"

@ -1,18 +1,40 @@
use serde::{Serialize};
use serde::{Serialize, Deserialize};
use crate::result::{VentedResult, VentedError};
use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use std::io::Read;
use serde::de::DeserializeOwned;
#[derive(Clone, Serialize, Deserialize)]
pub struct EmptyPayload {}
/// 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,
pub name: String,
pub payload: T,
}
impl Event<EmptyPayload> {
/// Creates a new Event with an empty payload
pub fn new(name: String) -> Self {
Self {
name,
payload: EmptyPayload {}
}
}
}
impl<T> Event<T> where T: Serialize + DeserializeOwned {
/// Creates a new Event with a payload
pub fn with_payload(name: String, payload: T) -> Self {
Self {
name,
payload,
}
}
/// Returns the byte representation for the message
/// the format is
/// `name-length`: `u16`,
@ -21,7 +43,7 @@ impl<T> Event<T> where T: Serialize + DeserializeOwned {
/// `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 mut name_raw = self.name.as_bytes().to_vec();
let name_length = name_raw.len();
let mut name_length_raw = [0u8; 2];
@ -53,7 +75,7 @@ impl<T> Event<T> where T: Serialize + DeserializeOwned {
let payload = rmp_serde::from_read(bytes.take(payload_length))?;
Ok(Self {
event_name,
name: event_name,
payload,
})
}

@ -3,8 +3,44 @@ pub mod result;
#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use crate::event::Event;
#[derive(PartialEq, Serialize, Deserialize, Clone, Debug)]
pub struct SimplePayload {
string: String,
number: u32,
float: f32
}
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
fn it_serializes_events() {
let payload = SimplePayload {
string: "test".to_string(),
number: 7,
float: 2.1
};
let payload_raw = rmp_serde::to_vec(&payload).unwrap();
let event = Event::with_payload("test".to_string(), payload);
let event_bytes = event.to_bytes().unwrap();
assert_eq!(event_bytes[0..2], [0x00, 0x04]);
assert_eq!(event_bytes[6..14], payload_raw.len().to_be_bytes());
}
#[test]
fn it_deserializes_events() {
let payload = SimplePayload {
string: "test".to_string(),
number: 7,
float: 2.1
};
let event = Event::with_payload("test".to_string(), payload.clone());
let event_bytes = event.to_bytes().unwrap();
let deserialized_event = Event::<SimplePayload>::from_bytes(&mut event_bytes.as_slice()).unwrap();
assert_eq!(deserialized_event.name, "test".to_string());
assert_eq!(deserialized_event.payload, payload);
}
}

@ -1,7 +1,9 @@
use std::{fmt, io};
use std::error::Error;
pub type VentedResult<T> = Result<T, VentedError>;
#[derive(Debug)]
pub enum VentedError {
NameDecodingError,
IOError(io::Error),
@ -20,6 +22,8 @@ impl fmt::Display for VentedError {
}
}
impl Error for VentedError {}
impl From<io::Error> for VentedError {
fn from(other: io::Error) -> Self {
Self::IOError(other)

Loading…
Cancel
Save