From b4b3b537fd33fbf1155a5ad9407c3898a2d7bfa7 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 10 Jul 2024 20:23:29 +0300 Subject: [PATCH] feat: Websocket error macro `ws_errors` macro to make adding new errors easy Signed-off-by: Awiteb --- crates/oxidetalis/src/macros.rs | 59 +++++++++++++++++++++++ crates/oxidetalis/src/main.rs | 1 + crates/oxidetalis/src/websocket/errors.rs | 42 +++------------- 3 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 crates/oxidetalis/src/macros.rs diff --git a/crates/oxidetalis/src/macros.rs b/crates/oxidetalis/src/macros.rs new file mode 100644 index 0000000..26a8732 --- /dev/null +++ b/crates/oxidetalis/src/macros.rs @@ -0,0 +1,59 @@ +// OxideTalis Messaging Protocol homeserver implementation +// Copyright (C) 2024 OxideTalis Developers +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +//! OxideTalis server macros, to make the code more readable and easier to +//! write. + +/// Macro to create the `WsError` enum with the given error names and reasons. +/// +/// ## Example +/// ```rust,ignore +/// ws_errors! { +/// FirstError = "This is the first error", +/// SecondError = "This is the second error", +/// } +/// ``` +#[macro_export] +macro_rules! ws_errors { + ($($name:ident = $reason:tt),+ $(,)?) => { + #[derive(Debug)] + #[doc = "Websocket errors, returned in the websocket communication"] + pub enum WsError { + $( + #[doc = $reason] + $name + ),+ + } + impl WsError { + #[doc = "Returns error name"] + pub const fn name(&self) -> &'static str { + match self { + $( + WsError::$name => stringify!($name) + ),+ + } + } + #[doc = "Returns the error reason"] + pub const fn reason(&self) -> &'static str { + match self { + $( + WsError::$name => $reason + ),+ + } + } + } + }; +} diff --git a/crates/oxidetalis/src/main.rs b/crates/oxidetalis/src/main.rs index 53ad66d..b15cfb5 100644 --- a/crates/oxidetalis/src/main.rs +++ b/crates/oxidetalis/src/main.rs @@ -26,6 +26,7 @@ use salvo::{conn::TcpListener, Listener, Server}; mod database; mod errors; mod extensions; +mod macros; mod middlewares; mod nonce; mod routes; diff --git a/crates/oxidetalis/src/websocket/errors.rs b/crates/oxidetalis/src/websocket/errors.rs index b05e550..e2fab4b 100644 --- a/crates/oxidetalis/src/websocket/errors.rs +++ b/crates/oxidetalis/src/websocket/errors.rs @@ -16,42 +16,14 @@ //! Websocket errors +use crate::ws_errors; + /// Result type of websocket pub type WsResult = Result; -/// Websocket errors, returned in the websocket communication -#[derive(Debug)] -pub enum WsError { - /// The signature is invalid - InvalidSignature, - /// Message type must be text - NotTextMessage, - /// Invalid json data - InvalidJsonData, - /// Unknown client event - UnknownClientEvent, -} - -impl WsError { - /// Returns error name - pub const fn name(&self) -> &'static str { - match self { - WsError::InvalidSignature => "InvalidSignature", - WsError::NotTextMessage => "NotTextMessage", - WsError::InvalidJsonData => "InvalidJsonData", - WsError::UnknownClientEvent => "UnknownClientEvent", - } - } - - /// Returns the error reason - pub const fn reason(&self) -> &'static str { - match self { - WsError::InvalidSignature => "Invalid event signature", - WsError::NotTextMessage => "The websocket message must be text message", - WsError::InvalidJsonData => "Received invalid json data, the text must be valid json", - WsError::UnknownClientEvent => { - "Unknown client event, the event is not recognized by the server" - } - } - } +ws_errors! { + InvalidSignature = "Invalid event signature", + NotTextMessage = "The websocket message must be text message", + InvalidJsonData = "Received invalid json data, the text must be valid json", + UnknownClientEvent = "Unknown client event, the event is not recognized by the server", }