mine_data_structs/
curse.rs

1use std::path::{Path, PathBuf};
2
3#[cfg(feature="serde")]
4use serde::{Deserialize, Serialize};
5#[cfg(feature="serde")]
6use std::fs::read_to_string;
7#[cfg(feature="serde")]
8use serde_json::Error;
9
10// Modpacks
11
12#[cfg_attr(feature="serde", derive(Deserialize, Serialize), serde(rename_all = "camelCase"))]
13#[derive(Debug, Clone)]
14pub struct CursePackFiles {
15    project_id: usize,
16    file_id: usize,
17}
18
19impl CursePackFiles {
20    pub fn get_project_id(&self) -> usize {
21        self.project_id
22    }
23
24    pub fn get_file_id(&self) -> usize {
25        self.file_id
26    }
27}
28
29#[cfg_attr(feature="serde", derive(Deserialize, Serialize))]
30#[derive(Debug, Clone)]
31pub struct CursePack {
32    pub name: String,
33    pub author: String,
34    files: Vec<CursePackFiles>,
35}
36
37impl CursePack {
38    pub fn get_files(&self) -> &Vec<CursePackFiles> {
39        &self.files
40    }
41}
42
43#[cfg(feature="serde")]
44fn deserializ_pack(path: &str) -> Result<CursePack, Error> {
45    let aux = read_to_string(path).unwrap();
46    serde_json::from_str(&aux)
47}
48
49#[cfg(feature="serde")]
50pub fn load_curse_pack(pack_path: &str) -> Option<CursePack> {
51    match read_to_string(pack_path) {
52        Ok(_) => {}
53        Err(error) => {
54            eprintln!("Error reading the pack \n\n{error}");
55            return None;
56        }
57    };
58
59    match deserializ_pack(pack_path) {
60        Ok(e) => Some(e),
61        Err(error) => {
62            eprintln!("Error deserializing the pack \n\n{error}");
63            None
64        }
65    }
66}
67
68// Mods
69
70#[cfg_attr(feature="serde", derive(Deserialize, Serialize), serde(rename_all = "camelCase"))]
71#[derive(Clone, Debug)]
72/// This struct only contains data about the mod logo.
73pub struct Logo {
74    pub id: usize,
75    pub mod_id: usize,
76    pub thumbnail_url: String,
77    pub url: String,
78}
79
80#[cfg_attr(feature="serde", derive(Deserialize, Serialize), serde(rename_all = "camelCase"))]
81#[derive(Clone, Debug)]
82/// This struct contains the data about the specific file of a mod
83pub struct CurseFile {
84    pub id: usize,
85    pub game_id: Option<usize>,
86    pub mod_id: usize,
87    pub display_name: String,
88    pub file_name: PathBuf,
89    pub download_url: Option<String>,
90    pub file_length: usize,
91    pub game_versions: Vec<String>,
92}
93
94impl CurseFile {
95    pub fn get_id(&self) -> usize {
96        self.id
97    }
98
99    pub fn get_game_id(&self) -> usize {
100        self.game_id
101            .unwrap_or_default()
102    }
103
104    pub fn get_mod_id(&self) -> usize {
105        self.mod_id
106    }
107
108    pub fn get_game_versions(&self) -> &[String] {
109        &self.game_versions
110    }
111
112    pub fn get_display_name(&self) -> &str {
113        &self.display_name
114    }
115
116    pub fn get_file_name(&self) -> &Path {
117        &self.file_name
118    }
119
120    pub fn get_download_url(&self) -> &str {
121        self.download_url
122            .as_ref()
123            .map_or("", |s| s)
124    }
125}
126
127#[cfg_attr(feature="serde", derive(Deserialize, Serialize))]
128#[derive(Debug, Clone)]
129pub struct FingerPrintInfo {
130    pub id: usize,
131    pub file: CurseFile,
132}
133
134/// This struct contains the data about the request of a fingerprint
135/// requests are like
136/// ```json
137/// "data": {
138///     exactMatches: [
139///         CurseFile
140///     ]
141/// }
142/// ```
143#[cfg_attr(feature="serde", derive(Deserialize, Serialize), serde(rename_all = "camelCase"))]
144#[derive(Clone, Debug)]
145pub struct CurseFingerPrint {
146    exact_matches: Vec<FingerPrintInfo>,
147}
148
149impl CurseFingerPrint {
150    pub fn get_file(&self) -> &CurseFile {
151        &self.exact_matches[0].file
152    }
153}
154
155/// This struct contains the data about a single version of a mod
156#[cfg_attr(feature="serde", derive(Deserialize, Serialize), serde(rename_all = "camelCase"))]
157#[derive(Clone, Debug)]
158pub struct CurseVersion {
159    pub id: usize,
160    pub game_id: usize,
161    pub name: String,
162    pub slug: String,
163    pub download_count: usize,
164    pub latest_files: Vec<CurseFile>,
165}
166
167/// This struct contains the data about the multiple versions of a mod
168#[cfg_attr(feature="serde", derive(Deserialize, Serialize))]
169#[derive(Clone, Debug)]
170pub struct CurseVersions {
171    pub data: Vec<CurseVersion>,
172}
173
174/// Because the standard response from Curse API is:
175/// "data": {
176///     * fields of other struct *
177/// }
178/// We need this struct.
179#[cfg(feature="serde")]
180#[derive(Serialize, Deserialize, Clone, Debug)]
181pub struct CurseResponse<T: Serialize> {
182    pub data: T,
183}