From 27d0539a4193483e36b54326d5b9e52724f97536 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sat, 31 Jul 2021 19:24:41 -0400 Subject: [PATCH] Replace rustc-serialize with serde. --- Cargo.lock | 3 +- client/Cargo.toml | 2 +- client/src/control/request.rs | 8 ++--- client/src/control/response.rs | 40 ++++++----------------- client/src/control/ws.rs | 60 +++++----------------------------- client/src/room.rs | 46 +++----------------------- proto/Cargo.toml | 1 - proto/src/core/user.rs | 23 ++----------- 8 files changed, 28 insertions(+), 155 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40afa3f..9034004 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,11 +1180,11 @@ dependencies = [ "log 0.4.14", "mio 0.6.23", "parking_lot 0.8.0", - "rustc-serialize", "serde", "serde_json", "slab 0.2.0", "solstice-proto", + "thiserror", "threadpool", "tokio 1.8.1", "tokio-codec", @@ -1209,7 +1209,6 @@ dependencies = [ "mio 0.6.23", "parking_lot 0.8.0", "rust-crypto", - "rustc-serialize", "serde", "serde_json", "slab 0.2.0", diff --git a/client/Cargo.toml b/client/Cargo.toml index a95a330..a0b610c 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -14,11 +14,11 @@ futures = "0.3" 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" } +thiserror = "1.0" threadpool = "^1.0" tokio = { version = "1", features = ["full"] } tokio-core = "^0.1" diff --git a/client/src/control/request.rs b/client/src/control/request.rs index 44e9f52..ec9eef6 100644 --- a/client/src/control/request.rs +++ b/client/src/control/request.rs @@ -2,9 +2,7 @@ use serde::{Deserialize, Serialize}; /// This enumeration is the list of possible control requests made by the /// controller client to the client. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Request { /// The controller wants to join a room. Contains the room name. RoomJoinRequest(String), @@ -21,9 +19,7 @@ pub enum Request { } /// This structure contains the chat room message request from the controller. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Deserialize, Serialize, -)] +#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct RoomMessageRequest { /// The name of the chat room in which to send the message. pub room_name: String, diff --git a/client/src/control/response.rs b/client/src/control/response.rs index 1ac111a..1772f15 100644 --- a/client/src/control/response.rs +++ b/client/src/control/response.rs @@ -4,9 +4,7 @@ use solstice_proto::User; /// This enumeration is the list of possible control responses from the client /// to the controller. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Response { LoginStatusResponse(LoginStatusResponse), RoomJoinResponse(RoomJoinResponse), @@ -19,25 +17,19 @@ pub enum Response { UserListResponse(UserListResponse), } -#[derive( - Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RoomJoinResponse { pub room_name: String, } -#[derive( - Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RoomLeaveResponse { pub room_name: String, } /// This enumeration is the list of possible login states, and the associated /// information. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum LoginStatusResponse { /// The login request has been sent to the server, but the response hasn't /// been received yet. @@ -65,9 +57,7 @@ pub enum LoginStatusResponse { /// This structure contains the list of all visible rooms, and their associated /// data. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RoomListResponse { /// The list of (room name, room data) pairs. pub rooms: Vec<(String, room::Room)>, @@ -75,9 +65,7 @@ pub struct RoomListResponse { /// This structure contains a message said in a chat room the user is a member /// of. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RoomMessageResponse { /// The name of the room in which the message was said. pub room_name: String, @@ -88,36 +76,28 @@ pub struct RoomMessageResponse { } /// This struct describes the fact that the given user joined the given room. -#[derive( - Debug, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, 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, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, 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, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, 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, PartialEq, Eq, RustcDecodable, RustcEncodable, Serialize, Deserialize, -)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserListResponse { pub user_list: Vec<(String, User)>, } diff --git a/client/src/control/ws.rs b/client/src/control/ws.rs index 4dc5cdf..1bb482f 100644 --- a/client/src/control/ws.rs +++ b/client/src/control/ws.rs @@ -1,9 +1,6 @@ -use std::error; -use std::fmt; - use crossbeam_channel; -use rustc_serialize::json; use solstice_proto::config; +use thiserror::Error; use ws; use super::request::*; @@ -25,53 +22,12 @@ pub enum Notification { } /// This error is returned when a `Sender` fails to send a control request. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum SendError { - /// Error encoding the control request. - JSONEncoderError(json::EncoderError), - /// Error sending the encoded control request to the websocket. - WebSocketError(ws::Error), -} - -impl fmt::Display for SendError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match *self { - SendError::JSONEncoderError(ref err) => { - write!(fmt, "JSONEncoderError: {}", err) - } - SendError::WebSocketError(ref err) => { - write!(fmt, "WebSocketError: {}", err) - } - } - } -} - -impl error::Error for SendError { - fn description(&self) -> &str { - match *self { - SendError::JSONEncoderError(_) => "JSONEncoderError", - SendError::WebSocketError(_) => "WebSocketError", - } - } - - fn cause(&self) -> Option<&dyn error::Error> { - match *self { - SendError::JSONEncoderError(ref err) => Some(err), - SendError::WebSocketError(ref err) => Some(err), - } - } -} - -impl From for SendError { - fn from(err: json::EncoderError) -> Self { - SendError::JSONEncoderError(err) - } -} - -impl From for SendError { - fn from(err: ws::Error) -> Self { - SendError::WebSocketError(err) - } + #[error("error encoding response to JSON: {0}")] + JsonError(#[from] serde_json::Error), + #[error("error sending response on socket: {0}")] + WebSocketError(#[from] ws::Error), } /// This struct is used to send control responses to the controller. @@ -85,7 +41,7 @@ pub struct Sender { impl Sender { /// Queues up a control response to be sent to the controller. pub fn send(&mut self, response: Response) -> Result<(), SendError> { - let encoded = json::encode(&response)?; + let encoded = serde_json::to_string(&response)?; self.sender.send(encoded)?; Ok(()) } @@ -141,7 +97,7 @@ impl ws::Handler for Handler { }; // Decode the json control request. - let control_request = match json::decode(&payload) { + let control_request = match serde_json::from_str(&payload) { Ok(control_request) => control_request, Err(e) => { error!("Received invalid JSON message from controller: {}", e); diff --git a/client/src/room.rs b/client/src/room.rs index 5a0332e..f7cfbe5 100644 --- a/client/src/room.rs +++ b/client/src/room.rs @@ -7,17 +7,7 @@ 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, - Serialize, - Deserialize, -)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] pub enum Membership { /// The user is not a member of this room. NonMember, @@ -33,17 +23,7 @@ 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, - Serialize, - Deserialize, -)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] pub enum Visibility { /// This room is visible to any user. Public, @@ -54,16 +34,7 @@ pub enum Visibility { } /// This structure contains a chat room message. -#[derive( - Clone, - Debug, - Eq, - PartialEq, - RustcDecodable, - RustcEncodable, - Serialize, - Deserialize, -)] +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Message { pub user_name: String, pub message: String, @@ -72,16 +43,7 @@ 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, - Serialize, - Deserialize, -)] +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Room { /// The membership state of the user for the room. pub membership: Membership, diff --git a/proto/Cargo.toml b/proto/Cargo.toml index a2ebd1b..175fb64 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -17,7 +17,6 @@ log = "^0.4" mio = "^0.6" parking_lot = "^0.8" rust-crypto = "^0.2.34" -rustc-serialize = "^0.3.17" serde = { version = "1.0", features = ["derive"] } slab = "^0.2" thiserror = "^1.0" diff --git a/proto/src/core/user.rs b/proto/src/core/user.rs index cbd27da..034156d 100644 --- a/proto/src/core/user.rs +++ b/proto/src/core/user.rs @@ -16,17 +16,7 @@ const STATUS_ONLINE: u32 = 3; /// This enumeration is the list of possible user statuses. #[derive( - Clone, - Copy, - Debug, - Eq, - Ord, - PartialEq, - PartialOrd, - RustcDecodable, - RustcEncodable, - Serialize, - Deserialize, + Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, )] pub enum UserStatus { /// The user if offline. @@ -94,16 +84,7 @@ impl ValueDecode for UserStatus { /// This structure contains the last known information about a fellow user. #[derive( - Clone, - Debug, - Eq, - Ord, - PartialEq, - PartialOrd, - RustcDecodable, - RustcEncodable, - Serialize, - Deserialize, + Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, )] pub struct User { /// The name of the user.