Browse Source

Introduce serde for ControlRequest.

wip
Titouan Rigoudy 4 years ago
parent
commit
46b7fed632
3 changed files with 110 additions and 2 deletions
  1. +45
    -0
      Cargo.lock
  2. +2
    -0
      client/Cargo.toml
  3. +63
    -2
      client/src/control/request.rs

+ 45
- 0
Cargo.lock View File

@ -489,6 +489,12 @@ dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -1046,6 +1052,12 @@ dependencies = [
"semver",
]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "scoped-tls"
version = "0.1.2"
@ -1073,6 +1085,37 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "sha1"
version = "0.1.1"
@ -1138,6 +1181,8 @@ dependencies = [
"mio 0.6.23",
"parking_lot 0.8.0",
"rustc-serialize",
"serde",
"serde_json",
"slab 0.2.0",
"solstice-proto",
"threadpool",


+ 2
- 0
client/Cargo.toml View File

@ -15,6 +15,8 @@ log = "^0.4"
mio = "^0.6"
parking_lot = "^0.8"
rustc-serialize = "^0.3.17"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
slab = "^0.2"
solstice-proto = { path = "../proto" }
threadpool = "^1.0"


+ 63
- 2
client/src/control/request.rs View File

@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
/// This enumeration is the list of possible control requests made by the
/// controller client to the client.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(Debug, RustcDecodable, RustcEncodable, Serialize, Deserialize)]
pub enum Request {
/// The controller wants to join a room. Contains the room name.
RoomJoinRequest(String),
@ -17,10 +19,69 @@ pub enum Request {
}
/// This structure contains the chat room message request from the controller.
#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(Debug, RustcDecodable, RustcEncodable, 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 serialize_room_join_request() {
assert_eq!(
serde_json::to_string(&Request::RoomJoinRequest("bleep".to_string()))
.unwrap(),
r#"{"RoomJoinRequest":"bleep"}"#
);
}
#[test]
fn serialize_room_leave_request() {
assert_eq!(
serde_json::to_string(&Request::RoomLeaveRequest("bleep".to_string()))
.unwrap(),
r#"{"RoomLeaveRequest":"bleep"}"#
);
}
#[test]
fn serialize_login_status_request() {
assert_eq!(
serde_json::to_string(&Request::LoginStatusRequest).unwrap(),
r#""LoginStatusRequest""#
);
}
#[test]
fn serialize_room_list_request() {
assert_eq!(
serde_json::to_string(&Request::RoomListRequest).unwrap(),
r#""RoomListRequest""#
);
}
#[test]
fn serialize_user_list_request() {
assert_eq!(
serde_json::to_string(&Request::UserListRequest).unwrap(),
r#""UserListRequest""#
);
}
#[test]
fn serialize_room_message_request() {
assert_eq!(
serde_json::to_string(&Request::RoomMessageRequest(RoomMessageRequest {
room_name: "bleep".to_string(),
message: "heyo".to_string(),
}))
.unwrap(),
r#"{"RoomMessageRequest":{"room_name":"bleep","message":"heyo"}}"#
);
}
}

Loading…
Cancel
Save