Avoid baking error reasons into protocol

This commit is contained in:
Joscha 2022-02-11 22:36:42 +01:00
parent 0e6acdbff4
commit 620285d1d1
2 changed files with 23 additions and 8 deletions

View file

@ -5,6 +5,7 @@ use crate::{Message, MessageId, User};
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct HelloCmd { pub struct HelloCmd {
pub room: String,
pub nick: String, pub nick: String,
pub identity: String, pub identity: String,
} }
@ -17,8 +18,15 @@ pub enum HelloRpl {
others: Vec<User>, others: Vec<User>,
last_message: MessageId, last_message: MessageId,
}, },
NickTooLong, InvalidRoom {
IdentityTooLong, reason: String,
},
InvalidNick {
reason: String,
},
InvalidIdentity {
reason: String,
},
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -30,7 +38,7 @@ pub struct NickCmd {
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum NickRpl { pub enum NickRpl {
Success, Success,
NickTooLong, InvalidNick { reason: String },
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -43,8 +51,8 @@ pub struct SendCmd {
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum SendRpl { pub enum SendRpl {
Success { message: Message }, Success { message: Message },
NickTooLong, InvalidNick { reason: String },
ContentTooLong, InvalidContent { reason: String },
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]

View file

@ -25,6 +25,7 @@ async fn main() {
serde_json::to_string_pretty(&Packet::Cmd { serde_json::to_string_pretty(&Packet::Cmd {
id: 12345, id: 12345,
cmd: Cmd::Hello(HelloCmd { cmd: Cmd::Hello(HelloCmd {
room: "welcome".to_string(),
nick: "Garmy".to_string(), nick: "Garmy".to_string(),
identity: "random garbage".to_string() identity: "random garbage".to_string()
}) })
@ -47,7 +48,9 @@ async fn main() {
"{}", "{}",
serde_json::to_string_pretty(&Packet::Rpl { serde_json::to_string_pretty(&Packet::Rpl {
id: 67890, id: 67890,
rpl: Rpl::Hello(HelloRpl::NickTooLong) rpl: Rpl::Hello(HelloRpl::InvalidNick {
reason: "foo".to_string()
})
}) })
.unwrap() .unwrap()
); );
@ -73,7 +76,9 @@ async fn main() {
"{}", "{}",
serde_json::to_string_pretty(&Packet::Rpl { serde_json::to_string_pretty(&Packet::Rpl {
id: 67890, id: 67890,
rpl: Rpl::Nick(NickRpl::NickTooLong) rpl: Rpl::Nick(NickRpl::InvalidNick {
reason: "foo".to_string()
})
}) })
.unwrap() .unwrap()
); );
@ -103,7 +108,9 @@ async fn main() {
"{}", "{}",
serde_json::to_string_pretty(&Packet::Rpl { serde_json::to_string_pretty(&Packet::Rpl {
id: 67890, id: 67890,
rpl: Rpl::Send(SendRpl::ContentTooLong) rpl: Rpl::Send(SendRpl::InvalidContent {
reason: "foo".to_string()
})
}) })
.unwrap() .unwrap()
); );