More derives and impls (#448)

Add some sensible implementations for PartialEq, Eq, etc. where I found
them to be necessary
This commit is contained in:
Flori 2023-12-02 17:52:31 +01:00 committed by GitHub
commit 741b6d6978
9 changed files with 50 additions and 13 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::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.
/// If `limits_information` is `None`, then the instance will not be rate limited.
pub struct Instance {
pub urls: UrlBundle,
pub instance_info: GeneralConfiguration,
pub limits_information: Option<LimitsInformation>,
#[serde(skip)]
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 ratelimits: HashMap<LimitType, Limit>,
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 {
/// 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> {

View File

@ -8,7 +8,7 @@ instead of worrying about the underlying implementation details.
To connect to a Spacebar compatible server, you need to create an [`Instance`](https://docs.rs/chorus/latest/chorus/instance/struct.Instance.html) like this:
```
```rs
use chorus::instance::Instance;
use chorus::UrlBundle;
@ -35,7 +35,7 @@ This Instance can now be used to log in, register and from there on, interact wi
Logging in correctly provides you with an instance of [`ChorusUser`](https://docs.rs/chorus/latest/chorus/instance/struct.ChorusUser.html), with which you can interact with the server and
manipulate the account. Assuming you already have an account on the server, you can log in like this:
```
```rs
use chorus::types::LoginSchema;
// Assume, you already have an account created on this instance. Registering an account works
// the same way, but you'd use the Register-specific Structs and methods instead.
@ -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"))]
compile_error!("feature \"rt\" and feature \"rt_multi_thread\" cannot be enabled at the same time");
use serde::{Deserialize, Serialize};
use url::{ParseError, Url};
#[cfg(feature = "client")]
@ -119,7 +120,7 @@ pub mod types;
#[cfg(feature = "client")]
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.
///
/// # Notes

View File

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

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
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 login: RateLimitOptions,
pub register: RateLimitOptions,

View File

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

View File

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

View File

@ -7,7 +7,7 @@ use crate::types::{
LimitType,
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct RateLimits {
pub enabled: bool,
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.
/// 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 bucket: LimitType,
pub limit: u64,

View File

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