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:
commit
81eda95ac0
|
@ -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> {
|
||||||
|
|
|
@ -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:
|
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::instance::Instance;
|
||||||
use chorus::UrlBundle;
|
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
|
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:
|
manipulate the account. Assuming you already have an account on the server, you can log in like this:
|
||||||
|
|
||||||
```
|
```rs
|
||||||
use chorus::types::LoginSchema;
|
use chorus::types::LoginSchema;
|
||||||
// Assume, you already have an account created on this instance. Registering an account works
|
// 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.
|
// 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"))]
|
#[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::{Deserialize, Serialize};
|
||||||
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
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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>,
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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::*;
|
||||||
|
|
Loading…
Reference in New Issue