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
use serde::{Deserialize, Serialize};
use super::TelegraphType;
/// Available fields of the account struct
#[derive(Serialize)]
pub enum AccountField {
/// short_name
#[serde(rename = "short_name")]
ShortName,
/// author_name
#[serde(rename = "author_name")]
AuthorName,
/// author_url
#[serde(rename = "author_url")]
AuthorUrl,
/// auth_url
#[serde(rename = "auth_url")]
AuthUrl,
/// page_count
#[serde(rename = "page_count")]
PageCount
}
/// Object represents a Telegraph account
#[derive(Deserialize, Default, Debug)]
pub struct Account {
/// Account name, helps users with several accounts remember
/// which they are currently using.
/// Displayed to the user above the "Edit/Publish" button
/// on Telegra.ph, other users don't see this name.
pub short_name: Option<String>,
/// Default author name used when creating new articles.
pub author_name: Option<String>,
/// Profile link, opened when users click on the author's name
/// below the title. Can be any link, not necessarily
/// to a Telegram profile or channel.
pub author_url: Option<String>,
/// Only returned by the `createAccount` and `revokeAccessToken` method.
/// Access token of the Telegraph account.
pub access_token: Option<String>,
/// URL to authorize a browser on telegra.ph and connect
/// it to a Telegraph account. This URL is valid for
/// only one use and for 5 minutes only.
pub auth_url: Option<String>,
/// Number of pages belonging to the Telegraph account.
pub page_count: Option<i32>
}
impl TelegraphType for Account {}
#[cfg(test)]
mod tests {
use serde_json;
use super::{Account, AccountField};
#[test]
fn account_deserialize() {
let acc_str = r#"
{
"short_name": "Short name",
"author_name": "Author name",
"author_url": "",
"access_token": "qwerty1234",
"page_count": 10
}"#;
let account: Account = serde_json::from_str(acc_str).unwrap_or_default();
assert_eq!(account.short_name.unwrap_or_default(), "Short name");
assert_eq!(account.author_name.unwrap_or_default(), "Author name");
assert_eq!(account.author_url.unwrap_or_default(), "");
assert_eq!(account.access_token.unwrap_or_default(), "qwerty1234");
assert_eq!(account.auth_url, None);
assert_eq!(account.page_count.unwrap_or_default(), 10);
}
#[test]
fn serialize_account_fields() {
let query = "[\"short_name\"]";
let fields = vec![AccountField::ShortName];
let json = serde_json::to_string(&fields);
assert_eq!(query, json.unwrap_or("".to_string()));
}
}