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