Skip to main content

twilight_http/request/channel/message/
get_channel_messages_configured.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::ListBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    channel::Message,
12    id::{
13        Id,
14        marker::{ChannelMarker, MessageMarker},
15    },
16};
17use twilight_validate::request::{
18    ValidationError, get_channel_messages_limit as validate_get_channel_messages_limit,
19};
20
21struct GetChannelMessagesConfiguredFields {
22    limit: Option<u16>,
23}
24
25/// This struct is returned when one of `after`, `around`, or `before` is specified in
26/// [`GetChannelMessages`].
27///
28/// [`GetChannelMessages`]: super::GetChannelMessages
29// nb: after, around, and before are mutually exclusive, so we use this
30// "configured" request to utilize the type system to prevent these from being
31// set in combination.
32#[must_use = "requests must be configured and executed"]
33pub struct GetChannelMessagesConfigured<'a> {
34    after: Option<Id<MessageMarker>>,
35    around: Option<Id<MessageMarker>>,
36    before: Option<Id<MessageMarker>>,
37    channel_id: Id<ChannelMarker>,
38    fields: Result<GetChannelMessagesConfiguredFields, ValidationError>,
39    http: &'a Client,
40}
41
42impl<'a> GetChannelMessagesConfigured<'a> {
43    pub(crate) fn new(
44        http: &'a Client,
45        channel_id: Id<ChannelMarker>,
46        after: Option<Id<MessageMarker>>,
47        around: Option<Id<MessageMarker>>,
48        before: Option<Id<MessageMarker>>,
49        limit: Result<Option<u16>, ValidationError>,
50    ) -> Self {
51        Self {
52            after,
53            around,
54            before,
55            channel_id,
56            fields: limit.map(|limit| GetChannelMessagesConfiguredFields { limit }),
57            http,
58        }
59    }
60
61    /// Set the maximum number of messages to retrieve.
62    ///
63    /// The minimum is 1 and the maximum is 100.
64    ///
65    /// # Errors
66    ///
67    /// Returns an error of type [`GetChannelMessages`] error type if the amount
68    /// is less than 1 or greater than 100.
69    ///
70    /// [`GetChannelMessages`]: twilight_validate::request::ValidationErrorType::GetChannelMessages
71    pub fn limit(mut self, limit: u16) -> Self {
72        self.fields = self.fields.and_then(|mut fields| {
73            validate_get_channel_messages_limit(limit)?;
74            fields.limit = Some(limit);
75
76            Ok(fields)
77        });
78
79        self
80    }
81}
82
83#[cfg(not(target_os = "wasi"))]
84impl IntoFuture for GetChannelMessagesConfigured<'_> {
85    type Output = Result<Response<ListBody<Message>>, Error>;
86
87    type IntoFuture = ResponseFuture<ListBody<Message>>;
88
89    fn into_future(self) -> Self::IntoFuture {
90        let http = self.http;
91
92        match self.try_into_request() {
93            Ok(request) => http.request(request),
94            Err(source) => ResponseFuture::error(source),
95        }
96    }
97}
98
99impl TryIntoRequest for GetChannelMessagesConfigured<'_> {
100    fn try_into_request(self) -> Result<Request, Error> {
101        let fields = self.fields.map_err(Error::validation)?;
102
103        Ok(Request::from_route(&Route::GetMessages {
104            after: self.after.map(Id::get),
105            around: self.around.map(Id::get),
106            before: self.before.map(Id::get),
107            channel_id: self.channel_id.get(),
108            limit: fields.limit,
109        }))
110    }
111}