rust_tdlib/types/
proxy_type.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5use std::fmt::Debug;
6
7/// Describes the type of a proxy server
8pub trait TDProxyType: Debug + RObject {}
9
10/// Describes the type of a proxy server
11#[derive(Debug, Clone, Deserialize, Serialize, Default)]
12#[serde(tag = "@type")]
13pub enum ProxyType {
14    #[doc(hidden)]
15    #[default]
16    _Default,
17    /// A HTTP transparent proxy server
18    #[serde(rename = "proxyTypeHttp")]
19    Http(ProxyTypeHttp),
20    /// An MTProto proxy server
21    #[serde(rename = "proxyTypeMtproto")]
22    Mtproto(ProxyTypeMtproto),
23    /// A SOCKS5 proxy server
24    #[serde(rename = "proxyTypeSocks5")]
25    Socks5(ProxyTypeSocks5),
26}
27
28impl RObject for ProxyType {
29    #[doc(hidden)]
30    fn extra(&self) -> Option<&str> {
31        match self {
32            ProxyType::Http(t) => t.extra(),
33            ProxyType::Mtproto(t) => t.extra(),
34            ProxyType::Socks5(t) => t.extra(),
35
36            _ => None,
37        }
38    }
39    #[doc(hidden)]
40    fn client_id(&self) -> Option<i32> {
41        match self {
42            ProxyType::Http(t) => t.client_id(),
43            ProxyType::Mtproto(t) => t.client_id(),
44            ProxyType::Socks5(t) => t.client_id(),
45
46            _ => None,
47        }
48    }
49}
50
51impl ProxyType {
52    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
53        Ok(serde_json::from_str(json.as_ref())?)
54    }
55    #[doc(hidden)]
56    pub fn _is_default(&self) -> bool {
57        matches!(self, ProxyType::_Default)
58    }
59}
60
61impl AsRef<ProxyType> for ProxyType {
62    fn as_ref(&self) -> &ProxyType {
63        self
64    }
65}
66
67/// A HTTP transparent proxy server
68#[derive(Debug, Clone, Default, Serialize, Deserialize)]
69pub struct ProxyTypeHttp {
70    #[doc(hidden)]
71    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
72    extra: Option<String>,
73    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
74    client_id: Option<i32>,
75    /// Username for logging in; may be empty
76
77    #[serde(default)]
78    username: String,
79    /// Password for logging in; may be empty
80
81    #[serde(default)]
82    password: String,
83    /// Pass true if the proxy supports only HTTP requests and doesn't support transparent TCP connections via HTTP CONNECT method
84
85    #[serde(default)]
86    http_only: bool,
87}
88
89impl RObject for ProxyTypeHttp {
90    #[doc(hidden)]
91    fn extra(&self) -> Option<&str> {
92        self.extra.as_deref()
93    }
94    #[doc(hidden)]
95    fn client_id(&self) -> Option<i32> {
96        self.client_id
97    }
98}
99
100impl TDProxyType for ProxyTypeHttp {}
101
102impl ProxyTypeHttp {
103    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
104        Ok(serde_json::from_str(json.as_ref())?)
105    }
106    pub fn builder() -> ProxyTypeHttpBuilder {
107        let mut inner = ProxyTypeHttp::default();
108        inner.extra = Some(Uuid::new_v4().to_string());
109
110        ProxyTypeHttpBuilder { inner }
111    }
112
113    pub fn username(&self) -> &String {
114        &self.username
115    }
116
117    pub fn password(&self) -> &String {
118        &self.password
119    }
120
121    pub fn http_only(&self) -> bool {
122        self.http_only
123    }
124}
125
126#[doc(hidden)]
127pub struct ProxyTypeHttpBuilder {
128    inner: ProxyTypeHttp,
129}
130
131#[deprecated]
132pub type RTDProxyTypeHttpBuilder = ProxyTypeHttpBuilder;
133
134impl ProxyTypeHttpBuilder {
135    pub fn build(&self) -> ProxyTypeHttp {
136        self.inner.clone()
137    }
138
139    pub fn username<T: AsRef<str>>(&mut self, username: T) -> &mut Self {
140        self.inner.username = username.as_ref().to_string();
141        self
142    }
143
144    pub fn password<T: AsRef<str>>(&mut self, password: T) -> &mut Self {
145        self.inner.password = password.as_ref().to_string();
146        self
147    }
148
149    pub fn http_only(&mut self, http_only: bool) -> &mut Self {
150        self.inner.http_only = http_only;
151        self
152    }
153}
154
155impl AsRef<ProxyTypeHttp> for ProxyTypeHttp {
156    fn as_ref(&self) -> &ProxyTypeHttp {
157        self
158    }
159}
160
161impl AsRef<ProxyTypeHttp> for ProxyTypeHttpBuilder {
162    fn as_ref(&self) -> &ProxyTypeHttp {
163        &self.inner
164    }
165}
166
167/// An MTProto proxy server
168#[derive(Debug, Clone, Default, Serialize, Deserialize)]
169pub struct ProxyTypeMtproto {
170    #[doc(hidden)]
171    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
172    extra: Option<String>,
173    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
174    client_id: Option<i32>,
175    /// The proxy's secret in hexadecimal encoding
176
177    #[serde(default)]
178    secret: String,
179}
180
181impl RObject for ProxyTypeMtproto {
182    #[doc(hidden)]
183    fn extra(&self) -> Option<&str> {
184        self.extra.as_deref()
185    }
186    #[doc(hidden)]
187    fn client_id(&self) -> Option<i32> {
188        self.client_id
189    }
190}
191
192impl TDProxyType for ProxyTypeMtproto {}
193
194impl ProxyTypeMtproto {
195    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
196        Ok(serde_json::from_str(json.as_ref())?)
197    }
198    pub fn builder() -> ProxyTypeMtprotoBuilder {
199        let mut inner = ProxyTypeMtproto::default();
200        inner.extra = Some(Uuid::new_v4().to_string());
201
202        ProxyTypeMtprotoBuilder { inner }
203    }
204
205    pub fn secret(&self) -> &String {
206        &self.secret
207    }
208}
209
210#[doc(hidden)]
211pub struct ProxyTypeMtprotoBuilder {
212    inner: ProxyTypeMtproto,
213}
214
215#[deprecated]
216pub type RTDProxyTypeMtprotoBuilder = ProxyTypeMtprotoBuilder;
217
218impl ProxyTypeMtprotoBuilder {
219    pub fn build(&self) -> ProxyTypeMtproto {
220        self.inner.clone()
221    }
222
223    pub fn secret<T: AsRef<str>>(&mut self, secret: T) -> &mut Self {
224        self.inner.secret = secret.as_ref().to_string();
225        self
226    }
227}
228
229impl AsRef<ProxyTypeMtproto> for ProxyTypeMtproto {
230    fn as_ref(&self) -> &ProxyTypeMtproto {
231        self
232    }
233}
234
235impl AsRef<ProxyTypeMtproto> for ProxyTypeMtprotoBuilder {
236    fn as_ref(&self) -> &ProxyTypeMtproto {
237        &self.inner
238    }
239}
240
241/// A SOCKS5 proxy server
242#[derive(Debug, Clone, Default, Serialize, Deserialize)]
243pub struct ProxyTypeSocks5 {
244    #[doc(hidden)]
245    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
246    extra: Option<String>,
247    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
248    client_id: Option<i32>,
249    /// Username for logging in; may be empty
250
251    #[serde(default)]
252    username: String,
253    /// Password for logging in; may be empty
254
255    #[serde(default)]
256    password: String,
257}
258
259impl RObject for ProxyTypeSocks5 {
260    #[doc(hidden)]
261    fn extra(&self) -> Option<&str> {
262        self.extra.as_deref()
263    }
264    #[doc(hidden)]
265    fn client_id(&self) -> Option<i32> {
266        self.client_id
267    }
268}
269
270impl TDProxyType for ProxyTypeSocks5 {}
271
272impl ProxyTypeSocks5 {
273    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
274        Ok(serde_json::from_str(json.as_ref())?)
275    }
276    pub fn builder() -> ProxyTypeSocks5Builder {
277        let mut inner = ProxyTypeSocks5::default();
278        inner.extra = Some(Uuid::new_v4().to_string());
279
280        ProxyTypeSocks5Builder { inner }
281    }
282
283    pub fn username(&self) -> &String {
284        &self.username
285    }
286
287    pub fn password(&self) -> &String {
288        &self.password
289    }
290}
291
292#[doc(hidden)]
293pub struct ProxyTypeSocks5Builder {
294    inner: ProxyTypeSocks5,
295}
296
297#[deprecated]
298pub type RTDProxyTypeSocks5Builder = ProxyTypeSocks5Builder;
299
300impl ProxyTypeSocks5Builder {
301    pub fn build(&self) -> ProxyTypeSocks5 {
302        self.inner.clone()
303    }
304
305    pub fn username<T: AsRef<str>>(&mut self, username: T) -> &mut Self {
306        self.inner.username = username.as_ref().to_string();
307        self
308    }
309
310    pub fn password<T: AsRef<str>>(&mut self, password: T) -> &mut Self {
311        self.inner.password = password.as_ref().to_string();
312        self
313    }
314}
315
316impl AsRef<ProxyTypeSocks5> for ProxyTypeSocks5 {
317    fn as_ref(&self) -> &ProxyTypeSocks5 {
318        self
319    }
320}
321
322impl AsRef<ProxyTypeSocks5> for ProxyTypeSocks5Builder {
323    fn as_ref(&self) -> &ProxyTypeSocks5 {
324        &self.inner
325    }
326}