twilight_http/request/application/emoji/
add_emoji.rs

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