speedrunapi 0.3.0

REST API wrapper for speedrun.com's API in rust!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Games
//! 
//! Handles requests for a games data
//! 
//! What is a game?
//! A game is where users can submit runs for different categories and leaderboards connected to the game.
//! 

use crate::types::GameData as Data;
use crate::tl_time;

#[derive(Debug)]
pub struct GameData{
    pub name: String,
    categories: bool,
    levels: bool,
    variables: bool,
    derived_games: bool,
    records: bool,
}

#[derive(Debug)]
pub enum GameResult {
    Game(Data),
    None,
    Error(GameError),
}

#[derive(Debug)]
pub enum GameError {
    GameNotFound,
    InvalidArguments,
    ReqwestError(reqwest::Error),
}

/// Generates a function that gets a datype from the result.
macro_rules! Generate_Function {
    ($doc:expr, $name:ident, $($field:tt).+, $datatype:ty $(, $args:ident($($extra_args:tt)*))?) => {
        #[doc = $doc]
        pub fn $name(&self) -> $datatype {
            if let GameResult::Game(game_data) = self {
                let value = game_data.data.$($field).+.clone();
                $(let value = value.clone().$args($($extra_args)*);)?
                value
            } else {
                panic!("Cannot get data from: {:?}", self);
            }
        }
    };
}

impl GameResult{

   // Generate_Function!("Test", abbreviations, names.international, String);
   // Generate_Function!("## test2 ", data, platforms, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the name of the game \n ## Returns:  \n The name of the game in english as a string \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.name(), \"Minecraft: Java Edition\")", name, names.international, String);

    Generate_Function!("Returns the japanese name of the game \n ## Returns: \n The name of the game in japanese as a string \n #### Notes:
    \n The japanese name may be \"None\" \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.japanese_name(), \"None\")", japanese_name, names.japanese, String, unwrap_or(String::from("None")));

    Generate_Function!("Returns the twitch name of the game \n ## Returns: \n The twitch name of the game as a String \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.twitch_name(), \"Minecraft\")", twitch_name, names.twitch, String);

    Generate_Function!("Returns the ID of the game \n ## Returns: \n The ID of the game as a String \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.id(), \"j1npme6p\")", id, id, String);

    Generate_Function!("Returns the number of boosts a game has received \n ## Returns: \n The number of boosts a game has recived as an i32 \n ## Example:
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.boosts(), 0);", boosts, boosts_received, i32, unwrap_or(i32::from(0)));

    Generate_Function!("Returns the number of unique players who have boosted a game \n ## Returns: \n The number of uniqe players who have boosted a game as an i32 \n ## Example:
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.boosters(), 0);", boosters, boost_distinct_donors, i32, unwrap_or(i32::from(0)));

    Generate_Function!("Returns the abbreviation of a game \n ## Returns: \n The abbreviation of a game as a string \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.abbreviation(), \"mc\")", abbreviation, abbreviation, String);

    Generate_Function!("Returns the weblink of a game \n ## Returns: \n The weblink of a game as a string \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.weblink(), \"https://www.speedrun.com/mc\")", weblink, weblink, String);

    Generate_Function!("Returns the game discord \n ## Returns: \n The discord of a game as a string \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.discord(), \"https://discord.gg/jmdFn3C\")", discord , discord, String);

    Generate_Function!("Returns the date the game was released \n ## Returns: \n The year the game was released as a i16 \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.released(), 2011)", released, released, i16);

    Generate_Function!("Returns the date the game was released \n ## Returns: \n the date the game was released as a string \n Note:
    This function is similar to the released function it is just newer and more specific, so some games have yet to migrate or set it so it can be \"None\" \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.release_date(), \"None\")", release_date, release_date, String, unwrap_or(String::from("None")));

    /*
    // Figure out how to be able to return multiple values
    pub fn ruleset(&self, rule: String) {
        if let GameResult::Game(game_data) = self {
            match rule.to_lowercase().as_str(){

            }
        }
        else{
            panic!("Cannot get ruleset from: {:?}", self);
        }
    }
    */

    Generate_Function!("Returns if the game has romhacks \n ## Returns: \n If the game has romhacks as a bool 
    \n Note: Use gametypes for more information \n ## Example: \n
    use speedrunapi::GameData;
    let result = GameData::new(\"Mc\").run();
    assert_eq!(result.romhack(), false)", romhack, romhack, bool);

    Generate_Function!("Returns the gametypes for a game \n ## Returns: \n The game types of a game in a vec of strings 
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().gametypes();
    assert_eq!(result, Vec::<String>::new())", gametypes, gametypes, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the platforms of a game \n ## Returns: \n the platforms of a game in a vec of strings
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String>  = GameData::new(\"Mc\").run().platforms();
    assert_eq!(result, [\"8gej2n93\"])", platforms, platforms, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the regions of a game \n ## Returns: \n the regions of a game as a Vec<String>
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().regions();
    assert_eq!(result, Vec::<String>::new())", regions, regions, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the genres of a game \n ## Returns: \n the genres of a game as a Vec<String>
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().genres();
    assert_eq!(result, [\"q4n60ln9\", \"jp230326\"])", genres, genres, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the engines of a game \n ## Returns: \n the engines of a game as a Vec<String>
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().engines();
    assert_eq!(result, Vec::<String>::new())", engines, engines, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the developers of a game \n ## Returns: \n the developers of a game as a Vec<String>
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().developers();
    assert_eq!(result, [\"k62d97ex\"])", developers, developers, Vec<String>, unwrap_or(vec!["None".to_string()]));

    Generate_Function!("Returns the publishers of a game \n ## Returns: \n the publishers of a game as a Vec<String>
    \n If there is none listed defults to \\[\"None\"\\] or \\[\\] \n ## Example: \n
    use speedrunapi::GameData;
    let result: Vec<String> = GameData::new(\"Mc\").run().publishers();
    assert_eq!(result, Vec::<String>::new())", publishers, publishers, Vec<String>, unwrap_or(vec!["None".to_string()]));

    /// Returns the moderators of a game
    /// 
    /// ## Returns:
    /// 
    /// The moderators and their positions of a game as a Vec<(String, String)>
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result = GameData::new("Mc").run().moderators();
    /// println!("{:?}", result);
    /// ```
    
    pub fn moderators(&self) -> Vec<(String, String)> {
        //for translations
        //use crate::user_data::UserData;
        if let GameResult::Game(game_data) = self {
            let mods = game_data.data.moderators.clone();
            let ids: Vec<String> = mods.iter().map(|x| x.0.to_string()).collect();
            let positions: Vec<String> = mods.iter().map(|x| x.1.to_string()).collect();
            for pos in 1..positions.len(){
                positions[pos].trim_matches(|c| c == '\\' || c == '"').to_string();
            }
            let merged_vec: Vec<(String, String)> = ids.iter().zip(positions.iter()).map(|(x, y)| (x.to_string(), y.to_string())).collect();
            return merged_vec.to_vec();
        }
        else{
            panic!("Cannot get moderators from: {:?}", self);
        }
    }
    
    /// Returns when the game was created on speedrun.com
    /// 
    /// ## Returns:
    /// 
    /// The date the game was created as a string
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result: String = GameData::new("Mc").run().created();
    /// assert_eq!(result, "2015-01-29 23:41:21")
    /// ```
    
    pub fn created(&self) -> String{
        if let GameResult::Game(game_data) = self{
            tl_time!(game_data.data.created)
        }
        else {
            panic!("Cannot get creation date from: {:?}", self)
        }
    }
    
    /// Returns the games assets
    /// 
    /// ## Arguments:
    /// 
    /// `asset_type: &str`: The type of assets to return (NON CASE SENSITIVE)
    /// asset_type can be: 
    /// - logo
    /// - cover_tiny
    /// - cover_small
    /// - cover_medium
    /// - cover_large
    /// - icon
    /// - trophy_1st
    /// - trophy_2nd
    /// - trophy_3rd
    /// - trophy_4th
    /// - background
    /// - foreground
    /// 
    /// ## Returns:
    /// 
    /// The link to the asset as a string
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result: String = GameData::new("Mc").run().assets("logo");
    /// assert_eq!(result, "https://www.speedrun.com/themeasset/2wo6q4we/logo?v=413b0b3")
    /// ```
    /// 
    /// ## Panics!
    /// 
    /// The function will panic if it recives an invalid asset type or no asset type at all!
    
    pub fn assets(&self, asset_type: &str) -> String{
        if let GameResult::Game(game_data) = self {
            match asset_type.to_lowercase().as_str(){
                "logo" => game_data.data.assets.logo.uri.clone().unwrap_or(String::from("None")),
                "cover_tiny" => game_data.data.assets.cover_tiny.uri.clone().unwrap_or(String::from("None")),
                "cover_small" => game_data.data.assets.cover_small.uri.clone().unwrap_or(String::from("None")),
                "cover_medium" => game_data.data.assets.cover_medium.uri.clone().unwrap_or(String::from("None")),
                "cover_large" => game_data.data.assets.cover_large.uri.clone().unwrap_or(String::from("None")),
                "icon" => game_data.data.assets.icon.uri.clone().unwrap_or(String::from("None")),
                "trophy_1st" => game_data.data.assets.trophy_1st.uri.clone().unwrap_or(String::from("None")),
                "trophy_2nd" => game_data.data.assets.trophy_2nd.uri.clone().unwrap_or(String::from("None")),
                "trophy_3rd" => game_data.data.assets.trophy_3rd.uri.clone().unwrap_or(String::from("None")),
                "trophy_4th" => game_data.data.assets.trophy_4th.iter().map(|link| link.uri.clone().unwrap_or(String::from("None"))).collect(),
                "background" => game_data.data.assets.background.uri.clone().unwrap_or(String::from("None")),
                "foreground" => game_data.data.assets.foreground.iter().map(|link| link.uri.clone().unwrap_or(String::from("None"))).collect(),
                _ => panic!("Invalid asset type: {:?}", asset_type)
            }
       }
        else{
            panic!("Cannot get assets from: {:?}", self);
        }
    }

    /// Returns the links of a game
    /// 
    /// ## Returns:
    /// 
    /// The links of a game as a Vec<(String, String)>
    /// The format is (name, link)
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result: Vec<(String, String)> = GameData::new("Mc").run().links();
    /// println!("{:?}", result);
    /// ```

    pub fn links(&self) -> Vec<(String, String)> {
        if let GameResult::Game(game_data) = self {
            let game = &game_data.data.links;
            let names: Vec<&str> = game.iter().map(|link| link.rel.as_str()).collect();
            let urls: Vec<&str> = game.iter().map(|link| link.uri.as_str()).collect();
            let merged_vec: Vec<(String, String)> = names.iter().zip(urls.iter()).map(|(x, y)| (x.to_string(), y.to_string())).collect();
            return merged_vec.to_vec();
        }
        else{
            panic!("Cannot get links from: {:?}", self);
        }
    }

}

impl GameData{
    
    /// Creates a new GameData object
    /// 
    /// # Arguments:
    /// 
    /// `game: &str`: The name of the game
    /// 
    /// # Examples:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result = GameData::new("Mc");
    /// ```
    /// This will create a new GameData object with the name "Mc" but when printed will only print out the parameters passed to it.
    /// To show the date recieved you need to add .run()
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result = GameData::new("Mc").run();
    /// println!("{:?}", result);
    /// ```

    pub fn new(game: &str) -> GameData{
        GameData{
            name: game.to_string(),
            categories: false,
            levels: false,
            variables: false,
            derived_games: false,
            records: false,
        }
    }

    /// Runs the request to the speedrun.com API
    /// 
    /// # Arguments:
    /// 
    /// This function requires that you have called the new function with the nessiasary parametrers first
    /// 
    /// # Returns:
    /// 
    /// The result of the request as a GameData object
    /// 
    /// If an error has occurred the program may return None or Error(ErrorType) as an OK
    /// 
    /// # Example:
    /// ```rust
    /// use speedrunapi::GameData;
    /// let result = GameData::new("Mc").run();
    /// println!("{:?}", result);
    /// ```
    
    #[tokio::main]
    pub async fn run(&self) -> GameResult{
        let client = reqwest::Client::new();
        let mut url = format!("https://www.speedrun.com/api/v1/games/{:1}", self.name);
        if self.categories{
            url.push_str("/categories");
        }
        if self.levels{
            url.push_str("/levels");
        }
        if self.variables{
            url.push_str("/variables");
        }
        if self.derived_games{
            url.push_str("/derived-games");
        }
        if self.records{
            url.push_str("/records");
        }
        else{
            url.to_string();
        }
        let response = match client.get(url).send().await{
            Ok(response) => response,
            Err(err) => return GameResult::Error(GameError::ReqwestError(err.into())),
        };
        if response.status() == reqwest::StatusCode::NOT_FOUND{
            return GameResult::Error(GameError::GameNotFound);
        }
        if self.categories{
            // for now print later on deseralize
            println!("test");
            GameResult::None
        }
        else{
            let response = match response.json::<Data>().await{
                Ok(response) => response,
                Err(err) => return GameResult::Error(GameError::ReqwestError(err.into())),
            };
            GameResult::Game(response)
        }
    }
}