Skip to main content

twilight_http/request/channel/invite/
get_invite.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::guild::invite::Invite;
11
12struct GetInviteFields {
13    with_counts: bool,
14    with_expiration: bool,
15}
16
17/// Get information about an invite by its code.
18///
19/// If [`with_counts`] is called, the returned invite will contain approximate
20/// member counts. If [`with_expiration`] is called, it will contain the
21/// expiration date.
22///
23/// # Examples
24///
25/// ```no_run
26/// use twilight_http::Client;
27///
28/// # #[tokio::main]
29/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
30/// let client = Client::new("my token".to_owned());
31///
32/// let invite = client.invite("code").with_counts().await?;
33/// # Ok(()) }
34/// ```
35///
36/// [`with_counts`]: Self::with_counts
37/// [`with_expiration`]: Self::with_expiration
38#[must_use = "requests must be configured and executed"]
39pub struct GetInvite<'a> {
40    code: &'a str,
41    fields: GetInviteFields,
42    http: &'a Client,
43}
44
45impl<'a> GetInvite<'a> {
46    pub(crate) const fn new(http: &'a Client, code: &'a str) -> Self {
47        Self {
48            code,
49            fields: GetInviteFields {
50                with_counts: false,
51                with_expiration: false,
52            },
53            http,
54        }
55    }
56
57    /// Whether the invite returned should contain approximate member counts.
58    pub const fn with_counts(mut self) -> Self {
59        self.fields.with_counts = true;
60
61        self
62    }
63
64    /// Whether the invite returned should contain its expiration date.
65    pub const fn with_expiration(mut self) -> Self {
66        self.fields.with_expiration = true;
67
68        self
69    }
70}
71
72#[cfg(not(target_os = "wasi"))]
73impl IntoFuture for GetInvite<'_> {
74    type Output = Result<Response<Invite>, Error>;
75
76    type IntoFuture = ResponseFuture<Invite>;
77
78    fn into_future(self) -> Self::IntoFuture {
79        let http = self.http;
80
81        match self.try_into_request() {
82            Ok(request) => http.request(request),
83            Err(source) => ResponseFuture::error(source),
84        }
85    }
86}
87
88impl TryIntoRequest for GetInvite<'_> {
89    fn try_into_request(self) -> Result<Request, Error> {
90        Ok(Request::from_route(&Route::GetInviteWithExpiration {
91            code: self.code,
92            with_counts: self.fields.with_counts,
93            with_expiration: self.fields.with_expiration,
94        }))
95    }
96}