rtdlib/types/
chat_source.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9use std::fmt::Debug;
10use serde::de::{Deserialize, Deserializer};
11
12
13
14/// TRAIT | Describes a reason why an external chat is shown in a chat list
15pub trait TDChatSource: Debug + RObject {}
16
17/// Describes a reason why an external chat is shown in a chat list
18#[derive(Debug, Clone, Serialize)]
19#[serde(untagged)]
20pub enum ChatSource {
21  #[doc(hidden)] _Default(()),
22  /// The chat is sponsored by the user's MTProxy server
23  MtprotoProxy(ChatSourceMtprotoProxy),
24  /// The chat contains a public service announcement
25  PublicServiceAnnouncement(ChatSourcePublicServiceAnnouncement),
26
27}
28
29impl Default for ChatSource {
30  fn default() -> Self { ChatSource::_Default(()) }
31}
32
33impl<'de> Deserialize<'de> for ChatSource {
34  fn deserialize<D>(deserializer: D) -> Result<ChatSource, D::Error> where D: Deserializer<'de> {
35    use serde::de::Error;
36    rtd_enum_deserialize!(
37      ChatSource,
38      (chatSourceMtprotoProxy, MtprotoProxy);
39      (chatSourcePublicServiceAnnouncement, PublicServiceAnnouncement);
40
41    )(deserializer)
42  }
43}
44
45impl RObject for ChatSource {
46  #[doc(hidden)] fn td_name(&self) -> &'static str {
47    match self {
48      ChatSource::MtprotoProxy(t) => t.td_name(),
49      ChatSource::PublicServiceAnnouncement(t) => t.td_name(),
50
51      _ => "-1",
52    }
53  }
54  #[doc(hidden)] fn extra(&self) -> Option<String> {
55    match self {
56      ChatSource::MtprotoProxy(t) => t.extra(),
57      ChatSource::PublicServiceAnnouncement(t) => t.extra(),
58
59      _ => None,
60    }
61  }
62  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
63}
64
65impl ChatSource {
66  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
67  #[doc(hidden)] pub fn _is_default(&self) -> bool { if let ChatSource::_Default(_) = self { true } else { false } }
68
69  pub fn is_mtproto_proxy(&self) -> bool { if let ChatSource::MtprotoProxy(_) = self { true } else { false } }
70  pub fn is_public_service_announcement(&self) -> bool { if let ChatSource::PublicServiceAnnouncement(_) = self { true } else { false } }
71
72  pub fn on_mtproto_proxy<F: FnOnce(&ChatSourceMtprotoProxy)>(&self, fnc: F) -> &Self { if let ChatSource::MtprotoProxy(t) = self { fnc(t) }; self }
73  pub fn on_public_service_announcement<F: FnOnce(&ChatSourcePublicServiceAnnouncement)>(&self, fnc: F) -> &Self { if let ChatSource::PublicServiceAnnouncement(t) = self { fnc(t) }; self }
74
75  pub fn as_mtproto_proxy(&self) -> Option<&ChatSourceMtprotoProxy> { if let ChatSource::MtprotoProxy(t) = self { return Some(t) } None }
76  pub fn as_public_service_announcement(&self) -> Option<&ChatSourcePublicServiceAnnouncement> { if let ChatSource::PublicServiceAnnouncement(t) = self { return Some(t) } None }
77
78
79
80  pub fn mtproto_proxy<T: AsRef<ChatSourceMtprotoProxy>>(t: T) -> Self { ChatSource::MtprotoProxy(t.as_ref().clone()) }
81
82  pub fn public_service_announcement<T: AsRef<ChatSourcePublicServiceAnnouncement>>(t: T) -> Self { ChatSource::PublicServiceAnnouncement(t.as_ref().clone()) }
83
84}
85
86impl AsRef<ChatSource> for ChatSource {
87  fn as_ref(&self) -> &ChatSource { self }
88}
89
90
91
92
93
94
95
96/// The chat is sponsored by the user's MTProxy server
97#[derive(Debug, Clone, Default, Serialize, Deserialize)]
98pub struct ChatSourceMtprotoProxy {
99  #[doc(hidden)]
100  #[serde(rename(serialize = "@type", deserialize = "@type"))]
101  td_name: String,
102  #[doc(hidden)]
103  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
104  extra: Option<String>,
105  
106}
107
108impl RObject for ChatSourceMtprotoProxy {
109  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatSourceMtprotoProxy" }
110  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
111  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
112}
113
114
115impl TDChatSource for ChatSourceMtprotoProxy {}
116
117
118
119impl ChatSourceMtprotoProxy {
120  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
121  pub fn builder() -> RTDChatSourceMtprotoProxyBuilder {
122    let mut inner = ChatSourceMtprotoProxy::default();
123    inner.td_name = "chatSourceMtprotoProxy".to_string();
124    inner.extra = Some(Uuid::new_v4().to_string());
125    RTDChatSourceMtprotoProxyBuilder { inner }
126  }
127
128}
129
130#[doc(hidden)]
131pub struct RTDChatSourceMtprotoProxyBuilder {
132  inner: ChatSourceMtprotoProxy
133}
134
135impl RTDChatSourceMtprotoProxyBuilder {
136  pub fn build(&self) -> ChatSourceMtprotoProxy { self.inner.clone() }
137
138}
139
140impl AsRef<ChatSourceMtprotoProxy> for ChatSourceMtprotoProxy {
141  fn as_ref(&self) -> &ChatSourceMtprotoProxy { self }
142}
143
144impl AsRef<ChatSourceMtprotoProxy> for RTDChatSourceMtprotoProxyBuilder {
145  fn as_ref(&self) -> &ChatSourceMtprotoProxy { &self.inner }
146}
147
148
149
150
151
152
153
154/// The chat contains a public service announcement
155#[derive(Debug, Clone, Default, Serialize, Deserialize)]
156pub struct ChatSourcePublicServiceAnnouncement {
157  #[doc(hidden)]
158  #[serde(rename(serialize = "@type", deserialize = "@type"))]
159  td_name: String,
160  #[doc(hidden)]
161  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
162  extra: Option<String>,
163  /// The type of the announcement
164  #[serde(rename(serialize = "type", deserialize = "type"))] type_: String,
165  /// The text of the announcement
166  text: String,
167  
168}
169
170impl RObject for ChatSourcePublicServiceAnnouncement {
171  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatSourcePublicServiceAnnouncement" }
172  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
173  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
174}
175
176
177impl TDChatSource for ChatSourcePublicServiceAnnouncement {}
178
179
180
181impl ChatSourcePublicServiceAnnouncement {
182  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
183  pub fn builder() -> RTDChatSourcePublicServiceAnnouncementBuilder {
184    let mut inner = ChatSourcePublicServiceAnnouncement::default();
185    inner.td_name = "chatSourcePublicServiceAnnouncement".to_string();
186    inner.extra = Some(Uuid::new_v4().to_string());
187    RTDChatSourcePublicServiceAnnouncementBuilder { inner }
188  }
189
190  pub fn type_(&self) -> &String { &self.type_ }
191
192  pub fn text(&self) -> &String { &self.text }
193
194}
195
196#[doc(hidden)]
197pub struct RTDChatSourcePublicServiceAnnouncementBuilder {
198  inner: ChatSourcePublicServiceAnnouncement
199}
200
201impl RTDChatSourcePublicServiceAnnouncementBuilder {
202  pub fn build(&self) -> ChatSourcePublicServiceAnnouncement { self.inner.clone() }
203
204   
205  pub fn type_<T: AsRef<str>>(&mut self, type_: T) -> &mut Self {
206    self.inner.type_ = type_.as_ref().to_string();
207    self
208  }
209
210   
211  pub fn text<T: AsRef<str>>(&mut self, text: T) -> &mut Self {
212    self.inner.text = text.as_ref().to_string();
213    self
214  }
215
216}
217
218impl AsRef<ChatSourcePublicServiceAnnouncement> for ChatSourcePublicServiceAnnouncement {
219  fn as_ref(&self) -> &ChatSourcePublicServiceAnnouncement { self }
220}
221
222impl AsRef<ChatSourcePublicServiceAnnouncement> for RTDChatSourcePublicServiceAnnouncementBuilder {
223  fn as_ref(&self) -> &ChatSourcePublicServiceAnnouncement { &self.inner }
224}
225
226
227