From 06bc2955a35f02f7ebbb14eac4288f4440e02001 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 21 Apr 2023 16:18:20 +0200 Subject: [PATCH] Move custom errros to central file --- src/api/policies/instance/instance.rs | 9 +-------- src/api/schemas.rs | 12 +++--------- src/errors.rs | 17 +++++++++++++++++ src/lib.rs | 1 + 4 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 src/errors.rs diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index aa5f8ae..f0af029 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -1,17 +1,10 @@ pub mod instance { - use custom_error::custom_error; use reqwest::Client; use serde_json::from_str; + use crate::errors::InstancePoliciesError; use crate::{api::schemas::schemas::InstancePoliciesSchema, instance::Instance}; - custom_error! { - #[derive(PartialEq, Eq)] - pub InstancePoliciesError - RequestErrorError{url:String, error:String} = "An error occured while trying to GET from {url}: {error}", - ReceivedErrorCodeError{error_code:String} = "Received the following error code while requesting from the route: {error_code}" - } - impl Instance { /** Gets the instance policies schema. diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 73cd464..680d074 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -5,6 +5,8 @@ pub mod schemas { use regex::Regex; use serde::{Deserialize, Serialize}; + use crate::errors::RegisterSchemaError; + #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub struct RegisterSchema { @@ -20,15 +22,6 @@ pub mod schemas { promotional_email_opt_in: Option, } - custom_error! { - #[derive(PartialEq, Eq)] - pub RegisterSchemaError - PasswordError = "Password must be between 1 and 72 characters.", - UsernameError = "Username must be between 2 and 32 characters.", - ConsentError = "Consent must be 'true' to register.", - EmailError = "The provided email address is in an invalid format." - } - impl RegisterSchema { /** Returns a new [`Result`]. @@ -166,6 +159,7 @@ pub mod schemas { #[cfg(test)] mod schemas_tests { use super::schemas::*; + use crate::errors::RegisterSchemaError; #[test] fn password_too_short() { diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..28b983f --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,17 @@ +use custom_error::custom_error; + +custom_error! { + #[derive(PartialEq, Eq)] + pub InstancePoliciesError + RequestErrorError{url:String, error:String} = "An error occured while trying to GET from {url}: {error}", + ReceivedErrorCodeError{error_code:String} = "Received the following error code while requesting from the route: {error_code}" +} + +custom_error! { + #[derive(PartialEq, Eq)] + pub RegisterSchemaError + PasswordError = "Password must be between 1 and 72 characters.", + UsernameError = "Username must be between 2 and 32 characters.", + ConsentError = "Consent must be 'true' to register.", + EmailError = "The provided email address is in an invalid format." +} diff --git a/src/lib.rs b/src/lib.rs index 838f31b..ab97b74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod api; +mod errors; mod gateway; mod instance; mod limit;