rust_tdlib/types/
chat_source.rs

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