Skip to main content

twilight_http/request/guild/
get_guild.rs

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