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};

/// Game
///
/// Use BotFather to create and edit games,
/// their short names will act as unique identifiers
#[derive(Clone, Debug)]
pub struct Game {
    /// Title of the game
    pub title: String,
    /// Description of the game
    pub description: String,
    /// Photo that will be displayed in the game message in chats
    pub photo: Vec<PhotoSize>,
    /// Brief description of the game or high scores included in the game message
    /// Can be automatically edited to include current high scores for the game
    /// when the bot calls setGameScore, or manually edited using editMessageText
    /// 0-4096 characters
    pub text: Option<Text>,
    /// Animation that will be displayed in the game message in chats
    /// Upload via BotFather
    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>,
}

/// One row of the high scores table for a game
#[derive(Clone, Debug, Deserialize)]
pub struct GameHighScore {
    /// Position in high score table for the game
    pub position: Integer,
    /// User
    pub user: User,
    /// Score
    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);
    }
}