Skip to main content

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