modio/
reports.rs

1//! Reports interface
2use crate::prelude::*;
3use crate::types::id::{GameId, ModId, UserId};
4
5#[derive(Clone)]
6pub struct Reports {
7    modio: Modio,
8}
9
10impl Reports {
11    pub(crate) fn new(modio: Modio) -> Self {
12        Self { modio }
13    }
14
15    /// Submit a report for any resource on mod.io. [required: token]
16    pub async fn submit(self, report: Report) -> Result<()> {
17        self.modio
18            .request(Route::SubmitReport)
19            .form(&report)
20            .send::<Message>()
21            .await?;
22        Ok(())
23    }
24}
25
26pub struct Report {
27    pub name: String,
28    pub contact: Option<String>,
29    pub summary: String,
30    pub kind: ReportType,
31    pub resource: Resource,
32}
33
34pub enum ReportType {
35    Generic,
36    DMCA,
37    NotWorking,
38    RudeContent,
39    IllegalContent,
40    StolenContent,
41    FalseInformation,
42    Other,
43}
44
45pub enum Resource {
46    Game(GameId),
47    Mod(ModId),
48    User(UserId),
49}
50
51impl Report {
52    pub fn new<S: Into<String>>(
53        name: S,
54        contact: Option<S>,
55        summary: S,
56        kind: ReportType,
57        resource: Resource,
58    ) -> Self {
59        Self {
60            name: name.into(),
61            contact: contact.map(Into::into),
62            summary: summary.into(),
63            kind,
64            resource,
65        }
66    }
67}
68
69#[doc(hidden)]
70impl serde::ser::Serialize for Report {
71    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
72    where
73        S: serde::ser::Serializer,
74    {
75        use serde::ser::SerializeMap;
76
77        let (resource, id) = match self.resource {
78            Resource::Game(id) => ("games", id.get()),
79            Resource::Mod(id) => ("mods", id.get()),
80            Resource::User(id) => ("users", id.get()),
81        };
82        let kind = match self.kind {
83            ReportType::Generic => 0,
84            ReportType::DMCA => 1,
85            ReportType::NotWorking => 2,
86            ReportType::RudeContent => 3,
87            ReportType::IllegalContent => 4,
88            ReportType::StolenContent => 5,
89            ReportType::FalseInformation => 6,
90            ReportType::Other => 7,
91        };
92
93        let len = if self.contact.is_some() { 6 } else { 5 };
94        let mut map = serializer.serialize_map(Some(len))?;
95
96        if let Some(ref c) = self.contact {
97            map.serialize_entry("contact", c)?;
98        }
99        map.serialize_entry("resource", resource)?;
100        map.serialize_entry("id", &id)?;
101        map.serialize_entry("type", &kind)?;
102        map.serialize_entry("name", &self.name)?;
103        map.serialize_entry("summary", &self.summary)?;
104
105        map.end()
106    }
107}