twilight_http/request/channel/message/
get_channel_messages_configured.rs

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