2024-01-30 17:19:34 +01:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2023-11-14 11:13:02 +01:00
|
|
|
use crate::types;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2024-01-31 22:27:53 +01:00
|
|
|
/// Represents a message received from the gateway. This will be either a [types::GatewayReceivePayload], containing events, or a [GatewayError].
|
2023-11-14 11:13:02 +01:00
|
|
|
/// This struct is used internally when handling messages.
|
|
|
|
#[derive(Clone, Debug)]
|
2023-11-19 17:08:53 +01:00
|
|
|
pub struct GatewayMessage(pub String);
|
2023-11-14 11:13:02 +01:00
|
|
|
|
|
|
|
impl GatewayMessage {
|
|
|
|
/// Parses the message as an error;
|
2024-01-31 22:27:53 +01:00
|
|
|
/// Returns the error if successfully parsed, None if the message isn't an error
|
2023-11-14 11:13:02 +01:00
|
|
|
pub fn error(&self) -> Option<GatewayError> {
|
|
|
|
// Some error strings have dots on the end, which we don't care about
|
2023-11-19 17:08:53 +01:00
|
|
|
let processed_content = self.0.to_lowercase().replace('.', "");
|
2023-11-14 11:13:02 +01:00
|
|
|
|
|
|
|
match processed_content.as_str() {
|
|
|
|
"unknown error" | "4000" => Some(GatewayError::Unknown),
|
|
|
|
"unknown opcode" | "4001" => Some(GatewayError::UnknownOpcode),
|
|
|
|
"decode error" | "error while decoding payload" | "4002" => Some(GatewayError::Decode),
|
|
|
|
"not authenticated" | "4003" => Some(GatewayError::NotAuthenticated),
|
|
|
|
"authentication failed" | "4004" => Some(GatewayError::AuthenticationFailed),
|
|
|
|
"already authenticated" | "4005" => Some(GatewayError::AlreadyAuthenticated),
|
|
|
|
"invalid seq" | "4007" => Some(GatewayError::InvalidSequenceNumber),
|
|
|
|
"rate limited" | "4008" => Some(GatewayError::RateLimited),
|
|
|
|
"session timed out" | "4009" => Some(GatewayError::SessionTimedOut),
|
|
|
|
"invalid shard" | "4010" => Some(GatewayError::InvalidShard),
|
|
|
|
"sharding required" | "4011" => Some(GatewayError::ShardingRequired),
|
|
|
|
"invalid api version" | "4012" => Some(GatewayError::InvalidAPIVersion),
|
|
|
|
"invalid intent(s)" | "invalid intent" | "4013" => Some(GatewayError::InvalidIntents),
|
|
|
|
"disallowed intent(s)" | "disallowed intents" | "4014" => {
|
|
|
|
Some(GatewayError::DisallowedIntents)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the message as a payload;
|
|
|
|
/// Returns a result of deserializing
|
|
|
|
pub fn payload(&self) -> Result<types::GatewayReceivePayload, serde_json::Error> {
|
2023-11-19 17:46:49 +01:00
|
|
|
serde_json::from_str(&self.0)
|
2023-11-14 11:13:02 +01:00
|
|
|
}
|
|
|
|
}
|