rust_tdlib/types/
game.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes a game
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Game {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Game ID
14
15    #[serde(
16        deserialize_with = "super::_common::number_from_string",
17        serialize_with = "super::_common::string_to_number"
18    )]
19    #[serde(default)]
20    id: i64,
21    /// Game short name. To share a game use the URL https://t.me/{bot_username}?game={game_short_name}
22
23    #[serde(default)]
24    short_name: String,
25    /// Game title
26
27    #[serde(default)]
28    title: String,
29    /// Game text, usually containing scoreboards for a game
30    text: FormattedText,
31    /// Describes a game
32
33    #[serde(default)]
34    description: String,
35    /// Game photo
36    photo: Photo,
37    /// Game animation; may be null
38    animation: Option<Animation>,
39}
40
41impl RObject for Game {
42    #[doc(hidden)]
43    fn extra(&self) -> Option<&str> {
44        self.extra.as_deref()
45    }
46    #[doc(hidden)]
47    fn client_id(&self) -> Option<i32> {
48        self.client_id
49    }
50}
51
52impl Game {
53    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
54        Ok(serde_json::from_str(json.as_ref())?)
55    }
56    pub fn builder() -> GameBuilder {
57        let mut inner = Game::default();
58        inner.extra = Some(Uuid::new_v4().to_string());
59
60        GameBuilder { inner }
61    }
62
63    pub fn id(&self) -> i64 {
64        self.id
65    }
66
67    pub fn short_name(&self) -> &String {
68        &self.short_name
69    }
70
71    pub fn title(&self) -> &String {
72        &self.title
73    }
74
75    pub fn text(&self) -> &FormattedText {
76        &self.text
77    }
78
79    pub fn description(&self) -> &String {
80        &self.description
81    }
82
83    pub fn photo(&self) -> &Photo {
84        &self.photo
85    }
86
87    pub fn animation(&self) -> &Option<Animation> {
88        &self.animation
89    }
90}
91
92#[doc(hidden)]
93pub struct GameBuilder {
94    inner: Game,
95}
96
97#[deprecated]
98pub type RTDGameBuilder = GameBuilder;
99
100impl GameBuilder {
101    pub fn build(&self) -> Game {
102        self.inner.clone()
103    }
104
105    pub fn id(&mut self, id: i64) -> &mut Self {
106        self.inner.id = id;
107        self
108    }
109
110    pub fn short_name<T: AsRef<str>>(&mut self, short_name: T) -> &mut Self {
111        self.inner.short_name = short_name.as_ref().to_string();
112        self
113    }
114
115    pub fn title<T: AsRef<str>>(&mut self, title: T) -> &mut Self {
116        self.inner.title = title.as_ref().to_string();
117        self
118    }
119
120    pub fn text<T: AsRef<FormattedText>>(&mut self, text: T) -> &mut Self {
121        self.inner.text = text.as_ref().clone();
122        self
123    }
124
125    pub fn description<T: AsRef<str>>(&mut self, description: T) -> &mut Self {
126        self.inner.description = description.as_ref().to_string();
127        self
128    }
129
130    pub fn photo<T: AsRef<Photo>>(&mut self, photo: T) -> &mut Self {
131        self.inner.photo = photo.as_ref().clone();
132        self
133    }
134
135    pub fn animation<T: AsRef<Animation>>(&mut self, animation: T) -> &mut Self {
136        self.inner.animation = Some(animation.as_ref().clone());
137        self
138    }
139}
140
141impl AsRef<Game> for Game {
142    fn as_ref(&self) -> &Game {
143        self
144    }
145}
146
147impl AsRef<Game> for GameBuilder {
148    fn as_ref(&self) -> &Game {
149        &self.inner
150    }
151}