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
//! Game Types
//! 
//! Game types are classifications for unofficial games, such as ROMS.
//! 
//! # Arguments:
//! 
//! When calling for Gametypes first asing a variable to GameTypeData after importing it. 
//! After that add ::new("gametype_name/id") and call .run() to get the data.
//! 
//! # Example:
//! 
//! ```rust
//! use speedrunapi::GameTypeData;
//! let result = GameTypeData::new("Fangame").run();
//! println!("{:?}", result);
//! ```
//! This will fetch the entirenty of the data for the gametype as a json object and print it.

use crate::types::GameTypeData as Data;

#[derive(Debug)]
pub struct GameTypeData{
    pub gametype: String,
}

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

#[derive(Debug)]
pub enum GameTypeResult {
    GameType(Data),
    None,
    Error(GameTypeError),
}

impl GameTypeResult{
    
    /// Returns the name of the gametype
    /// 
    /// ## Returns:
    /// 
    /// The name of the gametype as an &str
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameTypeData;
    /// let result = GameTypeData::new("Fangame").run();
    /// assert_eq!(result.name(), "Fangame");
    /// ```
    
    pub fn name(&self) -> &str{
        if let GameTypeResult::GameType(gametype_data) = self{
            &gametype_data.data.name
        }
        else{
            panic!("Cannot Get name from: {:?}", self);
        }
    }

    /// Returns the id of a gametype
    /// 
    /// ## Returns:
    /// 
    /// The id of the gametype as a &str
    /// 
    /// ## Example:
    /// ```rust
    /// use speedrunapi::GameTypeData;
    /// let result = GameTypeData::new("Fangame").run();
    /// assert_eq!(result.id(), "d91jd1ex")
    /// ```
    
    pub fn id(&self) -> &str{
        if let GameTypeResult::GameType(gametype_data) = self{
            &gametype_data.data.id
        }
        else{
            panic!("Cannot Get id from: {:?}", self);
        }
    }
}

impl GameTypeData{
    
    /// Creates a new GameTypeData object
    /// 
    /// # Arguments:
    /// 
    /// `gametype: &str` - The name or id of the gametype you are seraching for
    /// 
    /// # Examples:
    /// ```rust
    /// use speedrunapi::GameTypeData;
    /// let result = GameTypeData::new("Fangame");
    /// println!("{:?}", result);
    /// ```
    /// This will return the parameters passed to the function
    /// ```rust
    /// use speedrunapi::GameTypeData;
    /// let result = GameTypeData::new("Fangame").run();
    /// println!("{:?}", result);
    /// ```
    /// This will print the JSON data of the gametype

    pub fn new(gametype: &str) -> GameTypeData{
        GameTypeData{
            gametype: gametype.to_string(),
        }
    }

    /// Runs the GameType Object
    /// 
    /// # Arguments:
    /// 
    /// This function requires that you have called the new function with the nessiasary parametrers first
    /// 
    /// # Returns:
    /// 
    /// The result of the request as a GameTypeResult object
    /// 
    /// If an error has occurred the program may return None or Error(ErrorType) as an OK
    /// 
    /// # Example:
    /// ```rust
    /// use speedrunapi::GameTypeData;
    /// let result = GameTypeData::new("Fangame").run();
    /// println!("{:?}", result);
    /// ```
    /// This will return the data from the gametype you are searching for

    #[tokio::main]
    pub async fn run(&self) -> GameTypeResult{
        let client = reqwest::Client::new();
        let url = format!("https://www.speedrun.com/api/v1/gametypes/{:1}", self.gametype);
        let response = match client.get(url).send().await{
            Ok(response) => response,
            Err(err) => return GameTypeResult::Error(GameTypeError::ReqwestError(err)),
        };
        if response.status() == reqwest::StatusCode::NOT_FOUND{
            return GameTypeResult::Error(GameTypeError::GameTypeNotFound);
        }
        let response = match response.json::<Data>().await{
            Ok(response) => response,
            Err(err) => return GameTypeResult::Error(GameTypeError::ReqwestError(err)),
        };
        GameTypeResult::GameType(response)
    }
}