use serde::{Deserialize, Serialize}; /// This enumeration is the list of possible control requests made by the /// controller client to the client. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Request { /// The controller wants to connect to a peer. Contains the peer name. PeerConnectRequest(String), /// The controller wants to join a room. Contains the room name. RoomJoinRequest(String), /// The controller wants to leave a room. Contains the room name. RoomLeaveRequest(String), /// The controller wants to know what the login status is. LoginStatusRequest, /// The controller wants to know the list of visible chat rooms. RoomListRequest, /// The controller wants to send a message to a chat room. RoomMessageRequest(RoomMessageRequest), /// The controller wants to know the list of known users. UserListRequest, } impl From for Request { fn from(request: RoomMessageRequest) -> Self { Self::RoomMessageRequest(request) } } /// This structure contains the chat room message request from the controller. #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct RoomMessageRequest { /// The name of the chat room in which to send the message. pub room_name: String, /// The message to be said. pub message: String, } #[cfg(test)] mod tests { use super::{Request, RoomMessageRequest}; #[test] fn deserialize_room_join_request() { assert_eq!( serde_json::from_str::(r#"{"RoomJoinRequest": "bleep"}"#) .unwrap(), Request::RoomJoinRequest("bleep".to_string()) ); } #[test] fn deserialize_room_leave_request() { assert_eq!( serde_json::from_str::(r#"{"RoomLeaveRequest": "bleep"}"#) .unwrap(), Request::RoomLeaveRequest("bleep".to_string()) ); } #[test] fn deserialize_login_status_request() { assert_eq!( serde_json::from_str::(r#""LoginStatusRequest""#).unwrap(), Request::LoginStatusRequest ); } #[test] fn deserialize_room_list_request() { assert_eq!( serde_json::from_str::(r#""RoomListRequest""#).unwrap(), Request::RoomListRequest ); } #[test] fn deserialize_user_list_request() { assert_eq!( serde_json::from_str::(r#""UserListRequest""#).unwrap(), Request::UserListRequest ); } #[test] fn deserialize_room_message_request() { assert_eq!( serde_json::from_str::( r#"{ "RoomMessageRequest": { "room_name":"bleep", "message":"heyo" } }"# ) .unwrap(), Request::RoomMessageRequest(RoomMessageRequest { room_name: "bleep".to_string(), message: "heyo".to_string(), }) ); } }