Skip to main content

twilight_http/request/application/emoji/
add_emoji.rs

1use std::future::IntoFuture;
2
3#[cfg(not(target_os = "wasi"))]
4use crate::response::{Response, ResponseFuture};
5use crate::{
6    Client, Error,
7    request::{Request, TryIntoRequest},
8    routing::Route,
9};
10
11use serde::Serialize;
12use twilight_model::{
13    guild::Emoji,
14    id::{Id, marker::ApplicationMarker},
15};
16
17#[derive(Serialize)]
18struct AddApplicationEmojiFields<'a> {
19    image: &'a str,
20    name: &'a str,
21}
22
23pub struct AddApplicationEmoji<'a> {
24    fields: AddApplicationEmojiFields<'a>,
25    application_id: Id<ApplicationMarker>,
26    http: &'a Client,
27}
28
29impl<'a> AddApplicationEmoji<'a> {
30    pub(crate) const fn new(
31        http: &'a Client,
32        application_id: Id<ApplicationMarker>,
33        name: &'a str,
34        image: &'a str,
35    ) -> Self {
36        Self {
37            fields: AddApplicationEmojiFields { image, name },
38            application_id,
39            http,
40        }
41    }
42}
43
44#[cfg(not(target_os = "wasi"))]
45impl IntoFuture for AddApplicationEmoji<'_> {
46    type Output = Result<Response<Emoji>, Error>;
47
48    type IntoFuture = ResponseFuture<Emoji>;
49
50    fn into_future(self) -> Self::IntoFuture {
51        let http = self.http;
52
53        match self.try_into_request() {
54            Ok(request) => http.request(request),
55            Err(source) => ResponseFuture::error(source),
56        }
57    }
58}
59
60impl TryIntoRequest for AddApplicationEmoji<'_> {
61    fn try_into_request(self) -> Result<Request, Error> {
62        let mut request = Request::builder(&Route::AddApplicationEmoji {
63            application_id: self.application_id.get(),
64        });
65
66        request = request.json(&self.fields);
67
68        request.build()
69    }
70}