Add Default, Hash, etc. where needed

This commit is contained in:
bitfl0wer 2023-12-02 17:35:47 +01:00
parent 4cfeee8d86
commit 7ef61eb317
9 changed files with 48 additions and 11 deletions

View File

@ -15,22 +15,60 @@ use crate::types::types::subconfigs::limits::rates::RateLimits;
use crate::types::{GeneralConfiguration, Limit, LimitType, User, UserSettings}; use crate::types::{GeneralConfiguration, Limit, LimitType, User, UserSettings};
use crate::UrlBundle; use crate::UrlBundle;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
/// The [`Instance`]; what you will be using to perform all sorts of actions on the Spacebar server. /// The [`Instance`]; what you will be using to perform all sorts of actions on the Spacebar server.
/// If `limits_information` is `None`, then the instance will not be rate limited. /// If `limits_information` is `None`, then the instance will not be rate limited.
pub struct Instance { pub struct Instance {
pub urls: UrlBundle, pub urls: UrlBundle,
pub instance_info: GeneralConfiguration, pub instance_info: GeneralConfiguration,
pub limits_information: Option<LimitsInformation>, pub limits_information: Option<LimitsInformation>,
#[serde(skip)]
pub client: Client, pub client: Client,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Default)] impl PartialEq for Instance {
fn eq(&self, other: &Self) -> bool {
self.urls == other.urls
&& self.instance_info == other.instance_info
&& self.limits_information == other.limits_information
}
}
impl Eq for Instance {}
impl std::hash::Hash for Instance {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.urls.hash(state);
self.instance_info.hash(state);
if let Some(inf) = &self.limits_information {
inf.hash(state);
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq)]
pub struct LimitsInformation { pub struct LimitsInformation {
pub ratelimits: HashMap<LimitType, Limit>, pub ratelimits: HashMap<LimitType, Limit>,
pub configuration: RateLimits, pub configuration: RateLimits,
} }
impl std::hash::Hash for LimitsInformation {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
for (k, v) in self.ratelimits.iter() {
k.hash(state);
v.hash(state);
}
self.configuration.hash(state);
}
}
impl PartialEq for LimitsInformation {
fn eq(&self, other: &Self) -> bool {
self.ratelimits.iter().eq(other.ratelimits.iter())
&& self.configuration == other.configuration
}
}
impl Instance { impl Instance {
/// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle), where `limited` is whether or not to automatically use rate limits. /// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle), where `limited` is whether or not to automatically use rate limits.
pub async fn new(urls: UrlBundle, limited: bool) -> ChorusResult<Instance> { pub async fn new(urls: UrlBundle, limited: bool) -> ChorusResult<Instance> {

View File

@ -104,6 +104,7 @@ This crate uses Semantic Versioning 2.0.0 as its versioning scheme. You can read
#[cfg(all(feature = "rt", feature = "rt_multi_thread"))] #[cfg(all(feature = "rt", feature = "rt_multi_thread"))]
compile_error!("feature \"rt\" and feature \"rt_multi_thread\" cannot be enabled at the same time"); compile_error!("feature \"rt\" and feature \"rt_multi_thread\" cannot be enabled at the same time");
use serde::{Serialize, Deserialize};
use url::{ParseError, Url}; use url::{ParseError, Url};
#[cfg(feature = "client")] #[cfg(feature = "client")]
@ -119,7 +120,7 @@ pub mod types;
#[cfg(feature = "client")] #[cfg(feature = "client")]
pub mod voice; pub mod voice;
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Default, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// A URLBundle bundles together the API-, Gateway- and CDN-URLs of a Spacebar instance. /// A URLBundle bundles together the API-, Gateway- and CDN-URLs of a Spacebar instance.
/// ///
/// # Notes /// # Notes

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::types::utils::Snowflake; use crate::types::utils::Snowflake;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GeneralConfiguration { pub struct GeneralConfiguration {
pub instance_name: String, pub instance_name: String,

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::types::config::types::subconfigs::limits::ratelimits::RateLimitOptions; use crate::types::config::types::subconfigs::limits::ratelimits::RateLimitOptions;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct AuthRateLimit { pub struct AuthRateLimit {
pub login: RateLimitOptions, pub login: RateLimitOptions,
pub register: RateLimitOptions, pub register: RateLimitOptions,

View File

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
pub mod auth; pub mod auth;
pub mod route; pub mod route;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct RateLimitOptions { pub struct RateLimitOptions {
pub bot: Option<u64>, pub bot: Option<u64>,

View File

@ -4,7 +4,7 @@ use crate::types::config::types::subconfigs::limits::ratelimits::{
auth::AuthRateLimit, RateLimitOptions, auth::AuthRateLimit, RateLimitOptions,
}; };
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct RouteRateLimit { pub struct RouteRateLimit {
pub guild: RateLimitOptions, pub guild: RateLimitOptions,
pub webhook: RateLimitOptions, pub webhook: RateLimitOptions,

View File

@ -7,7 +7,7 @@ use crate::types::{
LimitType, LimitType,
}; };
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct RateLimits { pub struct RateLimits {
pub enabled: bool, pub enabled: bool,
pub ip: RateLimitOptions, pub ip: RateLimitOptions,

View File

@ -25,7 +25,7 @@ pub enum LimitType {
/// A struct that represents the current ratelimits, either instance-wide or user-wide. /// A struct that represents the current ratelimits, either instance-wide or user-wide.
/// See <https://discord.com/developers/docs/topics/rate-limits#rate-limits> for more information. /// See <https://discord.com/developers/docs/topics/rate-limits#rate-limits> for more information.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct Limit { pub struct Limit {
pub bucket: LimitType, pub bucket: LimitType,
pub limit: u64, pub limit: u64,

View File

@ -1,5 +1,3 @@
use std::borrow::BorrowMut;
use chorus::types::{LoginSchema, RegisterSchema}; use chorus::types::{LoginSchema, RegisterSchema};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*; use wasm_bindgen_test::*;