rust_tdlib/client/
tdlib_client.rs

1use crate::errors::Result;
2use crate::tdjson;
3use crate::types::RFunction;
4
5/// A bridge between TDLib and rust-tdlib.
6pub trait TdLibClient {
7    fn send<Fnc: RFunction>(&self, client_id: tdjson::ClientId, fnc: Fnc) -> Result<()>;
8    fn receive(&self, timeout: f64) -> Option<String>;
9    fn execute<Fnc: RFunction>(&self, fnc: Fnc) -> Result<Option<String>>;
10    fn new_client(&self) -> tdjson::ClientId;
11}
12
13#[derive(Clone, Debug, Copy)]
14/// Base implementation. See [tdjson](crate::tdjson) for details.
15pub struct TdJson;
16
17impl Default for TdJson {
18    fn default() -> Self {
19        Self
20    }
21}
22
23impl TdLibClient for TdJson {
24    fn send<Fnc: RFunction>(&self, client_id: tdjson::ClientId, fnc: Fnc) -> Result<()> {
25        let json = fnc.to_json()?;
26        tdjson::send(client_id, &json[..]);
27        Ok(())
28    }
29
30    fn receive(&self, timeout: f64) -> Option<String> {
31        tdjson::receive(timeout)
32    }
33
34    fn execute<Fnc: RFunction>(&self, fnc: Fnc) -> Result<Option<String>> {
35        let json = fnc.to_json()?;
36        Ok(tdjson::execute(&json[..]))
37    }
38
39    fn new_client(&self) -> tdjson::ClientId {
40        tdjson::new_client()
41    }
42}
43
44impl TdJson {
45    pub fn new() -> Self {
46        Self
47    }
48}