From baf77bb6be88e796683bc21fd16a0359e68c7791 Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 18 Jan 2020 17:25:45 +0100 Subject: [PATCH] simplify json serialization code --- server/src/websocket/server.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 2d9ddcd4..9afbff25 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -295,18 +295,21 @@ impl Handler for ChatServer { } } +#[derive(Serialize)] +struct WebsocketResponse { + op: String, + data: T, +} + fn to_json_string(op: &UserOperation, data: T) -> Result where T: Serialize, { - let mut json = serde_json::to_value(&data)?; - match json.as_object_mut() { - Some(j) => j.insert("op".to_string(), serde_json::to_value(op.to_string())?), - None => return Err(format_err!("")), + let response = WebsocketResponse { + op: op.to_string(), + data, }; - // TODO: it seems like this is never called? - let x = serde_json::to_string(&json)?; - Ok(x) + Ok(serde_json::to_string(&response)?) } fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result {