From 6f2dac66950a08bbd8b0c4fd803e1ef8dc84b0b2 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 25 Apr 2023 17:32:30 +0200 Subject: [PATCH 1/3] Split up schemas.rs --- src/api/auth/login.rs | 3 +- src/api/auth/register.rs | 7 +- src/api/mod.rs | 2 + src/api/policies/instance/instance.rs | 6 +- src/api/schemas.rs | 605 +++++++++----------------- src/api/types.rs | 217 +++++++++ src/instance.rs | 8 +- 7 files changed, 425 insertions(+), 423 deletions(-) create mode 100644 src/api/types.rs diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index a11db3c..bc37b25 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -3,7 +3,8 @@ pub mod login { use serde_json::{from_str, json}; use crate::api::limits::LimitType; - use crate::api::schemas::schemas::{ErrorResponse, LoginResult, LoginSchema}; + use crate::api::schemas::LoginSchema; + use crate::api::types::{ErrorResponse, LoginResult}; use crate::errors::InstanceServerError; use crate::instance::Instance; diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 2e0db06..650b8cf 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -3,10 +3,7 @@ pub mod register { use serde_json::json; use crate::{ - api::{ - limits::LimitType, - schemas::schemas::{ErrorResponse, RegisterSchema}, - }, + api::{limits::LimitType, schemas::RegisterSchema, types::ErrorResponse}, errors::InstanceServerError, instance::{Instance, Token}, }; @@ -67,7 +64,7 @@ pub mod register { #[cfg(test)] mod test { - use crate::api::schemas::schemas::{AuthEmail, AuthPassword, AuthUsername, RegisterSchema}; + use crate::api::schemas::{AuthEmail, AuthPassword, AuthUsername, RegisterSchema}; use crate::errors::InstanceServerError; use crate::instance::Instance; use crate::limit::LimitedRequester; diff --git a/src/api/mod.rs b/src/api/mod.rs index f619259..a9902d6 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,7 +1,9 @@ pub mod auth; pub mod policies; pub mod schemas; +pub mod types; pub use policies::instance::instance::*; pub use policies::instance::limits::*; pub use schemas::*; +pub use types::*; diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 2eaac5b..7f69670 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -3,7 +3,7 @@ pub mod instance { use serde_json::from_str; use crate::errors::InstanceServerError; - use crate::{api::schemas::schemas::InstancePoliciesSchema, instance::Instance}; + use crate::{api::types::InstancePolicies, instance::Instance}; impl Instance { /** @@ -13,7 +13,7 @@ pub mod instance { */ pub async fn instance_policies_schema( &self, - ) -> Result { + ) -> Result { let client = Client::new(); let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/"; let request = match client.get(&endpoint_url).send().await { @@ -33,7 +33,7 @@ pub mod instance { } let body = request.text().await.unwrap(); - let instance_policies_schema: InstancePoliciesSchema = from_str(&body).unwrap(); + let instance_policies_schema: InstancePolicies = from_str(&body).unwrap(); Ok(instance_policies_schema) } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 0feb280..33091a6 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -1,465 +1,250 @@ -pub mod schemas { - use regex::Regex; - use serde::{Deserialize, Serialize}; - use std::{collections::HashMap, fmt}; +use regex::Regex; +use serde::{Deserialize, Serialize}; - use crate::{api::limits::Limits, errors::FieldFormatError, URLBundle}; +use crate::errors::FieldFormatError; +/** +A struct that represents a well-formed email address. + */ +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct AuthEmail { + pub email: String, +} + +impl AuthEmail { /** - A struct that represents a well-formed email address. - */ - #[derive(Clone, PartialEq, Eq, Debug)] - pub struct AuthEmail { - pub email: String, - } - - impl AuthEmail { - /** - Returns a new [`Result`]. - ## Arguments - The email address you want to validate. - ## Errors - You will receive a [`FieldFormatError`], if: - - The email address is not in a valid format. - - */ - pub fn new(email: String) -> Result { - let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); - if !regex.is_match(email.clone().as_str()) { - return Err(FieldFormatError::EmailError); - } - return Ok(AuthEmail { email }); - } - } - - /** - A struct that represents a well-formed username. + Returns a new [`Result`]. ## Arguments - Please use new() to create a new instance of this struct. + The email address you want to validate. + ## Errors + You will receive a [`FieldFormatError`], if: + - The email address is not in a valid format. + + */ + pub fn new(email: String) -> Result { + let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); + if !regex.is_match(email.clone().as_str()) { + return Err(FieldFormatError::EmailError); + } + return Ok(AuthEmail { email }); + } +} + +/** +A struct that represents a well-formed username. +## Arguments +Please use new() to create a new instance of this struct. +## Errors +You will receive a [`FieldFormatError`], if: +- The username is not between 2 and 32 characters. + */ +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct AuthUsername { + pub username: String, +} + +impl AuthUsername { + /** + Returns a new [`Result`]. + ## Arguments + The username you want to validate. ## Errors You will receive a [`FieldFormatError`], if: - The username is not between 2 and 32 characters. */ - #[derive(Clone, PartialEq, Eq, Debug)] - pub struct AuthUsername { - pub username: String, - } - - impl AuthUsername { - /** - Returns a new [`Result`]. - ## Arguments - The username you want to validate. - ## Errors - You will receive a [`FieldFormatError`], if: - - The username is not between 2 and 32 characters. - */ - pub fn new(username: String) -> Result { - if username.len() < 2 || username.len() > 32 { - return Err(FieldFormatError::UsernameError); - } else { - return Ok(AuthUsername { username }); - } + pub fn new(username: String) -> Result { + if username.len() < 2 || username.len() > 32 { + return Err(FieldFormatError::UsernameError); + } else { + return Ok(AuthUsername { username }); } } +} +/** +A struct that represents a well-formed password. +## Arguments +Please use new() to create a new instance of this struct. +## Errors +You will receive a [`FieldFormatError`], if: +- The password is not between 1 and 72 characters. + */ +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct AuthPassword { + pub password: String, +} + +impl AuthPassword { /** - A struct that represents a well-formed password. + Returns a new [`Result`]. ## Arguments - Please use new() to create a new instance of this struct. + The password you want to validate. ## Errors You will receive a [`FieldFormatError`], if: - The password is not between 1 and 72 characters. */ - #[derive(Clone, PartialEq, Eq, Debug)] - pub struct AuthPassword { - pub password: String, - } - - impl AuthPassword { - /** - Returns a new [`Result`]. - ## Arguments - The password you want to validate. - ## Errors - You will receive a [`FieldFormatError`], if: - - The password is not between 1 and 72 characters. - */ - pub fn new(password: String) -> Result { - if password.len() < 1 || password.len() > 72 { - return Err(FieldFormatError::PasswordError); - } else { - return Ok(AuthPassword { password }); - } + pub fn new(password: String) -> Result { + if password.len() < 1 || password.len() > 72 { + return Err(FieldFormatError::PasswordError); + } else { + return Ok(AuthPassword { password }); } } +} +/** +A struct that represents a well-formed register request. +## Arguments +Please use new() to create a new instance of this struct. +## Errors +You will receive a [`FieldFormatError`], if: +- The username is not between 2 and 32 characters. +- The password is not between 1 and 72 characters. + */ + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub struct RegisterSchema { + username: String, + password: Option, + consent: bool, + email: Option, + fingerprint: Option, + invite: Option, + date_of_birth: Option, + gift_code_sku_id: Option, + captcha_key: Option, + promotional_email_opt_in: Option, +} + +impl RegisterSchema { /** - A struct that represents a well-formed register request. + Returns a new [`Result`]. ## Arguments - Please use new() to create a new instance of this struct. + All but "String::username" and "bool::consent" are optional. + ## Errors You will receive a [`FieldFormatError`], if: - - The username is not between 2 and 32 characters. - - The password is not between 1 and 72 characters. - */ + - The username is less than 2 or more than 32 characters in length + - You supply a `password` which is less than 1 or more than 72 characters in length. - #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] - #[serde(rename_all = "snake_case")] - pub struct RegisterSchema { - username: String, - password: Option, + These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/) + */ + pub fn new( + username: AuthUsername, + password: Option, consent: bool, - email: Option, + email: Option, fingerprint: Option, invite: Option, date_of_birth: Option, gift_code_sku_id: Option, captcha_key: Option, promotional_email_opt_in: Option, - } + ) -> Result { + let username = username.username; - impl RegisterSchema { - /** - Returns a new [`Result`]. - ## Arguments - All but "String::username" and "bool::consent" are optional. - - ## Errors - You will receive a [`FieldFormatError`], if: - - The username is less than 2 or more than 32 characters in length - - You supply a `password` which is less than 1 or more than 72 characters in length. - - These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/) - */ - pub fn new( - username: AuthUsername, - password: Option, - consent: bool, - email: Option, - fingerprint: Option, - invite: Option, - date_of_birth: Option, - gift_code_sku_id: Option, - captcha_key: Option, - promotional_email_opt_in: Option, - ) -> Result { - let username = username.username; - - let email_addr; - if email.is_some() { - email_addr = Some(email.unwrap().email); - } else { - email_addr = None; - } - - let has_password; - if password.is_some() { - has_password = Some(password.unwrap().password); - } else { - has_password = None; - } - - if !consent { - return Err(FieldFormatError::ConsentError); - } - - return Ok(RegisterSchema { - username, - password: has_password, - consent, - email: email_addr, - fingerprint, - invite, - date_of_birth, - gift_code_sku_id, - captcha_key, - promotional_email_opt_in, - }); + let email_addr; + if email.is_some() { + email_addr = Some(email.unwrap().email); + } else { + email_addr = None; } - } + let has_password; + if password.is_some() { + has_password = Some(password.unwrap().password); + } else { + has_password = None; + } + + if !consent { + return Err(FieldFormatError::ConsentError); + } + + return Ok(RegisterSchema { + username, + password: has_password, + consent, + email: email_addr, + fingerprint, + invite, + date_of_birth, + gift_code_sku_id, + captcha_key, + promotional_email_opt_in, + }); + } +} + +/** +A struct that represents a well-formed login request. +## Arguments +Please use new() to create a new instance of this struct. +## Errors +You will receive a [`FieldFormatError`], if: +- The username is not between 2 and 32 characters. +- The password is not between 1 and 72 characters. + */ +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub struct LoginSchema { + login: String, + password: String, + undelete: Option, + captcha_key: Option, + login_source: Option, + gift_code_sku_id: Option, +} + +impl LoginSchema { /** - A struct that represents a well-formed login request. + Returns a new [`Result`]. ## Arguments - Please use new() to create a new instance of this struct. + login: The username you want to login with. + password: The password you want to login with. + undelete: Honestly no idea what this is for. + captcha_key: The captcha key you want to login with. + login_source: The login source. + gift_code_sku_id: The gift code sku id. ## Errors You will receive a [`FieldFormatError`], if: - - The username is not between 2 and 32 characters. - - The password is not between 1 and 72 characters. - */ - #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] - #[serde(rename_all = "snake_case")] - pub struct LoginSchema { - login: String, + - The username is less than 2 or more than 32 characters in length + */ + pub fn new( + login: AuthUsername, password: String, undelete: Option, captcha_key: Option, login_source: Option, gift_code_sku_id: Option, + ) -> Result { + let login = login.username; + return Ok(LoginSchema { + login, + password, + undelete, + captcha_key, + login_source, + gift_code_sku_id, + }); } +} - impl LoginSchema { - /** - Returns a new [`Result`]. - ## Arguments - login: The username you want to login with. - password: The password you want to login with. - undelete: Honestly no idea what this is for. - captcha_key: The captcha key you want to login with. - login_source: The login source. - gift_code_sku_id: The gift code sku id. - ## Errors - You will receive a [`FieldFormatError`], if: - - The username is less than 2 or more than 32 characters in length - */ - pub fn new( - login: AuthUsername, - password: String, - undelete: Option, - captcha_key: Option, - login_source: Option, - gift_code_sku_id: Option, - ) -> Result { - let login = login.username; - return Ok(LoginSchema { - login, - password, - undelete, - captcha_key, - login_source, - gift_code_sku_id, - }); - } - } - - #[derive(Debug, Serialize, Deserialize)] - pub struct LoginResult { - token: String, - settings: UserSettings, - } - - #[derive(Debug, Serialize, Deserialize)] - pub struct UserSettings { - afk_timeout: i32, - allow_accessibility_detection: bool, - animate_emoji: bool, - animate_stickers: i32, - contact_sync_enabled: bool, - convert_emoticons: bool, - custom_status: Option, - default_guilds_restricted: bool, - detect_platform_accounts: bool, - developer_mode: bool, - disable_games_tab: bool, - enable_tts_command: bool, - explicit_content_filter: i32, - friend_source_flags: FriendSourceFlags, - friend_discovery_flags: Option, - gateway_connected: bool, - gif_auto_play: bool, - guild_folders: Vec, - guild_positions: Vec, - inline_attachment_media: bool, - inline_embed_media: bool, - locale: String, - message_display_compact: bool, - native_phone_integration_enabled: bool, - render_embeds: bool, - render_reactions: bool, - restricted_guilds: Vec, - show_current_game: bool, - status: String, - stream_notifications_enabled: bool, - theme: String, - timezone_offset: i32, - view_nsfw_guilds: bool, - } - - #[derive(Debug, Serialize, Deserialize)] - pub struct FriendSourceFlags { - all: Option, - mutual_friends: Option, - mutual_guilds: Option, - } - - #[derive(Debug, Serialize, Deserialize)] - pub struct GuildFolder { - id: String, - guild_ids: Vec, - name: String, - } - - #[derive(Debug, Serialize, Deserialize)] - #[serde(rename_all = "snake_case")] - pub struct TotpSchema { - code: String, - ticket: String, - gift_code_sku_id: Option, - login_source: Option, - } - - /** - Represents the result you get from GET: /api/instance/policies/. - */ - #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] - #[serde(rename_all = "camelCase")] - pub struct InstancePoliciesSchema { - instance_name: String, - instance_description: Option, - front_page: Option, - tos_page: Option, - correspondence_email: Option, - correspondence_user_id: Option, - image: Option, - instance_id: Option, - } - - impl InstancePoliciesSchema { - pub fn new( - instance_name: String, - instance_description: Option, - front_page: Option, - tos_page: Option, - correspondence_email: Option, - correspondence_user_id: Option, - image: Option, - instance_id: Option, - ) -> Self { - InstancePoliciesSchema { - instance_name, - instance_description, - front_page, - tos_page, - correspondence_email, - correspondence_user_id, - image, - instance_id, - } - } - } - - impl fmt::Display for InstancePoliciesSchema { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "InstancePoliciesSchema {{ instance_name: {}, instance_description: {}, front_page: {}, tos_page: {}, correspondence_email: {}, correspondence_user_id: {}, image: {}, instance_id: {} }}", - self.instance_name, - self.instance_description.clone().unwrap_or("None".to_string()), - self.front_page.clone().unwrap_or("None".to_string()), - self.tos_page.clone().unwrap_or("None".to_string()), - self.correspondence_email.clone().unwrap_or("None".to_string()), - self.correspondence_user_id.clone().unwrap_or("None".to_string()), - self.image.clone().unwrap_or("None".to_string()), - self.instance_id.clone().unwrap_or("None".to_string()), - ) - } - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct ErrorResponse { - pub code: i32, - pub message: String, - pub errors: IntermittentError, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct IntermittentError { - #[serde(flatten)] - pub errors: std::collections::HashMap, - } - - #[derive(Serialize, Deserialize, Debug, Default)] - pub struct ErrorField { - #[serde(default)] - pub _errors: Vec, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct Error { - pub message: String, - pub code: String, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct UserObject { - id: String, - username: String, - discriminator: String, - avatar: Option, - bot: Option, - system: Option, - mfa_enabled: Option, - banner: Option, - accent_color: Option, - locale: String, - verified: Option, - email: Option, - flags: i8, - premium_type: Option, - public_flags: Option, - } - - #[derive(Debug)] - pub struct User { - logged_in: bool, - belongs_to: URLBundle, - token: String, - rate_limits: Limits, - pub settings: UserSettings, - pub object: UserObject, - } - - impl User { - pub fn is_logged_in(&self) -> bool { - if self.logged_in == true { - true - } else { - false - } - } - - pub fn belongs_to(&self) -> URLBundle { - return self.belongs_to.clone(); - } - - pub fn token(&self) -> String { - return self.token.clone(); - } - - pub fn set_logged_in(&mut self, bool: bool) { - self.logged_in = bool; - } - - pub fn set_token(&mut self, token: String) { - self.token = token; - } - - pub fn new( - logged_in: bool, - belongs_to: URLBundle, - token: String, - rate_limits: Limits, - settings: UserSettings, - object: UserObject, - ) -> User { - User { - logged_in, - belongs_to, - token, - rate_limits, - settings, - object, - } - } - } +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct TotpSchema { + code: String, + ticket: String, + gift_code_sku_id: Option, + login_source: Option, } // I know that some of these tests are... really really basic and unneccessary, but sometimes, I // just feel like writing tests, so there you go :) -@bitfl0wer #[cfg(test)] mod schemas_tests { - use super::schemas::*; + use super::*; use crate::errors::FieldFormatError; #[test] diff --git a/src/api/types.rs b/src/api/types.rs new file mode 100644 index 0000000..1f281f0 --- /dev/null +++ b/src/api/types.rs @@ -0,0 +1,217 @@ +use std::fmt; + +use serde::{Deserialize, Serialize}; + +use crate::{api::limits::Limits, URLBundle}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct LoginResult { + token: String, + settings: UserSettings, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserSettings { + afk_timeout: i32, + allow_accessibility_detection: bool, + animate_emoji: bool, + animate_stickers: i32, + contact_sync_enabled: bool, + convert_emoticons: bool, + custom_status: Option, + default_guilds_restricted: bool, + detect_platform_accounts: bool, + developer_mode: bool, + disable_games_tab: bool, + enable_tts_command: bool, + explicit_content_filter: i32, + friend_source_flags: FriendSourceFlags, + friend_discovery_flags: Option, + gateway_connected: bool, + gif_auto_play: bool, + guild_folders: Vec, + guild_positions: Vec, + inline_attachment_media: bool, + inline_embed_media: bool, + locale: String, + message_display_compact: bool, + native_phone_integration_enabled: bool, + render_embeds: bool, + render_reactions: bool, + restricted_guilds: Vec, + show_current_game: bool, + status: String, + stream_notifications_enabled: bool, + theme: String, + timezone_offset: i32, + view_nsfw_guilds: bool, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct FriendSourceFlags { + all: Option, + mutual_friends: Option, + mutual_guilds: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct GuildFolder { + id: String, + guild_ids: Vec, + name: String, +} + +/** +Represents the result you get from GET: /api/instance/policies/. +*/ +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct InstancePolicies { + instance_name: String, + instance_description: Option, + front_page: Option, + tos_page: Option, + correspondence_email: Option, + correspondence_user_id: Option, + image: Option, + instance_id: Option, +} + +impl InstancePolicies { + pub fn new( + instance_name: String, + instance_description: Option, + front_page: Option, + tos_page: Option, + correspondence_email: Option, + correspondence_user_id: Option, + image: Option, + instance_id: Option, + ) -> Self { + InstancePolicies { + instance_name, + instance_description, + front_page, + tos_page, + correspondence_email, + correspondence_user_id, + image, + instance_id, + } + } +} + +impl fmt::Display for InstancePolicies { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "InstancePoliciesSchema {{ instance_name: {}, instance_description: {}, front_page: {}, tos_page: {}, correspondence_email: {}, correspondence_user_id: {}, image: {}, instance_id: {} }}", + self.instance_name, + self.instance_description.clone().unwrap_or("None".to_string()), + self.front_page.clone().unwrap_or("None".to_string()), + self.tos_page.clone().unwrap_or("None".to_string()), + self.correspondence_email.clone().unwrap_or("None".to_string()), + self.correspondence_user_id.clone().unwrap_or("None".to_string()), + self.image.clone().unwrap_or("None".to_string()), + self.instance_id.clone().unwrap_or("None".to_string()), + ) + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct ErrorResponse { + pub code: i32, + pub message: String, + pub errors: IntermittentError, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct IntermittentError { + #[serde(flatten)] + pub errors: std::collections::HashMap, +} + +#[derive(Serialize, Deserialize, Debug, Default)] +pub struct ErrorField { + #[serde(default)] + pub _errors: Vec, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Error { + pub message: String, + pub code: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct UserObject { + id: String, + username: String, + discriminator: String, + avatar: Option, + bot: Option, + system: Option, + mfa_enabled: Option, + banner: Option, + accent_color: Option, + locale: String, + verified: Option, + email: Option, + flags: i8, + premium_type: Option, + public_flags: Option, +} + +#[derive(Debug)] +pub struct User { + logged_in: bool, + belongs_to: URLBundle, + token: String, + rate_limits: Limits, + pub settings: UserSettings, + pub object: UserObject, +} + +impl User { + pub fn is_logged_in(&self) -> bool { + if self.logged_in == true { + true + } else { + false + } + } + + pub fn belongs_to(&self) -> URLBundle { + return self.belongs_to.clone(); + } + + pub fn token(&self) -> String { + return self.token.clone(); + } + + pub fn set_logged_in(&mut self, bool: bool) { + self.logged_in = bool; + } + + pub fn set_token(&mut self, token: String) { + self.token = token; + } + + pub fn new( + logged_in: bool, + belongs_to: URLBundle, + token: String, + rate_limits: Limits, + settings: UserSettings, + object: UserObject, + ) -> User { + User { + logged_in, + belongs_to, + token, + rate_limits, + settings, + object, + } + } +} diff --git a/src/instance.rs b/src/instance.rs index d606b42..7f2bb9e 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,5 +1,5 @@ -use crate::api::limits::{Limit, LimitType, Limits}; -use crate::api::schemas::schemas::{InstancePoliciesSchema, User}; +use crate::api::limits::Limits; +use crate::api::types::{InstancePolicies, User}; use crate::errors::{FieldFormatError, InstanceServerError}; use crate::limit::LimitedRequester; use crate::URLBundle; @@ -13,7 +13,7 @@ The [`Instance`] what you will be using to perform all sorts of actions on the S */ pub struct Instance { pub urls: URLBundle, - pub instance_info: InstancePoliciesSchema, + pub instance_info: InstancePolicies, pub requester: LimitedRequester, pub limits: Limits, //pub gateway: Gateway, @@ -34,7 +34,7 @@ impl Instance { let users: HashMap = HashMap::new(); let mut instance = Instance { urls: urls.clone(), - instance_info: InstancePoliciesSchema::new( + instance_info: InstancePolicies::new( // This is okay, because the instance_info will be overwritten by the instance_policies_schema() function. "".to_string(), None, From c67c23f6b4dcaf6b8a931a8628940fed1f05b952 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 25 Apr 2023 17:33:35 +0200 Subject: [PATCH 2/3] Improve readability --- src/api/auth/register.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 650b8cf..d0d6cf8 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -37,7 +37,7 @@ pub mod register { &mut cloned_limits, ) .await; - if !response.is_ok() { + if response.is_err() { return Err(InstanceServerError::NoResponse); } @@ -55,9 +55,9 @@ pub mod register { } return Err(InstanceServerError::InvalidFormBodyError { error_type, error }); } - return Ok(Token { + Ok(Token { token: response_text_string, - }); + }) } } } From 3b8891b2dd12c10d9e26662b8cb960cca548de95 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 25 Apr 2023 17:41:14 +0200 Subject: [PATCH 3/3] Improve readability --- src/api/auth/login.rs | 4 +- src/api/auth/register.rs | 4 +- src/api/policies/instance/instance.rs | 4 +- src/api/policies/instance/limits.rs | 62 +++++++++++++-------------- src/api/schemas.rs | 26 +++++------ src/api/types.rs | 10 ++--- src/instance.rs | 2 +- src/lib.rs | 4 +- src/limit.rs | 22 +++++----- 9 files changed, 67 insertions(+), 71 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index bc37b25..f7c4fe6 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -30,7 +30,7 @@ pub mod login { &mut cloned_limits, ) .await; - if !response.is_ok() { + if response.is_err() { return Err(InstanceServerError::NoResponse); } @@ -51,7 +51,7 @@ pub mod login { let login_result: LoginResult = from_str(&response_text_string).unwrap(); - return Ok(login_result); + Ok(login_result) } } } diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index d0d6cf8..b932b9f 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -76,7 +76,7 @@ mod test { "http://localhost:3001".to_string(), "http://localhost:3001".to_string(), ); - let limited_requester = LimitedRequester::new(urls.get_api().to_string()).await; + let limited_requester = LimitedRequester::new().await; let mut test_instance = Instance::new(urls.clone(), limited_requester) .await .unwrap(); @@ -109,7 +109,7 @@ mod test { "http://localhost:3001".to_string(), "http://localhost:3001".to_string(), ); - let limited_requester = LimitedRequester::new(urls.get_api().to_string()).await; + let limited_requester = LimitedRequester::new().await; let mut test_instance = Instance::new(urls.clone(), limited_requester) .await .unwrap(); diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 7f69670..f96325f 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -26,7 +26,7 @@ pub mod instance { } }; - if request.status().as_str().chars().next().unwrap() != '2' { + if !request.status().as_str().starts_with('2') { return Err(InstanceServerError::ReceivedErrorCodeError { error_code: request.status().to_string(), }); @@ -50,7 +50,7 @@ mod instance_policies_schema_test { "http://localhost:3001".to_string(), "http://localhost:3001".to_string(), ); - let limited_requester = LimitedRequester::new(urls.get_api().to_string()).await; + let limited_requester = LimitedRequester::new().await; let test_instance = Instance::new(urls.clone(), limited_requester) .await .unwrap(); diff --git a/src/api/policies/instance/limits.rs b/src/api/policies/instance/limits.rs index c3c76a3..7c4b624 100644 --- a/src/api/policies/instance/limits.rs +++ b/src/api/policies/instance/limits.rs @@ -155,14 +155,14 @@ pub mod limits { impl Limit { pub fn add_remaining(&mut self, remaining: i64) { if remaining < 0 { - if ((self.remaining as i64 + remaining) as i64) <= 0 { + if (self.remaining as i64 + remaining) <= 0 { self.remaining = 0; return; } - self.remaining -= remaining.abs() as u64; + self.remaining -= remaining.unsigned_abs(); return; } - self.remaining += remaining.abs() as u64; + self.remaining += remaining.unsigned_abs(); } } @@ -184,7 +184,7 @@ pub mod limits { instance_rate_limits: &'a mut Limits, user_rate_limits: &'a mut Limits, ) -> LimitsMutRef<'a> { - return LimitsMutRef { + LimitsMutRef { limit_absolute_messages: &mut instance_rate_limits.limit_absolute_messages, limit_absolute_register: &mut instance_rate_limits.limit_absolute_register, limit_auth_login: &mut instance_rate_limits.limit_auth_login, @@ -195,36 +195,36 @@ pub mod limits { limit_guild: &mut user_rate_limits.limit_guild, limit_ip: &mut instance_rate_limits.limit_ip, limit_webhook: &mut user_rate_limits.limit_webhook, - }; + } } pub fn get_limit_ref(&self, limit_type: &LimitType) -> &Limit { match limit_type { - &LimitType::AbsoluteMessage => &self.limit_absolute_messages, - &LimitType::AbsoluteRegister => &self.limit_absolute_register, - &LimitType::AuthLogin => &self.limit_auth_login, - &LimitType::AuthRegister => &self.limit_auth_register, - &LimitType::Channel => &self.limit_channel, - &LimitType::Error => &self.limit_error, - &LimitType::Global => &self.limit_global, - &LimitType::Guild => &self.limit_guild, - &LimitType::Ip => &self.limit_ip, - &LimitType::Webhook => &self.limit_webhook, + &LimitType::AbsoluteMessage => self.limit_absolute_messages, + &LimitType::AbsoluteRegister => self.limit_absolute_register, + &LimitType::AuthLogin => self.limit_auth_login, + &LimitType::AuthRegister => self.limit_auth_register, + &LimitType::Channel => self.limit_channel, + &LimitType::Error => self.limit_error, + &LimitType::Global => self.limit_global, + &LimitType::Guild => self.limit_guild, + &LimitType::Ip => self.limit_ip, + &LimitType::Webhook => self.limit_webhook, } } pub fn get_limit_mut_ref(&mut self, limit_type: &LimitType) -> &mut Limit { match limit_type { - &LimitType::AbsoluteMessage => &mut self.limit_absolute_messages, - &LimitType::AbsoluteRegister => &mut self.limit_absolute_register, - &LimitType::AuthLogin => &mut self.limit_auth_login, - &LimitType::AuthRegister => &mut self.limit_auth_register, - &LimitType::Channel => &mut self.limit_channel, - &LimitType::Error => &mut self.limit_error, - &LimitType::Global => &mut self.limit_global, - &LimitType::Guild => &mut self.limit_guild, - &LimitType::Ip => &mut self.limit_ip, - &LimitType::Webhook => &mut self.limit_webhook, + &LimitType::AbsoluteMessage => self.limit_absolute_messages, + &LimitType::AbsoluteRegister => self.limit_absolute_register, + &LimitType::AuthLogin => self.limit_auth_login, + &LimitType::AuthRegister => self.limit_auth_register, + &LimitType::Channel => self.limit_channel, + &LimitType::Error => self.limit_error, + &LimitType::Global => self.limit_global, + &LimitType::Guild => self.limit_guild, + &LimitType::Ip => self.limit_ip, + &LimitType::Webhook => self.limit_webhook, } } } @@ -245,7 +245,7 @@ pub mod limits { impl Limits { pub fn combine(instance_rate_limits: &Limits, user_rate_limits: &Limits) -> Limits { - return Limits { + Limits { limit_absolute_messages: instance_rate_limits.limit_absolute_messages, limit_absolute_register: instance_rate_limits.limit_absolute_register, limit_auth_login: instance_rate_limits.limit_auth_login, @@ -256,7 +256,7 @@ pub mod limits { limit_guild: user_rate_limits.limit_guild, limit_ip: instance_rate_limits.limit_ip, limit_webhook: user_rate_limits.limit_webhook, - }; + } } pub fn get_limit_ref(&self, limit_type: &LimitType) -> &Limit { @@ -329,7 +329,7 @@ pub mod limits { let config: Config = from_str(&result).unwrap(); // If config.rate.enabled is false, then add return a Limits struct with all limits set to u64::MAX let mut limits: Limits; - if config.rate.enabled == false { + if !config.rate.enabled { limits = Limits { limit_absolute_messages: Limit { bucket: LimitType::AbsoluteMessage, @@ -475,7 +475,7 @@ pub mod limits { }; } - return limits; + limits } } } @@ -493,8 +493,8 @@ mod instance_limits { reset: 0, }; limit.add_remaining(-2); - assert_eq!(0 as u64, limit.remaining); + assert_eq!(0_u64, limit.remaining); limit.add_remaining(-2123123); - assert_eq!(0 as u64, limit.remaining); + assert_eq!(0_u64, limit.remaining); } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 33091a6..08425a3 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -23,10 +23,10 @@ impl AuthEmail { */ pub fn new(email: String) -> Result { let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); - if !regex.is_match(email.clone().as_str()) { + if !regex.is_match(email.as_str()) { return Err(FieldFormatError::EmailError); } - return Ok(AuthEmail { email }); + Ok(AuthEmail { email }) } } @@ -54,9 +54,9 @@ impl AuthUsername { */ pub fn new(username: String) -> Result { if username.len() < 2 || username.len() > 32 { - return Err(FieldFormatError::UsernameError); + Err(FieldFormatError::UsernameError) } else { - return Ok(AuthUsername { username }); + Ok(AuthUsername { username }) } } } @@ -84,10 +84,10 @@ impl AuthPassword { - The password is not between 1 and 72 characters. */ pub fn new(password: String) -> Result { - if password.len() < 1 || password.len() > 72 { - return Err(FieldFormatError::PasswordError); + if password.is_empty() || password.len() > 72 { + Err(FieldFormatError::PasswordError) } else { - return Ok(AuthPassword { password }); + Ok(AuthPassword { password }) } } } @@ -162,7 +162,7 @@ impl RegisterSchema { return Err(FieldFormatError::ConsentError); } - return Ok(RegisterSchema { + Ok(RegisterSchema { username, password: has_password, consent, @@ -173,7 +173,7 @@ impl RegisterSchema { gift_code_sku_id, captcha_key, promotional_email_opt_in, - }); + }) } } @@ -220,14 +220,14 @@ impl LoginSchema { gift_code_sku_id: Option, ) -> Result { let login = login.username; - return Ok(LoginSchema { + Ok(LoginSchema { login, password, undelete, captcha_key, login_source, gift_code_sku_id, - }); + }) } } @@ -259,7 +259,7 @@ mod schemas_tests { fn password_too_long() { let mut long_pw = String::new(); for _ in 0..73 { - long_pw = long_pw + "a"; + long_pw += "a"; } assert_eq!( AuthPassword::new(long_pw), @@ -279,7 +279,7 @@ mod schemas_tests { fn username_too_long() { let mut long_un = String::new(); for _ in 0..33 { - long_un = long_un + "a"; + long_un += "a"; } assert_eq!( AuthUsername::new(long_un), diff --git a/src/api/types.rs b/src/api/types.rs index 1f281f0..d4c8309 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -174,19 +174,15 @@ pub struct User { impl User { pub fn is_logged_in(&self) -> bool { - if self.logged_in == true { - true - } else { - false - } + self.logged_in } pub fn belongs_to(&self) -> URLBundle { - return self.belongs_to.clone(); + self.belongs_to.clone() } pub fn token(&self) -> String { - return self.token.clone(); + self.token.clone() } pub fn set_logged_in(&mut self, bool: bool) { diff --git a/src/instance.rs b/src/instance.rs index 7f2bb9e..3e52e88 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -87,6 +87,6 @@ impl Username { if username.len() < 2 || username.len() > 32 { return Err(FieldFormatError::UsernameError); } - return Ok(Username { username }); + Ok(Username { username }) } } diff --git a/src/lib.rs b/src/lib.rs index ab97b74..00170d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,10 +49,10 @@ impl URLBundle { }; // if the last character of the string is a slash, remove it. let mut url_string = url.to_string(); - if url_string.chars().last().unwrap() == '/' { + if url_string.ends_with('/') { url_string.pop(); } - return url_string; + url_string } pub fn get_api(&self) -> &str { diff --git a/src/limit.rs b/src/limit.rs index f7f89d5..84e6d17 100644 --- a/src/limit.rs +++ b/src/limit.rs @@ -28,7 +28,7 @@ impl LimitedRequester { /// be send within the `Limit` of an external API Ratelimiter, and looks at the returned request /// headers to see if it can find Ratelimit info to update itself. #[allow(dead_code)] - pub async fn new(api_url: String) -> Self { + pub async fn new() -> Self { LimitedRequester { http: Client::new(), requests: VecDeque::new(), @@ -86,13 +86,13 @@ impl LimitedRequester { instance_rate_limits, user_rate_limits, ); - return Ok(response); + Ok(response) } else { self.requests.push_back(TypedRequest { - request: request, - limit_type: limit_type, + request, + limit_type, }); - return Err(InstanceServerError::RateLimited); + Err(InstanceServerError::RateLimited) } } @@ -125,7 +125,7 @@ impl LimitedRequester { ] .to_vec(); for limit in constant_limits.iter() { - match rate_limits.to_hash_map().get(&limit) { + match rate_limits.to_hash_map().get(limit) { Some(limit) => { if limit.remaining == 0 { return false; @@ -155,7 +155,7 @@ impl LimitedRequester { None => return false, } } - return true; + true } fn update_limits( @@ -183,7 +183,7 @@ impl LimitedRequester { let status = response.status(); let status_str = status.as_str(); - if status_str.chars().next().unwrap() == '4' { + if status_str.starts_with('4') { rate_limits .get_limit_mut_ref(&LimitType::Error) .add_remaining(-1); @@ -266,7 +266,7 @@ mod rate_limit { String::from("wss://localhost:3001/"), String::from("http://localhost:3001/cdn"), ); - let requester = LimitedRequester::new(urls.api).await; + let _requester = LimitedRequester::new().await; } #[tokio::test] @@ -276,7 +276,7 @@ mod rate_limit { String::from("wss://localhost:3001/"), String::from("http://localhost:3001/cdn"), ); - let mut requester = LimitedRequester::new(urls.api.clone()).await; + let mut requester = LimitedRequester::new().await; let mut request: Option> = None; let mut instance_rate_limits = Limits::check_limits(urls.api.clone()).await; let mut user_rate_limits = Limits::check_limits(urls.api.clone()).await; @@ -314,7 +314,7 @@ mod rate_limit { ); let mut instance_rate_limits = Limits::check_limits(urls.api.clone()).await; let mut user_rate_limits = Limits::check_limits(urls.api.clone()).await; - let mut requester = LimitedRequester::new(urls.api.clone()).await; + let mut requester = LimitedRequester::new().await; let request_path = urls.api.clone() + "/policies/instance/limits"; let request_builder = requester.http.get(request_path); let request = requester