Change Option<ChorusLibError> to Result<(), ChorusLibError>

This commit is contained in:
kozabrada123 2023-06-20 18:26:30 +02:00
parent cafdc287d6
commit b8d7030ab1
8 changed files with 70 additions and 94 deletions

View File

@ -42,8 +42,8 @@ impl Channel {
///
/// # Returns
///
/// An `Option` that contains an `ChorusLibError` if an error occurred during the request, or `None` if the request was successful.
pub async fn delete(self, user: &mut UserMeta) -> Option<ChorusLibError> {
/// A `Result` that contains a `ChorusLibError` if an error occurred during the request, or `()` if the request was successful.
pub async fn delete(self, user: &mut UserMeta) -> Result<(), ChorusLibError> {
let request = Client::new()
.delete(format!(
"{}/channels/{}/",
@ -52,8 +52,9 @@ impl Channel {
))
.bearer_auth(user.token());
let response =
common::handle_request(request, user, crate::api::limits::LimitType::Channel).await;
response.err()
common::handle_request_as_result(request, user, crate::api::limits::LimitType::Channel)
.await;
response
}
/// Modifies a channel.

View File

@ -2,7 +2,7 @@ use reqwest::Client;
use serde_json::to_string;
use crate::{
api::handle_request,
api::{handle_request, handle_request_as_result},
errors::ChorusLibError,
instance::UserMeta,
types::{self, PermissionOverwrite},
@ -19,12 +19,12 @@ impl types::Channel {
///
/// # Returns
///
/// This function returns [`None`] if the request is successful, otherwise it returns a [`ChorusLibError`] instance.
/// This function returns a result that is either [`Ok(())`] if the request is successful, or an [`Err(ChorusLibError)`].
pub async fn edit_permissions(
user: &mut UserMeta,
channel_id: &str,
overwrite: PermissionOverwrite,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let url = {
format!(
"{}/channels/{}/permissions/{}",
@ -36,18 +36,13 @@ impl types::Channel {
let body = match to_string(&overwrite) {
Ok(string) => string,
Err(e) => {
return Some(ChorusLibError::FormCreationError {
return Err(ChorusLibError::FormCreationError {
error: e.to_string(),
});
}
};
let request = Client::new().put(url).bearer_auth(user.token()).body(body);
match handle_request(request, user, crate::api::limits::LimitType::Channel).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/// Deletes a permission overwrite for a channel.
@ -60,12 +55,12 @@ impl types::Channel {
///
/// # Returns
///
/// This function returns [`None`] if the request is successful, otherwise it returns a [`ChorusLibError`] instance.
/// This function returns a Result that is either [`Ok(())`] if the request is successfulm or an [`Err(ChorusLibError)`].
pub async fn delete_permission(
user: &mut UserMeta,
channel_id: &str,
overwrite_id: &str,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/permissions/{}",
user.belongs_to.borrow_mut().urls.api,
@ -73,11 +68,6 @@ impl types::Channel {
overwrite_id
);
let request = Client::new().delete(url).bearer_auth(user.token());
match handle_request(request, user, crate::api::limits::LimitType::Channel).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
}

View File

@ -1,7 +1,7 @@
use reqwest::Client;
use crate::{
api::{handle_request, handle_request_as_option},
api::{handle_request, handle_request_as_result},
errors::ChorusLibError,
instance::UserMeta,
types,
@ -24,13 +24,13 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
An `Option` [`crate::errors::ChorusLibError`] if something went wrong.
A `Result` [`()`] [`crate::errors::ChorusLibError`] if something went wrong.
Fires a `Message Reaction Remove All` Gateway event.
# Reference
See [https://discord.com/developers/docs/resources/channel#delete-all-reactions](https://discord.com/developers/docs/resources/channel#delete-all-reactions)
*/
pub async fn delete_all(&self, user: &mut UserMeta) -> Option<ChorusLibError> {
pub async fn delete_all(&self, user: &mut UserMeta) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/",
user.belongs_to.borrow().urls.api,
@ -38,12 +38,7 @@ impl ReactionMeta {
self.message_id
);
let request = Client::new().delete(url).bearer_auth(user.token());
match handle_request(request, user, crate::api::limits::LimitType::Channel).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/**
@ -56,12 +51,12 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
A [`crate::errors::ChorusLibError`] if something went wrong.
A Result that is [`Err(crate::errors::ChorusLibError)`] if something went wrong.
# Reference
See [https://discord.com/developers/docs/resources/channel#get-reactions](https://discord.com/developers/docs/resources/channel#get-reactions)
*/
pub async fn get(&self, emoji: &str, user: &mut UserMeta) -> Option<ChorusLibError> {
pub async fn get(&self, emoji: &str, user: &mut UserMeta) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/{}/",
user.belongs_to.borrow().urls.api,
@ -70,12 +65,7 @@ impl ReactionMeta {
emoji
);
let request = Client::new().get(url).bearer_auth(user.token());
match handle_request(request, user, crate::api::limits::LimitType::Channel).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/**
@ -89,13 +79,17 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
A [`crate::errors::ChorusLibError`] if something went wrong.
A Result that is [`Err(crate::errors::ChorusLibError)`] if something went wrong.
Fires a `Message Reaction Remove Emoji` Gateway event.
# Reference
See [https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji](https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji)
*/
pub async fn delete_emoji(&self, emoji: &str, user: &mut UserMeta) -> Option<ChorusLibError> {
pub async fn delete_emoji(
&self,
emoji: &str,
user: &mut UserMeta,
) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/{}/",
user.belongs_to.borrow().urls.api,
@ -104,12 +98,7 @@ impl ReactionMeta {
emoji
);
let request = Client::new().delete(url).bearer_auth(user.token());
match handle_request(request, user, crate::api::limits::LimitType::Channel).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/**
@ -126,14 +115,12 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
A `Result` containing a [`reqwest::Response`] or a [`crate::errors::ChorusLibError`].
Returns a 204 empty response on success.
Fires a Message Reaction Add Gateway event.
A `Result` containing [`()`] or a [`crate::errors::ChorusLibError`].
# Reference
See [https://discord.com/developers/docs/resources/channel#create-reaction](https://discord.com/developers/docs/resources/channel#create-reaction)
*/
pub async fn create(&self, emoji: &str, user: &mut UserMeta) -> Option<ChorusLibError> {
pub async fn create(&self, emoji: &str, user: &mut UserMeta) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/{}/@me/",
user.belongs_to.borrow().urls.api,
@ -142,7 +129,7 @@ impl ReactionMeta {
emoji
);
let request = Client::new().put(url).bearer_auth(user.token());
handle_request_as_option(request, user, crate::api::limits::LimitType::Channel).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/**
@ -155,14 +142,13 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
A `Result` containing a [`reqwest::Response`] or a [`crate::errors::ChorusLibError`].
Returns a 204 empty response on success.
A `Result` containing [`()`] or a [`crate::errors::ChorusLibError`].
Fires a `Message Reaction Remove` Gateway event.
# Reference
See [https://discord.com/developers/docs/resources/channel#delete-own-reaction](https://discord.com/developers/docs/resources/channel#delete-own-reaction)
*/
pub async fn remove(&self, emoji: &str, user: &mut UserMeta) -> Option<ChorusLibError> {
pub async fn remove(&self, emoji: &str, user: &mut UserMeta) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/{}/@me/",
user.belongs_to.borrow().urls.api,
@ -171,7 +157,7 @@ impl ReactionMeta {
emoji
);
let request = Client::new().delete(url).bearer_auth(user.token());
handle_request_as_option(request, user, crate::api::limits::LimitType::Channel).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
/**
@ -187,8 +173,7 @@ impl ReactionMeta {
* `user` - A mutable reference to a [`UserMeta`] instance.
# Returns
A `Result` containing a [`reqwest::Response`] or a [`crate::errors::ChorusLibError`].
Returns a 204 empty response on success.
A `Result` containing [`()`] or a [`crate::errors::ChorusLibError`].
Fires a Message Reaction Remove Gateway event.
# Reference
@ -199,7 +184,7 @@ impl ReactionMeta {
user_id: &str,
emoji: &str,
user: &mut UserMeta,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let url = format!(
"{}/channels/{}/messages/{}/reactions/{}/{}",
user.belongs_to.borrow().urls.api,
@ -209,6 +194,6 @@ impl ReactionMeta {
user_id
);
let request = Client::new().delete(url).bearer_auth(user.token());
handle_request_as_option(request, user, crate::api::limits::LimitType::Channel).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Channel).await
}
}

View File

@ -21,16 +21,16 @@ pub async fn handle_request(
.await
}
/// Sends a request to wherever it needs to go. Returns [`None`] on success and
/// [`Some(ChorusLibError)`] on failure.
pub async fn handle_request_as_option(
/// Sends a request to wherever it needs to go. Returns [`Ok(())`] on success and
/// [`Err(ChorusLibError)`] on failure.
pub async fn handle_request_as_result(
request: RequestBuilder,
user: &mut UserMeta,
limit_type: LimitType,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
match handle_request(request, user, limit_type).await {
Ok(_) => None,
Err(e) => Some(ChorusLibError::InvalidResponseError {
Ok(_) => Ok(()),
Err(e) => Err(ChorusLibError::InvalidResponseError {
error: e.to_string(),
}),
}

View File

@ -4,7 +4,7 @@ use serde_json::to_string;
use crate::api::deserialize_response;
use crate::api::handle_request;
use crate::api::handle_request_as_option;
use crate::api::handle_request_as_result;
use crate::api::limits::Limits;
use crate::errors::ChorusLibError;
use crate::instance::UserMeta;
@ -50,7 +50,7 @@ impl Guild {
///
/// # Returns
///
/// An `Option` containing an `ChorusLibError` if an error occurred during the request, otherwise `None`.
/// An `Result` containing an `ChorusLibError` if an error occurred during the request, otherwise `()`.
///
/// # Example
///
@ -64,7 +64,7 @@ impl Guild {
/// None => println!("Guild deleted successfully"),
/// }
/// ```
pub async fn delete(user: &mut UserMeta, guild_id: &str) -> Option<ChorusLibError> {
pub async fn delete(user: &mut UserMeta, guild_id: &str) -> Result<(), ChorusLibError> {
let url = format!(
"{}/guilds/{}/delete/",
user.belongs_to.borrow().urls.api,
@ -73,7 +73,7 @@ impl Guild {
let request = reqwest::Client::new()
.post(url.clone())
.bearer_auth(user.token.clone());
handle_request_as_option(request, user, crate::api::limits::LimitType::Guild).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Guild).await
}
/// Sends a request to create a new channel in the guild.

View File

@ -1,7 +1,7 @@
use reqwest::Client;
use crate::{
api::{deserialize_response, handle_request_as_option},
api::{deserialize_response, handle_request_as_result},
errors::ChorusLibError,
instance::UserMeta,
types,
@ -50,13 +50,13 @@ impl types::GuildMember {
///
/// # Returns
///
/// An `Option` containing a `ChorusLibError` if the request fails, or `None` if the request succeeds.
/// An `Result` containing a `ChorusLibError` if the request fails, or `()` if the request succeeds.
pub async fn add_role(
user: &mut UserMeta,
guild_id: &str,
member_id: &str,
role_id: &str,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let url = format!(
"{}/guilds/{}/members/{}/roles/{}/",
user.belongs_to.borrow().urls.api,
@ -65,7 +65,7 @@ impl types::GuildMember {
role_id
);
let request = Client::new().put(url).bearer_auth(user.token());
handle_request_as_option(request, user, crate::api::limits::LimitType::Guild).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Guild).await
}
/// Removes a role from a guild member.
@ -79,13 +79,13 @@ impl types::GuildMember {
///
/// # Returns
///
/// An `Option` containing a `ChorusLibError` if the request fails, or `None` if the request succeeds.
/// A `Result` containing a `ChorusLibError` if the request fails, or `()` if the request succeeds.
pub async fn remove_role(
user: &mut UserMeta,
guild_id: &str,
member_id: &str,
role_id: &str,
) -> Option<crate::errors::ChorusLibError> {
) -> Result<(), crate::errors::ChorusLibError> {
let url = format!(
"{}/guilds/{}/members/{}/roles/{}/",
user.belongs_to.borrow().urls.api,
@ -94,6 +94,6 @@ impl types::GuildMember {
role_id
);
let request = Client::new().delete(url).bearer_auth(user.token());
handle_request_as_option(request, user, crate::api::limits::LimitType::Guild).await
handle_request_as_result(request, user, crate::api::limits::LimitType::Guild).await
}
}

View File

@ -2,7 +2,7 @@ use reqwest::Client;
use serde_json::to_string;
use crate::{
api::{deserialize_response, handle_request_as_option},
api::{deserialize_response, handle_request_as_result},
errors::ChorusLibError,
instance::UserMeta,
types::{self, CreateUserRelationshipSchema, RelationshipType},
@ -60,18 +60,18 @@ impl UserMeta {
/// * `schema` - A [`FriendRequestSendSchema`] struct that holds the information about the friend request to be sent.
///
/// # Returns
/// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails.
/// This function returns a [`Result`] that holds a [`ChorusLibError`] if the request fails.
pub async fn send_friend_request(
&mut self,
schema: types::FriendRequestSendSchema,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let url = format!(
"{}/users/@me/relationships/",
self.belongs_to.borrow().urls.api
);
let body = to_string(&schema).unwrap();
let request = Client::new().post(url).bearer_auth(self.token()).body(body);
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await
handle_request_as_result(request, self, crate::api::limits::LimitType::Global).await
}
/// Modifies the relationship between the authenticated user and the specified user.
@ -87,19 +87,19 @@ impl UserMeta {
/// * [`RelationshipType::Blocked`]: Blocks the specified user_id.
///
/// # Returns
/// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails.
/// This function returns an [`Result`] that holds a [`ChorusLibError`] if the request fails.
pub async fn modify_user_relationship(
&mut self,
user_id: &str,
relationship_type: RelationshipType,
) -> Option<ChorusLibError> {
) -> Result<(), ChorusLibError> {
let api_url = self.belongs_to.borrow().urls.api.clone();
match relationship_type {
RelationshipType::None => {
let request = Client::new()
.delete(format!("{}/users/@me/relationships/{}/", api_url, user_id))
.bearer_auth(self.token());
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await
handle_request_as_result(request, self, crate::api::limits::LimitType::Global).await
}
RelationshipType::Friends | RelationshipType::Incoming | RelationshipType::Outgoing => {
let body = CreateUserRelationshipSchema {
@ -111,7 +111,7 @@ impl UserMeta {
.put(format!("{}/users/@me/relationships/{}/", api_url, user_id))
.bearer_auth(self.token())
.body(to_string(&body).unwrap());
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await
handle_request_as_result(request, self, crate::api::limits::LimitType::Global).await
}
RelationshipType::Blocked => {
let body = CreateUserRelationshipSchema {
@ -123,9 +123,9 @@ impl UserMeta {
.put(format!("{}/users/@me/relationships/{}/", api_url, user_id))
.bearer_auth(self.token())
.body(to_string(&body).unwrap());
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await
handle_request_as_result(request, self, crate::api::limits::LimitType::Global).await
}
RelationshipType::Suggestion | RelationshipType::Implicit => None,
RelationshipType::Suggestion | RelationshipType::Implicit => Ok(()),
}
}
@ -136,14 +136,14 @@ impl UserMeta {
/// * `user_id` - A string slice that holds the ID of the user to remove the relationship with.
///
/// # Returns
/// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails.
pub async fn remove_relationship(&mut self, user_id: &str) -> Option<ChorusLibError> {
/// This function returns a [`Result`] that holds a [`ChorusLibError`] if the request fails.
pub async fn remove_relationship(&mut self, user_id: &str) -> Result<(), ChorusLibError> {
let url = format!(
"{}/users/@me/relationships/{}/",
self.belongs_to.borrow().urls.api,
user_id
);
let request = Client::new().delete(url).bearer_auth(self.token());
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await
handle_request_as_result(request, self, crate::api::limits::LimitType::Global).await
}
}

View File

@ -2,7 +2,7 @@ use reqwest::Client;
use serde_json::to_string;
use crate::{
api::{deserialize_response, handle_request_as_option, limits::Limits},
api::{deserialize_response, handle_request_as_result, limits::Limits},
errors::ChorusLibError,
instance::{Instance, UserMeta},
limit::LimitedRequester,
@ -71,15 +71,15 @@ impl UserMeta {
///
/// # Returns
///
/// Returns `None` if the user was successfully deleted, or an `ChorusLibError` if an error occurred.
pub async fn delete(mut self) -> Option<ChorusLibError> {
/// Returns `()` if the user was successfully deleted, or a `ChorusLibError` if an error occurred.
pub async fn delete(mut self) -> Result<(), ChorusLibError> {
let request = Client::new()
.post(format!(
"{}/users/@me/delete/",
self.belongs_to.borrow().urls.api
))
.bearer_auth(self.token());
handle_request_as_option(request, &mut self, crate::api::limits::LimitType::Ip).await
handle_request_as_result(request, &mut self, crate::api::limits::LimitType::Ip).await
}
}