Skip to main content

twilight_http/request/guild/emoji/
get_emoji.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 std::future::IntoFuture;
10use twilight_model::{
11    guild::Emoji,
12    id::{
13        Id,
14        marker::{EmojiMarker, GuildMarker},
15    },
16};
17
18/// Get an emoji for a guild by the the guild's ID and emoji's ID.
19///
20/// # Examples
21///
22/// Get emoji `100` from guild `50`:
23///
24/// ```no_run
25/// use twilight_http::Client;
26/// use twilight_model::id::Id;
27///
28/// # #[tokio::main]
29/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
30/// let client = Client::new("my token".to_owned());
31///
32/// let guild_id = Id::new(50);
33/// let emoji_id = Id::new(100);
34///
35/// client.emoji(guild_id, emoji_id).await?;
36/// # Ok(()) }
37/// ```
38#[must_use = "requests must be configured and executed"]
39pub struct GetEmoji<'a> {
40    emoji_id: Id<EmojiMarker>,
41    guild_id: Id<GuildMarker>,
42    http: &'a Client,
43}
44
45impl<'a> GetEmoji<'a> {
46    pub(crate) const fn new(
47        http: &'a Client,
48        guild_id: Id<GuildMarker>,
49        emoji_id: Id<EmojiMarker>,
50    ) -> Self {
51        Self {
52            emoji_id,
53            guild_id,
54            http,
55        }
56    }
57}
58
59#[cfg(not(target_os = "wasi"))]
60impl IntoFuture for GetEmoji<'_> {
61    type Output = Result<Response<Emoji>, Error>;
62
63    type IntoFuture = ResponseFuture<Emoji>;
64
65    fn into_future(self) -> Self::IntoFuture {
66        let http = self.http;
67
68        match self.try_into_request() {
69            Ok(request) => http.request(request),
70            Err(source) => ResponseFuture::error(source),
71        }
72    }
73}
74
75impl TryIntoRequest for GetEmoji<'_> {
76    fn try_into_request(self) -> Result<Request, Error> {
77        Ok(Request::from_route(&Route::GetEmoji {
78            emoji_id: self.emoji_id.get(),
79            guild_id: self.guild_id.get(),
80        }))
81    }
82}