From 620285d1d155b8ea270b6d5cb7ff073bef001435 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 11 Feb 2022 22:36:42 +0100 Subject: [PATCH] Avoid baking error reasons into protocol --- cove-core/src/packets.rs | 18 +++++++++++++----- cove-server/src/main.rs | 13 ++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cove-core/src/packets.rs b/cove-core/src/packets.rs index bcf67d8..4ba1e4a 100644 --- a/cove-core/src/packets.rs +++ b/cove-core/src/packets.rs @@ -5,6 +5,7 @@ use crate::{Message, MessageId, User}; #[derive(Debug, Deserialize, Serialize)] pub struct HelloCmd { + pub room: String, pub nick: String, pub identity: String, } @@ -17,8 +18,15 @@ pub enum HelloRpl { others: Vec, last_message: MessageId, }, - NickTooLong, - IdentityTooLong, + InvalidRoom { + reason: String, + }, + InvalidNick { + reason: String, + }, + InvalidIdentity { + reason: String, + }, } #[derive(Debug, Deserialize, Serialize)] @@ -30,7 +38,7 @@ pub struct NickCmd { #[serde(tag = "type")] pub enum NickRpl { Success, - NickTooLong, + InvalidNick { reason: String }, } #[derive(Debug, Deserialize, Serialize)] @@ -43,8 +51,8 @@ pub struct SendCmd { #[serde(tag = "type")] pub enum SendRpl { Success { message: Message }, - NickTooLong, - ContentTooLong, + InvalidNick { reason: String }, + InvalidContent { reason: String }, } #[derive(Debug, Deserialize, Serialize)] diff --git a/cove-server/src/main.rs b/cove-server/src/main.rs index fc8fec7..ab1ec9b 100644 --- a/cove-server/src/main.rs +++ b/cove-server/src/main.rs @@ -25,6 +25,7 @@ async fn main() { serde_json::to_string_pretty(&Packet::Cmd { id: 12345, cmd: Cmd::Hello(HelloCmd { + room: "welcome".to_string(), nick: "Garmy".to_string(), identity: "random garbage".to_string() }) @@ -47,7 +48,9 @@ async fn main() { "{}", serde_json::to_string_pretty(&Packet::Rpl { id: 67890, - rpl: Rpl::Hello(HelloRpl::NickTooLong) + rpl: Rpl::Hello(HelloRpl::InvalidNick { + reason: "foo".to_string() + }) }) .unwrap() ); @@ -73,7 +76,9 @@ async fn main() { "{}", serde_json::to_string_pretty(&Packet::Rpl { id: 67890, - rpl: Rpl::Nick(NickRpl::NickTooLong) + rpl: Rpl::Nick(NickRpl::InvalidNick { + reason: "foo".to_string() + }) }) .unwrap() ); @@ -103,7 +108,9 @@ async fn main() { "{}", serde_json::to_string_pretty(&Packet::Rpl { id: 67890, - rpl: Rpl::Send(SendRpl::ContentTooLong) + rpl: Rpl::Send(SendRpl::InvalidContent { + reason: "foo".to_string() + }) }) .unwrap() );