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
use crate::types::LanguageCode;
use serde::{Deserialize, Serialize};
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub is_bot: bool,
pub first_name: String,
pub last_name: Option<String>,
pub username: Option<String>,
pub language_code: Option<LanguageCode>,
}
impl User {
pub fn full_name(&self) -> String {
match &self.last_name {
Some(last_name) => (format!("{0} {1}", self.first_name, last_name)),
None => self.first_name.clone(),
}
}
pub fn mention(&self) -> Option<String> {
Some(format!("@{}", self.username.as_ref()?))
}
pub fn url(&self) -> reqwest::Url {
reqwest::Url::parse(format!("tg://user/?id={}", self.id).as_str())
.unwrap()
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Me {
#[serde(flatten)]
pub user: User,
pub can_join_groups: bool,
pub can_read_all_group_messages: bool,
pub supports_inline_queries: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let json = r#"{
"id":12345,
"is_bot":false,
"first_name":"firstName",
"last_name":"lastName",
"username":"Username",
"language_code":"ru"
}"#;
let expected = User {
id: 12345,
is_bot: false,
first_name: "firstName".to_string(),
last_name: Some("lastName".to_string()),
username: Some("Username".to_string()),
language_code: Some(LanguageCode::RU),
};
let actual = serde_json::from_str::<User>(&json).unwrap();
assert_eq!(actual, expected)
}
}