1use crate::error::Error;
2use serde::Deserialize;
3
4#[derive(Debug, Deserialize, Clone)]
5#[serde(rename_all = "camelCase")]
6pub struct Credentials {
7 pub account_id: String,
8 pub password: String,
9 pub host: String,
10 pub type_: String,
11 pub safe: bool,
12}
13
14impl Default for Credentials {
15 fn default() -> Self {
16 Self {
17 account_id: String::new(),
18 password: String::new(),
19 host: String::from("ws.xtb.com"),
20 type_: String::from("real"),
21 safe: false,
22 }
23 }
24}
25
26impl Credentials {
27 pub fn from(json: &str) -> Result<Credentials, Error> {
28 match serde_json::from_str(json) {
29 Ok(credentials) => Ok(credentials),
30 Err(err) => Err(Error::JsonParseError(err)),
31 }
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn test_credentials_default() {
41 let creds = Credentials::default();
42 assert_eq!(creds.host, "ws.xtb.com");
43 assert_eq!(creds.type_, "real");
44 assert_eq!(creds.safe, false);
45 }
46
47 #[test]
48 fn test_credentials_loads() {
49 const DATA: &str = r#"{
50 "accountId": "John Doe",
51 "password": "123456",
52 "host": "example.com",
53 "type": "demo",
54 "safe": true
55 }"#;
56
57 let creds = Credentials::from(DATA);
58 assert!(creds.is_ok());
59
60 let creds = creds.unwrap();
61 assert!(creds.account_id == "John Doe");
62 assert!(creds.password == "123456");
63 assert!(creds.host == "example.com");
64 assert!(creds.type_ == "demo");
65 assert!(creds.safe);
66 }
67}