From 253a5958b0ce6fac5602756bbbbbb40f06444160 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 31 Oct 2020 23:25:26 +0100 Subject: [PATCH] Add tests for serialization and deserialization Signed-off-by: trivernis --- Cargo.toml | 2 +- src/event.rs | 32 +++++++++++++++++++++++++++----- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++-- src/result.rs | 4 ++++ 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f1d55b..8700267 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/event.rs b/src/event.rs index 50830e1..ac6b325 100644 --- a/src/event.rs +++ b/src/event.rs @@ -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 { - event_name: String, - payload: T, + pub name: String, + pub payload: T, +} + +impl Event { + /// Creates a new Event with an empty payload + pub fn new(name: String) -> Self { + Self { + name, + payload: EmptyPayload {} + } + } } impl Event 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 Event where T: Serialize + DeserializeOwned { /// `payload`: `payload-length`, pub fn to_bytes(&self) -> VentedResult> { 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 Event where T: Serialize + DeserializeOwned { let payload = rmp_serde::from_read(bytes.take(payload_length))?; Ok(Self { - event_name, + name: event_name, payload, }) } diff --git a/src/lib.rs b/src/lib.rs index ace3ff1..20e32d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::::from_bytes(&mut event_bytes.as_slice()).unwrap(); + assert_eq!(deserialized_event.name, "test".to_string()); + assert_eq!(deserialized_event.payload, payload); } } diff --git a/src/result.rs b/src/result.rs index 92923b3..72ad54e 100644 --- a/src/result.rs +++ b/src/result.rs @@ -1,7 +1,9 @@ use std::{fmt, io}; +use std::error::Error; pub type VentedResult = Result; +#[derive(Debug)] pub enum VentedError { NameDecodingError, IOError(io::Error), @@ -20,6 +22,8 @@ impl fmt::Display for VentedError { } } +impl Error for VentedError {} + impl From for VentedError { fn from(other: io::Error) -> Self { Self::IOError(other)