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
use super::*;

#[derive(Serialize)]
struct Request {
    token: String,
    password: String,
}

#[derive(Deserialize)]
struct Response {
    id: i32,
    email: String,
}

#[derive(Debug)]
pub enum Returns {
    InvalidToken,
    Valid((i32, String)),
}

impl Client {
    // Returns ID of user and email
    pub async fn reset_password_set(
        &self,
        token: String,
        password: String,
    ) -> Result<Returns, Box<dyn std::error::Error>> {
        let res = req()
            .put(self.endpoint("/v1/users/reset", vec![]))
            .json(&Request { token, password })
            .send()
            .await?;

        match res.status() {
            StatusCode::NOT_FOUND => Ok(Returns::InvalidToken),
            StatusCode::BAD_REQUEST => Err("Bad Request".into()),
            StatusCode::OK => {
                let parsed = res.json::<Response>().await?;

                Ok(Returns::Valid((parsed.id, parsed.email)))
            }
            _ => Err("Unknown Response Status Code".into()),
        }
    }
}