Skip to content

Commit

Permalink
Use NonMax in HTTP function signatures (#2803)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Mar 17, 2024
1 parent 23a2f96 commit 3ab58dc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/builder/get_messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use nonmax::NonMaxU8;

#[cfg(feature = "http")]
use crate::http::{CacheHttp, MessagePagination};
#[cfg(feature = "http")]
Expand Down Expand Up @@ -48,7 +50,7 @@ use crate::model::prelude::*;
#[must_use]
pub struct GetMessages {
search_filter: Option<SearchFilter>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
}

impl GetMessages {
Expand Down Expand Up @@ -83,7 +85,7 @@ impl GetMessages {
/// **Note**: This field is capped to 100 messages due to a Discord limitation. If an amount
/// larger than 100 is supplied, it will be truncated.
pub fn limit(mut self, limit: u8) -> Self {
self.limit = Some(limit.min(100));
self.limit = NonMaxU8::new(limit.min(100));
self
}

Expand Down
26 changes: 13 additions & 13 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use arrayvec::ArrayVec;
use nonmax::NonMaxU16;
use nonmax::{NonMaxU16, NonMaxU8};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::header::{HeaderMap as Headers, HeaderValue};
#[cfg(feature = "utils")]
Expand Down Expand Up @@ -2284,14 +2284,14 @@ impl Http {
&self,
guild_id: GuildId,
target: Option<UserPagination>,
limit: Option<u8>,
limit: Option<NonMaxU16>,
) -> Result<Vec<Ban>> {
let id_str;
let limit_str;
let mut params = ArrayVec::<_, 2>::new();

if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", limit_str.as_str()));
}

Expand Down Expand Up @@ -2325,7 +2325,7 @@ impl Http {
action_type: Option<audit_log::Action>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
) -> Result<AuditLogs> {
let (action_type_str, before_str, limit_str, user_id_str);
let mut params = ArrayVec::<_, 4>::new();
Expand All @@ -2338,7 +2338,7 @@ impl Http {
params.push(("before", &before_str));
}
if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", &limit_str));
}
if let Some(user_id) = user_id {
Expand Down Expand Up @@ -2811,7 +2811,7 @@ impl Http {
sku_ids: Option<Vec<SkuId>>,
before: Option<EntitlementId>,
after: Option<EntitlementId>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
guild_id: Option<GuildId>,
exclude_ended: Option<bool>,
) -> Result<Vec<Entitlement>> {
Expand All @@ -2835,7 +2835,7 @@ impl Http {
params.push(("after", &after_str));
}
if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", &limit_str));
}
if let Some(guild_id) = guild_id {
Expand Down Expand Up @@ -3293,14 +3293,14 @@ impl Http {
&self,
guild_id: GuildId,
event_id: ScheduledEventId,
limit: Option<u64>,
limit: Option<NonMaxU8>,
target: Option<UserPagination>,
with_member: Option<bool>,
) -> Result<Vec<ScheduledEventUser>> {
let (limit_str, with_member_str, id_str);
let mut params = ArrayVec::<_, 3>::new();
if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", limit_str.as_str()));
}
if let Some(with_member) = with_member {
Expand Down Expand Up @@ -3403,12 +3403,12 @@ impl Http {
pub async fn get_guilds(
&self,
target: Option<GuildPagination>,
limit: Option<u64>,
limit: Option<NonMaxU8>,
) -> Result<Vec<GuildInfo>> {
let (limit_str, id_str);
let mut params = ArrayVec::<_, 2>::new();
if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", limit_str.as_str()));
}
if let Some(target) = target {
Expand Down Expand Up @@ -3544,13 +3544,13 @@ impl Http {
&self,
channel_id: ChannelId,
target: Option<MessagePagination>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
) -> Result<Vec<Message>> {
let (limit_str, id_str);
let mut params = ArrayVec::<_, 2>::new();

if let Some(limit) = limit {
limit_str = limit.to_arraystring();
limit_str = limit.get().to_arraystring();
params.push(("limit", limit_str.as_str()));
}

Expand Down
10 changes: 5 additions & 5 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

#[cfg(feature = "model")]
use futures::stream::Stream;
use nonmax::NonMaxU16;
use nonmax::{NonMaxU16, NonMaxU8};
use serde_json::json;

#[cfg(feature = "model")]
Expand Down Expand Up @@ -238,7 +238,7 @@ impl GuildId {
self,
http: &Http,
target: Option<UserPagination>,
limit: Option<u8>,
limit: Option<NonMaxU16>,
) -> Result<Vec<Ban>> {
http.get_bans(self, target, limit).await
}
Expand All @@ -259,7 +259,7 @@ impl GuildId {
action_type: Option<audit_log::Action>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
) -> Result<AuditLogs> {
http.get_audit_logs(self, action_type, user_id, before, limit).await
}
Expand Down Expand Up @@ -1170,7 +1170,7 @@ impl GuildId {
self,
http: &Http,
event_id: ScheduledEventId,
limit: Option<u64>,
limit: Option<NonMaxU8>,
) -> Result<Vec<ScheduledEventUser>> {
http.get_scheduled_event_users(self, event_id, limit, None, None).await
}
Expand All @@ -1190,7 +1190,7 @@ impl GuildId {
self,
http: &Http,
event_id: ScheduledEventId,
limit: Option<u64>,
limit: Option<NonMaxU8>,
target: Option<UserPagination>,
with_member: Option<bool>,
) -> Result<Vec<ScheduledEventUser>> {
Expand Down
10 changes: 5 additions & 5 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod welcome_screen;
#[cfg(feature = "model")]
use std::borrow::Cow;

use nonmax::{NonMaxU16, NonMaxU64};
use nonmax::{NonMaxU16, NonMaxU64, NonMaxU8};
#[cfg(feature = "model")]
use tracing::{error, warn};

Expand Down Expand Up @@ -484,7 +484,7 @@ impl Guild {
&self,
cache_http: impl CacheHttp,
target: Option<UserPagination>,
limit: Option<u8>,
limit: Option<NonMaxU16>,
) -> Result<Vec<Ban>> {
#[cfg(feature = "cache")]
{
Expand Down Expand Up @@ -529,7 +529,7 @@ impl Guild {
action_type: Option<audit_log::Action>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
) -> Result<AuditLogs> {
self.id.audit_logs(http, action_type, user_id, before, limit).await
}
Expand Down Expand Up @@ -1947,7 +1947,7 @@ impl Guild {
&self,
http: &Http,
event_id: ScheduledEventId,
limit: Option<u64>,
limit: Option<NonMaxU8>,
) -> Result<Vec<ScheduledEventUser>> {
self.id.scheduled_event_users(http, event_id, limit).await
}
Expand All @@ -1967,7 +1967,7 @@ impl Guild {
&self,
http: &Http,
event_id: ScheduledEventId,
limit: Option<u64>,
limit: Option<NonMaxU8>,
target: Option<UserPagination>,
with_member: Option<bool>,
) -> Result<Vec<ScheduledEventUser>> {
Expand Down
6 changes: 3 additions & 3 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nonmax::{NonMaxU16, NonMaxU64};
use nonmax::{NonMaxU16, NonMaxU64, NonMaxU8};
use serde::Serialize;

#[cfg(feature = "model")]
Expand Down Expand Up @@ -314,7 +314,7 @@ impl PartialGuild {
&self,
http: &Http,
target: Option<UserPagination>,
limit: Option<u8>,
limit: Option<NonMaxU16>,
) -> Result<Vec<Ban>> {
self.id.bans(http, target, limit).await
}
Expand All @@ -335,7 +335,7 @@ impl PartialGuild {
action_type: Option<audit_log::Action>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>,
limit: Option<NonMaxU8>,
) -> Result<AuditLogs> {
self.id.audit_logs(http, action_type, user_id, before, limit).await
}
Expand Down

0 comments on commit 3ab58dc

Please sign in to comment.