modio/request/mods/comments/
add_mod_comment.rs

1use std::future::IntoFuture;
2
3use serde_derive::Serialize;
4
5use crate::client::Client;
6use crate::request::{Output, RequestBuilder, Route};
7use crate::response::ResponseFuture;
8use crate::types::id::{CommentId, GameId, ModId};
9use crate::types::mods::Comment;
10
11/// Add a comment for a mod.
12pub struct AddModComment<'a> {
13    http: &'a Client,
14    game_id: GameId,
15    mod_id: ModId,
16    fields: AddModCommentFields<'a>,
17}
18
19#[derive(Serialize)]
20struct AddModCommentFields<'a> {
21    content: &'a str,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    reply_id: Option<CommentId>,
24}
25
26impl<'a> AddModComment<'a> {
27    pub(crate) const fn new(
28        http: &'a Client,
29        game_id: GameId,
30        mod_id: ModId,
31        content: &'a str,
32    ) -> Self {
33        Self {
34            http,
35            game_id,
36            mod_id,
37            fields: AddModCommentFields {
38                content,
39                reply_id: None,
40            },
41        }
42    }
43
44    pub const fn reply_id(mut self, reply_id: CommentId) -> Self {
45        self.fields.reply_id = Some(reply_id);
46        self
47    }
48}
49
50impl IntoFuture for AddModComment<'_> {
51    type Output = Output<Comment>;
52    type IntoFuture = ResponseFuture<Comment>;
53
54    fn into_future(self) -> Self::IntoFuture {
55        let route = Route::AddModComment {
56            game_id: self.game_id,
57            mod_id: self.mod_id,
58        };
59        match RequestBuilder::from_route(&route).form(&self.fields) {
60            Ok(req) => self.http.request(req),
61            Err(err) => ResponseFuture::failed(err),
62        }
63    }
64}