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
use crate::errors::FieldFormatError;
use regex::Regex;
use serde::{Deserialize, Serialize};

/**
A struct that represents a well-formed email address.
 */
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthEmail {
    pub email: String,
}

impl AuthEmail {
    /**
    Returns a new [`Result<AuthEmail, FieldFormatError>`].
    ## Arguments
    The email address you want to validate.
    ## Errors
    You will receive a [`FieldFormatError`], if:
    - The email address is not in a valid format.

     */
    pub fn new(email: String) -> Result<AuthEmail, FieldFormatError> {
        let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
        if !regex.is_match(email.as_str()) {
            return Err(FieldFormatError::EmailError);
        }
        Ok(AuthEmail { email })
    }
}

/**
A struct that represents a well-formed username.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
 */
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthUsername {
    pub username: String,
}

impl AuthUsername {
    /**
    Returns a new [`Result<AuthUsername, FieldFormatError>`].
    ## Arguments
    The username you want to validate.
    ## Errors
    You will receive a [`FieldFormatError`], if:
    - The username is not between 2 and 32 characters.
     */
    pub fn new(username: String) -> Result<AuthUsername, FieldFormatError> {
        if username.len() < 2 || username.len() > 32 {
            Err(FieldFormatError::UsernameError)
        } else {
            Ok(AuthUsername { username })
        }
    }
}

/**
A struct that represents a well-formed password.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The password is not between 1 and 72 characters.
 */
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthPassword {
    pub password: String,
}

impl AuthPassword {
    /**
    Returns a new [`Result<AuthPassword, FieldFormatError>`].
    ## Arguments
    The password you want to validate.
    ## Errors
    You will receive a [`FieldFormatError`], if:
    - The password is not between 1 and 72 characters.
     */
    pub fn new(password: String) -> Result<AuthPassword, FieldFormatError> {
        if password.is_empty() || password.len() > 72 {
            Err(FieldFormatError::PasswordError)
        } else {
            Ok(AuthPassword { password })
        }
    }
}

/**
A struct that represents a well-formed register request.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
- The password is not between 1 and 72 characters.
 */

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct RegisterSchema {
    username: String,
    password: Option<String>,
    consent: bool,
    email: Option<String>,
    fingerprint: Option<String>,
    invite: Option<String>,
    date_of_birth: Option<String>,
    gift_code_sku_id: Option<String>,
    captcha_key: Option<String>,
    promotional_email_opt_in: Option<bool>,
}

impl RegisterSchema {
    /**
    Returns a new [`Result<RegisterSchema, FieldFormatError>`].
    ## Arguments
    All but "String::username" and "bool::consent" are optional.

    ## Errors
    You will receive a [`FieldFormatError`], if:
    - The username is less than 2 or more than 32 characters in length
    - You supply a `password` which is less than 1 or more than 72 characters in length.

    These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/)
     */
    pub fn new(
        username: String,
        password: Option<String>,
        consent: bool,
        email: Option<String>,
        fingerprint: Option<String>,
        invite: Option<String>,
        date_of_birth: Option<String>,
        gift_code_sku_id: Option<String>,
        captcha_key: Option<String>,
        promotional_email_opt_in: Option<bool>,
    ) -> Result<RegisterSchema, FieldFormatError> {
        let username = AuthUsername::new(username)?.username;

        let email = if let Some(email) = email {
            Some(AuthEmail::new(email)?.email)
        } else {
            None
        };

        let password = if let Some(password) = password {
            Some(AuthPassword::new(password)?.password)
        } else {
            None
        };

        if !consent {
            return Err(FieldFormatError::ConsentError);
        }

        Ok(RegisterSchema {
            username,
            password,
            consent,
            email,
            fingerprint,
            invite,
            date_of_birth,
            gift_code_sku_id,
            captcha_key,
            promotional_email_opt_in,
        })
    }
}

/**
A struct that represents a well-formed login request.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
- The password is not between 1 and 72 characters.
 */
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct LoginSchema {
    pub login: String,
    pub password: String,
    pub undelete: Option<bool>,
    pub captcha_key: Option<String>,
    pub login_source: Option<String>,
    pub gift_code_sku_id: Option<String>,
}

impl LoginSchema {
    /**
    Returns a new [`Result<LoginSchema, FieldFormatError>`].
    ## Arguments
    login: The username you want to login with.
    password: The password you want to login with.
    undelete: Honestly no idea what this is for.
    captcha_key: The captcha key you want to login with.
    login_source: The login source.
    gift_code_sku_id: The gift code sku id.
    ## Errors
    You will receive a [`FieldFormatError`], if:
    - The username is less than 2 or more than 32 characters in length
     */
    pub fn new(
        login: String,
        password: String,
        undelete: Option<bool>,
        captcha_key: Option<String>,
        login_source: Option<String>,
        gift_code_sku_id: Option<String>,
    ) -> Result<LoginSchema, FieldFormatError> {
        let login = AuthUsername::new(login)?.username;
        let password = AuthPassword::new(password)?.password;

        Ok(LoginSchema {
            login,
            password,
            undelete,
            captcha_key,
            login_source,
            gift_code_sku_id,
        })
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct TotpSchema {
    code: String,
    ticket: String,
    gift_code_sku_id: Option<String>,
    login_source: Option<String>,
}