Browse Source

Add serde support to Room struct.

wip
Titouan Rigoudy 4 years ago
parent
commit
6c8b17fd19
1 changed files with 126 additions and 5 deletions
  1. +126
    -5
      client/src/room.rs

+ 126
- 5
client/src/room.rs View File

@ -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::<Membership>(r#""Member""#).unwrap(),
Membership::Member
);
}
#[test]
fn deserialize_visibility() {
assert_eq!(
serde_json::from_str::<Visibility>(r#""Public""#).unwrap(),
Visibility::Public
);
}
#[test]
fn deserialize_message() {
assert_eq!(
serde_json::from_str::<Message>(
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::<Room>(
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() {


Loading…
Cancel
Save