├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | .idea/ 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "tdjson" 3 | version = "0.2.2" 4 | authors = ["Mike Lubinets "] 5 | description = "TDLIB Json Client for Rust" 6 | documentation = "https://docs.rs/crate/tdjson" 7 | repository = "https://github.com/mersinvald/tdjson-rs" 8 | license = "MIT" 9 | keywords = ["telegram", "tdlib", "bindings"] 10 | 11 | [dependencies] 12 | tdjson-sys = { version = "0.1.5" } 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Mike Lubinets 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## TDLIB Json Client for Rust 2 | 3 | ### Preparing bindgen 4 | 5 | Please follow the steps described in the [tdlib-sys](https://github.com/mersinvald/tdjson-sys) repo to setup FFI bindings generation. 6 | 7 | ### Usage 8 | 9 | Add `tdjson` to your `Cargo.toml` dependency list 10 | ```toml 11 | tdjson = "0.2" 12 | ``` 13 | 14 | And let the Cargo do it's magic! 15 | ```bash 16 | cargo build 17 | ``` -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate tdjson_sys; 2 | 3 | use tdjson_sys::*; 4 | 5 | use std::os::raw::{ 6 | c_void, 7 | c_char, 8 | }; 9 | 10 | use std::ffi::{ 11 | CString, 12 | CStr, 13 | }; 14 | 15 | use std::time::Duration; 16 | use std::ops::Drop; 17 | 18 | use std::sync::Arc; 19 | 20 | pub fn set_log_file(path: &str) -> Result { 21 | let cpath = CString::new(path)?; 22 | unsafe { 23 | Ok(td_set_log_file_path(cpath.as_ptr())) 24 | } 25 | } 26 | 27 | pub fn set_log_verbosity_level(level : i32) { 28 | unsafe { 29 | td_set_log_verbosity_level(level); 30 | } 31 | } 32 | 33 | struct UnsafeClient { 34 | client_ptr: *mut c_void 35 | } 36 | 37 | impl UnsafeClient { 38 | fn new() -> Self { 39 | unsafe { 40 | UnsafeClient { 41 | client_ptr: td_json_client_create() 42 | } 43 | } 44 | } 45 | 46 | /// UNSAFE: the returned slice is invalidated upon the next call to execute or receive 47 | unsafe fn execute<'a>(&'a self, request: &str) -> Option<&'a str> { 48 | let crequest = CString::new(request).expect("null character in request string"); 49 | let answer = td_json_client_execute( 50 | self.client_ptr, 51 | crequest.as_ptr() as *const c_char 52 | ); 53 | 54 | let answer = answer as *const c_char; 55 | if answer == std::ptr::null() { 56 | return None; 57 | } 58 | let answer = CStr::from_ptr(answer); 59 | Some(answer.to_str().expect("tdlib sent invalid utf-8 string")) 60 | } 61 | 62 | fn send(&self, request: &str) { 63 | let crequest = CString::new(request).expect("null character in request string"); 64 | unsafe { 65 | td_json_client_send( 66 | self.client_ptr, 67 | crequest.as_ptr() as *const c_char 68 | ) 69 | } 70 | } 71 | 72 | /// UNSAFE: the returned slice is invalidated upon the next call to execute or receive 73 | unsafe fn receive<'a>(&'a self, timeout: Duration) -> Option<&'a str> { 74 | let timeout = timeout.as_secs() as f64; 75 | 76 | let answer = td_json_client_receive( 77 | self.client_ptr, 78 | timeout 79 | ); 80 | 81 | let answer = answer as *const c_char; 82 | if answer == std::ptr::null() { 83 | return None; 84 | } 85 | let answer = CStr::from_ptr(answer); 86 | 87 | Some(answer.to_str().expect("tdlib sent invalid utf-8 string")) 88 | } 89 | } 90 | 91 | impl Drop for UnsafeClient { 92 | fn drop(&mut self) { 93 | unsafe { 94 | td_json_client_destroy(self.client_ptr) 95 | } 96 | } 97 | } 98 | 99 | pub struct Client { 100 | inner: UnsafeClient, 101 | } 102 | 103 | impl Client { 104 | pub fn new() -> Self { 105 | Client { 106 | inner: UnsafeClient::new(), 107 | } 108 | } 109 | 110 | pub fn execute<'a>(&'a mut self, request: &str) -> Option<&'a str> { 111 | // SAFE because we are taking self by mutable referene 112 | unsafe { 113 | self.inner.execute(request) 114 | } 115 | } 116 | 117 | pub fn send(&self, request: &str) { 118 | self.inner.send(request) 119 | } 120 | 121 | pub fn receive<'a>(&'a mut self, timeout: Duration) -> Option<&'a str> { 122 | // SAFE because we are taking self by mutable referene 123 | unsafe { 124 | self.inner.receive(timeout) 125 | } 126 | } 127 | pub fn split(self) -> (SendClient, ReceiveClient) { 128 | let c = Arc::new(self.inner); 129 | let s = SendClient { 130 | inner: c.clone(), 131 | }; 132 | let r = ReceiveClient { 133 | inner: c.clone(), 134 | }; 135 | (s, r) 136 | } 137 | } 138 | 139 | #[derive(Clone)] 140 | pub struct SendClient { 141 | inner: Arc, 142 | } 143 | pub struct ReceiveClient { 144 | inner: Arc, 145 | } 146 | 147 | /// SAFE because the send method can be called by any thread 148 | unsafe impl Send for SendClient{} 149 | /// SAFE because the send method can be called by multiple threads at the same time 150 | unsafe impl Sync for SendClient{} 151 | 152 | impl SendClient { 153 | pub fn send(&self, request: &str) { 154 | self.inner.send(request); 155 | } 156 | } 157 | 158 | /// SAFE because the receive method can be called by any thread 159 | unsafe impl Send for ReceiveClient{} 160 | impl ReceiveClient { 161 | pub fn receive<'a>(&'a mut self, timeout: Duration) -> Option<&'a str> { 162 | // SAFE because we are taking self by mutable referene 163 | unsafe { 164 | self.inner.receive(timeout) 165 | } 166 | } 167 | } 168 | --------------------------------------------------------------------------------