twilight_http/request/guild/
get_guild.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::{
10    guild::Guild,
11    id::{marker::GuildMarker, Id},
12};
13
14struct GetGuildFields {
15    with_counts: bool,
16}
17
18/// Get information about a guild.
19#[must_use = "requests must be configured and executed"]
20pub struct GetGuild<'a> {
21    fields: GetGuildFields,
22    guild_id: Id<GuildMarker>,
23    http: &'a Client,
24}
25
26impl<'a> GetGuild<'a> {
27    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
28        Self {
29            fields: GetGuildFields { with_counts: false },
30            guild_id,
31            http,
32        }
33    }
34
35    /// Sets if you want to receive `approximate_member_count` and `approximate_presence_count` in
36    /// the guild structure.
37    pub const fn with_counts(mut self, with: bool) -> Self {
38        self.fields.with_counts = with;
39
40        self
41    }
42}
43
44impl IntoFuture for GetGuild<'_> {
45    type Output = Result<Response<Guild>, Error>;
46
47    type IntoFuture = ResponseFuture<Guild>;
48
49    fn into_future(self) -> Self::IntoFuture {
50        let http = self.http;
51
52        match self.try_into_request() {
53            Ok(request) => http.request(request),
54            Err(source) => ResponseFuture::error(source),
55        }
56    }
57}
58
59impl TryIntoRequest for GetGuild<'_> {
60    fn try_into_request(self) -> Result<Request, Error> {
61        Ok(Request::from_route(&Route::GetGuild {
62            guild_id: self.guild_id.get(),
63            with_counts: self.fields.with_counts,
64        }))
65    }
66}