diff --git a/Cargo.toml b/Cargo.toml index 3f35601..175e3e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,6 @@ version = "0.1.0" license = "AGPL-3" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] tokio = {version = "1.27.0", features = ["rt", "macros"]} serde = {version = "1.0.159", features = ["derive"]} @@ -13,4 +11,5 @@ serde_json = "1.0.95" reqwest = "0.11.16" url = "2.3.1" chrono = "0.4.24" -regex = "1.7.3" \ No newline at end of file +regex = "1.7.3" +custom_error = "1.9.2" \ No newline at end of file diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 19bf426..cea10d2 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -1,6 +1,5 @@ pub mod register { - use std::fmt; - + use custom_error::custom_error; use reqwest::Client; use serde_json::json; @@ -16,23 +15,4 @@ pub mod register { // TODO } } - - #[derive(Debug, PartialEq, Eq)] - pub struct RegisterError { - pub message: String, - } - - impl RegisterError { - fn new(message: String) -> Self { - RegisterError { message } - } - } - - impl fmt::Display for RegisterError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message) - } - } - - impl std::error::Error for RegisterError {} } diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index f14af23..aa5f8ae 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -1,29 +1,17 @@ pub mod instance { - use std::fmt; - + use custom_error::custom_error; use reqwest::Client; use serde_json::from_str; use crate::{api::schemas::schemas::InstancePoliciesSchema, instance::Instance}; - #[derive(Debug, PartialEq, Eq)] - pub struct InstancePoliciesError { - pub message: String, + 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 InstancePoliciesError { - fn new(message: String) -> Self { - InstancePoliciesError { message } - } - } - - impl fmt::Display for InstancePoliciesError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message) - } - } - - impl std::error::Error for InstancePoliciesError {} impl Instance { /** Gets the instance policies schema. @@ -38,21 +26,16 @@ pub mod instance { let request = match client.get(&endpoint_url).send().await { Ok(result) => result, Err(e) => { - return Err(InstancePoliciesError { - message: format!( - "An error occured while trying to GET from {}: {}", - endpoint_url, e - ), + return Err(InstancePoliciesError::RequestErrorError { + url: endpoint_url, + error: e.to_string(), }); } }; if request.status().as_str().chars().next().unwrap() != '2' { - return Err(InstancePoliciesError { - message: format!( - "Received the following error code while requesting from the route: {}", - request.status().as_str() - ), + return Err(InstancePoliciesError::ReceivedErrorCodeError { + error_code: request.status().to_string(), }); } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 1ca238e..73cd464 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -1,6 +1,7 @@ pub mod schemas { use std::fmt; + use custom_error::custom_error; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -19,25 +20,15 @@ pub mod schemas { promotional_email_opt_in: Option, } - #[derive(Debug, PartialEq, Eq)] - pub struct RegisterSchemaError { - pub message: String, + 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 RegisterSchemaError { - fn new(message: String) -> Self { - RegisterSchemaError { message } - } - } - - impl fmt::Display for RegisterSchemaError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message) - } - } - - impl std::error::Error for RegisterSchemaError {} - impl RegisterSchema { /** Returns a new [`Result`]. @@ -64,28 +55,20 @@ pub mod schemas { promotional_email_opt_in: Option, ) -> Result { if username.len() < 2 || username.len() > 32 { - return Err(RegisterSchemaError::new( - "Username must be between 2 and 32 characters".to_string(), - )); + return Err(RegisterSchemaError::UsernameError); } if password.is_some() && (password.as_ref().unwrap().len() < 1 || password.as_ref().unwrap().len() > 72) { - return Err(RegisterSchemaError { - message: "Password must be between 1 and 72 characters.".to_string(), - }); + return Err(RegisterSchemaError::PasswordError); } if !consent { - return Err(RegisterSchemaError { - message: "Consent must be 'true' to register.".to_string(), - }); + return Err(RegisterSchemaError::ConsentError); } let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); if email.clone().is_some() && !regex.is_match(email.clone().unwrap().as_str()) { - return Err(RegisterSchemaError { - message: "The provided email address is in an invalid format.".to_string(), - }); + return Err(RegisterSchemaError::EmailError); } return Ok(RegisterSchema { @@ -199,9 +182,7 @@ mod schemas_tests { None, None, ), - Err(RegisterSchemaError { - message: "Password must be between 1 and 72 characters.".to_string() - }) + Err(RegisterSchemaError::PasswordError) ); } @@ -224,9 +205,7 @@ mod schemas_tests { None, None, ), - Err(RegisterSchemaError { - message: "Password must be between 1 and 72 characters.".to_string() - }) + Err(RegisterSchemaError::PasswordError) ); } @@ -245,9 +224,7 @@ mod schemas_tests { None, None, ), - Err(RegisterSchemaError { - message: "Username must be between 2 and 32 characters".to_string() - }) + Err(RegisterSchemaError::UsernameError) ); } @@ -259,9 +236,7 @@ mod schemas_tests { } assert_eq!( RegisterSchema::new(long_un, None, true, None, None, None, None, None, None, None,), - Err(RegisterSchemaError { - message: "Username must be between 2 and 32 characters".to_string() - }) + Err(RegisterSchemaError::UsernameError) ); } @@ -280,9 +255,7 @@ mod schemas_tests { None, None, ), - Err(RegisterSchemaError { - message: "Consent must be 'true' to register.".to_string() - }) + Err(RegisterSchemaError::ConsentError) ); } @@ -301,9 +274,7 @@ mod schemas_tests { None, None, ), - Err(RegisterSchemaError { - message: "The provided email address is in an invalid format.".to_string() - }) + Err(RegisterSchemaError::EmailError) ) } @@ -321,11 +292,6 @@ mod schemas_tests { None, None, ); - assert_ne!( - reg, - Err(RegisterSchemaError { - message: "The provided email address is in an invalid format.".to_string() - }) - ); + assert_ne!(reg, Err(RegisterSchemaError::EmailError)); } } diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index e69de29..0000000