modio/request/files/
edit_file.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::files::File;
9use crate::types::id::{FileId, GameId, ModId};
10
11/// Edit the details of a modfile.
12pub struct EditFile<'a> {
13    http: &'a Client,
14    game_id: GameId,
15    mod_id: ModId,
16    file_id: FileId,
17    fields: EditFileFields<'a>,
18}
19
20#[derive(Serialize)]
21struct EditFileFields<'a> {
22    #[serde(skip_serializing_if = "Option::is_none")]
23    active: Option<bool>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    changelog: Option<&'a str>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    metadata_blob: Option<&'a str>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    version: Option<&'a str>,
30}
31
32impl<'a> EditFile<'a> {
33    pub(crate) const fn new(
34        http: &'a Client,
35        game_id: GameId,
36        mod_id: ModId,
37        file_id: FileId,
38    ) -> Self {
39        Self {
40            http,
41            game_id,
42            mod_id,
43            file_id,
44            fields: EditFileFields {
45                active: None,
46                changelog: None,
47                metadata_blob: None,
48                version: None,
49            },
50        }
51    }
52
53    pub const fn active(mut self, active: bool) -> Self {
54        self.fields.active = Some(active);
55        self
56    }
57
58    pub const fn changelog(mut self, changelog: &'a str) -> Self {
59        self.fields.changelog = Some(changelog);
60        self
61    }
62
63    pub const fn metadata_blob(mut self, metadata: &'a str) -> Self {
64        self.fields.metadata_blob = Some(metadata);
65        self
66    }
67
68    pub const fn version(mut self, version: &'a str) -> Self {
69        self.fields.version = Some(version);
70        self
71    }
72}
73
74impl IntoFuture for EditFile<'_> {
75    type Output = Output<File>;
76    type IntoFuture = ResponseFuture<File>;
77
78    fn into_future(self) -> Self::IntoFuture {
79        let route = Route::EditFile {
80            game_id: self.game_id,
81            mod_id: self.mod_id,
82            file_id: self.file_id,
83        };
84        match RequestBuilder::from_route(&route).form(&self.fields) {
85            Ok(req) => self.http.request(req),
86            Err(err) => ResponseFuture::failed(err),
87        }
88    }
89}