twilight_http/request/user/
get_current_user_guilds.rs

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