wormhole_common/mods/schema/
browse.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Serialize, Deserialize, Debug, Clone)]
5pub struct ModVersion {
6    pub friendly_version: Option<String>,
7    pub game_version: Option<String>,
8    pub id: Option<i32>,
9    pub created: Option<String>,
10    pub download_path: Option<String>,
11    pub changelog: Option<String>,
12    pub downloads: Option<i32>,
13}
14
15impl Default for ModVersion {
16    fn default() -> Self {
17        return Self {
18            friendly_version: None,
19            game_version: None,
20            id: None,
21            created: None,
22            download_path: None,
23            changelog: None,
24            downloads: None,
25        };
26    }
27}
28
29impl ModVersion {
30    pub fn finish(&self) -> Self {
31        let out = ModVersion {
32            friendly_version: Some(self.friendly_version.clone().unwrap_or("".to_string())),
33            game_version: Some(self.friendly_version.clone().unwrap_or("".to_string())),
34            id: Some(self.id.unwrap_or(0)),
35            created: Some(self.created.clone().unwrap_or("".to_string())),
36            download_path: Some(self.download_path.clone().unwrap_or("".to_string())),
37            changelog: Some(self.changelog.clone().unwrap_or("".to_string())),
38            downloads: Some(self.downloads.unwrap_or(0)),
39        };
40
41        return out;
42    }
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone)]
46pub struct ModInfo {
47    pub name: Option<String>,
48    pub id: Option<i32>,
49    pub game: Option<String>,
50    pub game_id: Option<i32>,
51    pub short_description: Option<String>,
52    pub downloads: Option<i32>,
53    pub followers: Option<i32>,
54    pub author: Option<String>,
55    pub default_version_id: Option<i32>,
56    pub shared_authors: Option<Vec<Value>>,
57    pub background: Option<String>,
58    pub bg_offset_y: Option<i32>,
59    pub license: Option<String>,
60    pub website: Option<String>,
61    pub donations: Option<String>,
62    pub source_code: Option<String>,
63    pub url: Option<String>,
64    pub versions: Option<Vec<ModVersion>>,
65    pub description: Option<String>,
66}
67
68impl Default for ModInfo {
69    fn default() -> Self {
70        return Self {
71            name: None,
72            id: None,
73            game: None,
74            game_id: None,
75            short_description: None,
76            downloads: None,
77            followers: None,
78            author: None,
79            default_version_id: None,
80            shared_authors: None,
81            background: None,
82            bg_offset_y: None,
83            license: None,
84            website: None,
85            donations: None,
86            source_code: None,
87            url: None,
88            versions: None,
89            description: None,
90        };
91    }
92}
93
94impl ModInfo {
95    pub fn finish(&self, browse: bool) -> Self {
96        let mut out = ModInfo::default();
97        let mut _versions = Vec::new();
98        let mut bg_img = "https://spacedock.info/static/background.png".to_string();
99
100        if browse {
101            bg_img = "https://spacedock.info/static/background-s.png".to_string();
102        }
103
104        if let Some(versions) = self.versions.clone() {
105            _versions = versions.iter().map(|v| v.finish()).collect();
106        }
107
108        out.name = Some(self.name.clone().unwrap_or("".to_string()));
109        out.id = Some(self.id.unwrap_or(0));
110        out.game = Some(self.game.clone().unwrap_or("".to_string()));
111        out.game_id = Some(self.game_id.unwrap_or(0));
112        out.short_description = Some(self.short_description.clone().unwrap_or("".to_string()));
113        out.downloads = Some(self.downloads.unwrap_or(0));
114        out.followers = Some(self.followers.unwrap_or(0));
115        out.author = Some(self.author.clone().unwrap_or("".to_string()));
116        out.default_version_id = Some(self.default_version_id.unwrap_or(0));
117        out.shared_authors = Some(self.shared_authors.clone().unwrap_or(Vec::new()));
118
119        out.background = Some(self.background.clone().unwrap_or(bg_img));
120
121        out.bg_offset_y = Some(self.bg_offset_y.unwrap_or(0));
122        out.license = Some(self.license.clone().unwrap_or("".to_string()));
123        out.website = Some(self.website.clone().unwrap_or("".to_string()));
124        out.donations = Some(self.donations.clone().unwrap_or("".to_string()));
125        out.source_code = Some(self.source_code.clone().unwrap_or("".to_string()));
126        out.url = Some(self.url.clone().unwrap_or("".to_string()));
127        out.versions = Some(_versions);
128        out.description = Some(self.description.clone().unwrap_or("".to_string()));
129
130        return out;
131    }
132}
133
134#[derive(Serialize, Deserialize, Debug, Clone)]
135pub struct BrowseResult {
136    pub result: Option<Vec<ModInfo>>,
137    pub count: Option<i32>,
138    pub pages: Option<i32>,
139    pub page: Option<i32>,
140}
141
142impl Default for BrowseResult {
143    fn default() -> Self {
144        return Self {
145            result: None,
146            count: None,
147            pages: None,
148            page: None,
149        };
150    }
151}
152
153impl BrowseResult {
154    pub fn finish(&self) -> Self {
155        let mut out = BrowseResult::default();
156        let mut _res = Vec::new();
157
158        if let Some(result) = self.result.clone() {
159            _res = result.iter().map(|v| v.finish(true)).collect();
160        }
161
162        out.result = Some(_res);
163        out.count = Some(self.count.unwrap_or(0));
164        out.pages = Some(self.pages.unwrap_or(0));
165        out.page = Some(self.page.unwrap_or(0));
166
167        return out;
168    }
169}
170
171#[derive(Debug, Deserialize)]
172pub struct BrowseModInfo {
173    pub name: String,
174    pub id: i32,
175    pub game: String,
176    pub game_id: i32,
177    pub short_description: String,
178    pub downloads: i32,
179    pub followers: i32,
180    pub author: String,
181    pub default_version_id: i32,
182    pub shared_authors: Vec<Value>,
183    pub background: String,
184    pub bg_offset_y: String,
185    pub license: String,
186    pub website: String,
187    pub donations: String,
188    pub source_code: String,
189    pub url: String,
190    pub versions: Vec<ModVersion>,
191}