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
use crate::types::{
animation::Animation,
message::{RawMessageEntity, Text},
photo_size::PhotoSize,
primitive::Integer,
user::User,
};
use serde::{de::Error, Deserialize, Deserializer};
#[derive(Clone, Debug)]
pub struct Game {
pub title: String,
pub description: String,
pub photo: Vec<PhotoSize>,
pub text: Option<Text>,
pub animation: Option<Animation>,
}
impl<'de> Deserialize<'de> for Game {
fn deserialize<D>(deserializer: D) -> Result<Game, D::Error>
where
D: Deserializer<'de>,
{
let raw_game: RawGame = Deserialize::deserialize(deserializer)?;
Ok(Game {
title: raw_game.title,
description: raw_game.description,
photo: raw_game.photo,
text: match raw_game.text {
Some(data) => Some(Text::parse(data, raw_game.text_entities).map_err(D::Error::custom)?),
None => None,
},
animation: raw_game.animation,
})
}
}
#[derive(Debug, Deserialize)]
struct RawGame {
title: String,
description: String,
photo: Vec<PhotoSize>,
text: Option<String>,
text_entities: Option<Vec<RawMessageEntity>>,
animation: Option<Animation>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct GameHighScore {
pub position: Integer,
pub user: User,
pub score: Integer,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_game_full() {
let game: Game = serde_json::from_value(serde_json::json!({
"title": "title",
"description": "description",
"photo": [
{
"file_id": "photo file id",
"file_unique_id": "photo unique file id",
"width": 200,
"height": 200
}
],
"text": "text",
"animation": {
"file_id": "animation file id",
"file_unique_id": "animation unique file id",
"width": 200,
"height": 200,
"duration": 24
}
}))
.unwrap();
assert_eq!(game.title, "title");
assert_eq!(game.description, "description");
assert_eq!(game.photo.len(), 1);
assert_eq!(game.photo[0].file_id, "photo file id");
assert_eq!(game.photo[0].file_unique_id, "photo unique file id");
assert_eq!(game.text.unwrap().data, "text");
let animation = game.animation.unwrap();
assert_eq!(animation.file_id, "animation file id");
assert_eq!(animation.file_unique_id, "animation unique file id");
}
#[test]
fn deserialize_game_partial() {
let game: Game = serde_json::from_value(serde_json::json!({
"title": "title",
"description": "description",
"photo": []
}))
.unwrap();
assert_eq!(game.title, "title");
assert_eq!(game.description, "description");
assert_eq!(game.photo.len(), 0);
assert!(game.text.is_none());
assert!(game.animation.is_none());
}
#[test]
fn deserialize_game_high_score() {
let score: GameHighScore = serde_json::from_value(serde_json::json!({
"position": 1,
"user": {
"id": 2,
"first_name": "test",
"is_bot": false
},
"score": 3
}))
.unwrap();
assert_eq!(score.position, 1);
assert_eq!(score.user.id, 2);
assert_eq!(score.score, 3);
}
}