Skip to main content

twilight_http/request/user/
get_current_user_guilds.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::ListBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    id::{Id, marker::GuildMarker},
12    user::CurrentUserGuild,
13};
14use twilight_validate::request::{
15    ValidationError, get_current_user_guilds_limit as validate_get_current_user_guilds_limit,
16};
17
18struct GetCurrentUserGuildsFields {
19    after: Option<Id<GuildMarker>>,
20    before: Option<Id<GuildMarker>>,
21    limit: Option<u16>,
22}
23
24/// Returns a list of guilds for the current user.
25///
26/// # Examples
27///
28/// Get the first 25 guilds with an ID after `300` and before
29/// `400`:
30///
31/// ```no_run
32/// use twilight_http::Client;
33/// use twilight_model::id::Id;
34///
35/// # #[tokio::main]
36/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
37/// let client = Client::new("my token".to_owned());
38///
39/// let after = Id::new(300);
40/// let before = Id::new(400);
41/// let guilds = client
42///     .current_user_guilds()
43///     .after(after)
44///     .before(before)
45///     .limit(25)
46///     .await?;
47/// # Ok(()) }
48/// ```
49#[must_use = "requests must be configured and executed"]
50pub struct GetCurrentUserGuilds<'a> {
51    fields: Result<GetCurrentUserGuildsFields, ValidationError>,
52    http: &'a Client,
53}
54
55impl<'a> GetCurrentUserGuilds<'a> {
56    pub(crate) const fn new(http: &'a Client) -> Self {
57        Self {
58            fields: Ok(GetCurrentUserGuildsFields {
59                after: None,
60                before: None,
61                limit: None,
62            }),
63            http,
64        }
65    }
66
67    /// Get guilds after this guild id.
68    pub const fn after(mut self, guild_id: Id<GuildMarker>) -> Self {
69        if let Ok(fields) = self.fields.as_mut() {
70            fields.after = Some(guild_id);
71        }
72
73        self
74    }
75
76    /// Get guilds before this guild id.
77    pub const fn before(mut self, guild_id: Id<GuildMarker>) -> Self {
78        if let Ok(fields) = self.fields.as_mut() {
79            fields.before = Some(guild_id);
80        }
81
82        self
83    }
84
85    /// Set the maximum number of guilds to retrieve.
86    ///
87    /// The minimum is 1 and the maximum is 200. See
88    /// [Discord Docs/Get Current User Guilds].
89    ///
90    /// # Errors
91    ///
92    /// Returns an error of type [`GetCurrentUserGuilds`] if the name length is
93    /// too short or too long.
94    ///
95    /// [`GetCurrentUserGuilds`]: twilight_validate::request::ValidationErrorType::GetCurrentUserGuilds
96    /// [Discord Docs/Get Current User Guilds]: https://discordapp.com/developers/docs/resources/user#get-current-user-guilds-query-string-params
97    pub fn limit(mut self, limit: u16) -> Self {
98        self.fields = self.fields.and_then(|mut fields| {
99            validate_get_current_user_guilds_limit(limit)?;
100            fields.limit = Some(limit);
101
102            Ok(fields)
103        });
104
105        self
106    }
107}
108
109#[cfg(not(target_os = "wasi"))]
110impl IntoFuture for GetCurrentUserGuilds<'_> {
111    type Output = Result<Response<ListBody<CurrentUserGuild>>, Error>;
112
113    type IntoFuture = ResponseFuture<ListBody<CurrentUserGuild>>;
114
115    fn into_future(self) -> Self::IntoFuture {
116        let http = self.http;
117
118        match self.try_into_request() {
119            Ok(request) => http.request(request),
120            Err(source) => ResponseFuture::error(source),
121        }
122    }
123}
124
125impl TryIntoRequest for GetCurrentUserGuilds<'_> {
126    fn try_into_request(self) -> Result<Request, Error> {
127        let fields = self.fields.map_err(Error::validation)?;
128
129        Ok(Request::from_route(&Route::GetGuilds {
130            after: fields.after.map(Id::get),
131            before: fields.before.map(Id::get),
132            limit: fields.limit,
133        }))
134    }
135}