From 6c8b17fd19c4a02e1868ed1ceb7c83d284706bb6 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sat, 31 Jul 2021 18:04:03 -0400 Subject: [PATCH] Add serde support to Room struct. --- client/src/room.rs | 131 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 5 deletions(-) diff --git a/client/src/room.rs b/client/src/room.rs index 93f18dc..f30a61b 100644 --- a/client/src/room.rs +++ b/client/src/room.rs @@ -3,10 +3,21 @@ use std::error; use std::fmt; use std::mem; +use serde::{Deserialize, Serialize}; use solstice_proto::{server, User}; /// This enumeration is the list of possible membership states for a chat room. -#[derive(Clone, Copy, Debug, Eq, PartialEq, RustcDecodable, RustcEncodable)] +#[derive( + Clone, + Copy, + Debug, + Eq, + PartialEq, + RustcDecodable, + RustcEncodable, + Serialize, + Deserialize, +)] pub enum Membership { /// The user is not a member of this room. NonMember, @@ -22,7 +33,17 @@ pub enum Membership { /// This enumeration is the list of visibility types for rooms that the user is /// a member of. -#[derive(Clone, Copy, Debug, Eq, PartialEq, RustcDecodable, RustcEncodable)] +#[derive( + Clone, + Copy, + Debug, + Eq, + PartialEq, + RustcDecodable, + RustcEncodable, + Serialize, + Deserialize, +)] pub enum Visibility { /// This room is visible to any user. Public, @@ -33,7 +54,16 @@ pub enum Visibility { } /// This structure contains a chat room message. -#[derive(Clone, Debug, Eq, PartialEq, RustcDecodable, RustcEncodable)] +#[derive( + Clone, + Debug, + Eq, + PartialEq, + RustcDecodable, + RustcEncodable, + Serialize, + Deserialize, +)] pub struct Message { pub user_name: String, pub message: String, @@ -42,7 +72,16 @@ pub struct Message { /// This structure contains the last known information about a chat room. /// It does not store the name, as that is stored implicitly as the key in the /// room hash table. -#[derive(Clone, Debug, Eq, PartialEq, RustcDecodable, RustcEncodable)] +#[derive( + Clone, + Debug, + Eq, + PartialEq, + RustcDecodable, + RustcEncodable, + Serialize, + Deserialize, +)] pub struct Room { /// The membership state of the user for the room. pub membership: Membership, @@ -359,7 +398,89 @@ impl RoomMap { mod tests { use solstice_proto::server::RoomListResponse; - use super::{Room, RoomMap, Visibility}; + use super::{Membership, Message, Room, RoomMap, Visibility}; + + #[test] + fn deserialize_membership() { + assert_eq!( + serde_json::from_str::(r#""Member""#).unwrap(), + Membership::Member + ); + } + + #[test] + fn deserialize_visibility() { + assert_eq!( + serde_json::from_str::(r#""Public""#).unwrap(), + Visibility::Public + ); + } + + #[test] + fn deserialize_message() { + assert_eq!( + serde_json::from_str::( + r#"{ "user_name":"karandeep", "message":"namaste" }"# + ) + .unwrap(), + Message { + user_name: "karandeep".to_string(), + message: "namaste".to_string() + } + ); + } + + #[test] + fn deserialize_room() { + assert_eq!( + serde_json::from_str::( + r#"{ + "membership": "Joining", + "visibility": "PrivateOwned", + "operated": false, + "user_count": 3, + "owner": null, + "operators": ["op1", "op2"], + "members": ["m1", "m2"], + "messages": [ + { "user_name": "u1", "message": "msg1" }, + { "user_name": "u2", "message": "msg2" } + ], + "tickers": [["t11", "t12"], ["t21", "t22"]] + }"# + ) + .unwrap(), + Room { + membership: Membership::Joining, + visibility: Visibility::PrivateOwned, + operated: false, + user_count: 3, + owner: None, + operators: ["op1".to_string(), "op2".to_string()] + .iter() + .cloned() + .collect(), + members: ["m1".to_string(), "m2".to_string()] + .iter() + .cloned() + .collect(), + messages: vec![ + Message { + user_name: "u1".to_string(), + message: "msg1".to_string(), + }, + Message { + user_name: "u2".to_string(), + message: "msg2".to_string(), + } + ], + tickers: vec![ + ("t11".to_string(), "t12".to_string()), + ("t21".to_string(), "t22".to_string()), + ], + } + ); + } #[test] fn room_map_new_is_empty() {