twilight_http/request/user/
create_private_channel.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11    channel::Channel,
12    id::{marker::UserMarker, Id},
13};
14
15#[derive(Serialize)]
16struct CreatePrivateChannelFields {
17    recipient_id: Id<UserMarker>,
18}
19
20/// Create a DM channel with a user.
21#[must_use = "requests must be configured and executed"]
22pub struct CreatePrivateChannel<'a> {
23    fields: CreatePrivateChannelFields,
24    http: &'a Client,
25}
26
27impl<'a> CreatePrivateChannel<'a> {
28    pub(crate) const fn new(http: &'a Client, recipient_id: Id<UserMarker>) -> Self {
29        Self {
30            fields: CreatePrivateChannelFields { recipient_id },
31            http,
32        }
33    }
34}
35
36impl IntoFuture for CreatePrivateChannel<'_> {
37    type Output = Result<Response<Channel>, Error>;
38
39    type IntoFuture = ResponseFuture<Channel>;
40
41    fn into_future(self) -> Self::IntoFuture {
42        let http = self.http;
43
44        match self.try_into_request() {
45            Ok(request) => http.request(request),
46            Err(source) => ResponseFuture::error(source),
47        }
48    }
49}
50
51impl TryIntoRequest for CreatePrivateChannel<'_> {
52    fn try_into_request(self) -> Result<Request, Error> {
53        Request::builder(&Route::CreatePrivateChannel)
54            .json(&self.fields)
55            .build()
56    }
57}