Browse Source

Add serde support for control responses.

Start writing tests, add TODO for the others.
wip
Titouan Rigoudy 4 years ago
parent
commit
2227cafe0a
1 changed files with 104 additions and 10 deletions
  1. +104
    -10
      client/src/control/response.rs

+ 104
- 10
client/src/control/response.rs View File

@ -1,9 +1,12 @@
use crate::room;
use serde::{Deserialize, Serialize};
use solstice_proto::User;
/// This enumeration is the list of possible control responses from the client
/// to the controller.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub enum Response {
LoginStatusResponse(LoginStatusResponse),
RoomJoinResponse(RoomJoinResponse),
@ -16,19 +19,25 @@ pub enum Response {
UserListResponse(UserListResponse),
}
#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(
Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Serialize, Deserialize,
)]
pub struct RoomJoinResponse {
pub room_name: String,
}
#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(
Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Serialize, Deserialize,
)]
pub struct RoomLeaveResponse {
pub room_name: String,
}
/// This enumeration is the list of possible login states, and the associated
/// information.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub enum LoginStatusResponse {
/// The login request has been sent to the server, but the response hasn't
/// been received yet.
@ -56,7 +65,9 @@ pub enum LoginStatusResponse {
/// This structure contains the list of all visible rooms, and their associated
/// data.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct RoomListResponse {
/// The list of (room name, room data) pairs.
pub rooms: Vec<(String, room::Room)>,
@ -64,7 +75,9 @@ pub struct RoomListResponse {
/// This structure contains a message said in a chat room the user is a member
/// of.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct RoomMessageResponse {
/// The name of the room in which the message was said.
pub room_name: String,
@ -75,28 +88,109 @@ pub struct RoomMessageResponse {
}
/// This struct describes the fact that the given user joined the given room.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct RoomUserJoinedResponse {
pub room_name: String,
pub user_name: String,
}
/// This struct describes the fact that the given user left the given room.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct RoomUserLeftResponse {
pub room_name: String,
pub user_name: String,
}
/// This struct contains the last known information about a given user.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct UserInfoResponse {
pub user_name: String,
pub user_info: User,
}
/// This stuct contains the last known information about every user.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(
Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize,
)]
pub struct UserListResponse {
pub user_list: Vec<(String, User)>,
}
#[cfg(test)]
mod tests {
use super::{
LoginStatusResponse, Response, RoomJoinResponse, RoomLeaveResponse,
RoomListResponse, RoomMessageResponse, RoomUserJoinedResponse,
RoomUserLeftResponse, UserInfoResponse, UserListResponse,
};
#[test]
fn deserialize_login_status_response_pending() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Pending": { "username": "karandeep" }
}"#
)
.unwrap(),
LoginStatusResponse::Pending {
username: "karandeep".to_string()
}
);
}
#[test]
fn deserialize_login_status_response_success() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Success": {
"username": "karandeep",
"motd": "message of the day"
}
}"#
)
.unwrap(),
LoginStatusResponse::Success {
username: "karandeep".to_string(),
motd: "message of the day".to_string(),
}
);
}
#[test]
fn deserialize_login_status_response_failure() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Failure": {
"username": "karandeep",
"reason": "garbage!"
}
}"#
)
.unwrap(),
LoginStatusResponse::Failure {
username: "karandeep".to_string(),
reason: "garbage!".to_string(),
}
);
}
// TODO: Write tests for the other variants:
//
// - RoomJoinResponse
// - RoomLeaveResponse
// - RoomListResponse
// - RoomMessageResponse
// - RoomUserJoinedResponse
// - RoomUserLeftResponse
// - UserInfoResponse
// - UserListResponse
}

Loading…
Cancel
Save