tl_types/
lib.rs

1//! This library contains the Rust definitions for Telegram's [`types`] and
2//! [`functions`] in the form of `struct` and `enum`. All of them implement
3//! [`Serializable`], and by default only types implement [`Deserializable`].
4//!
5//! If you're here because you want to invoke the "raw API" methods Telegram
6//! has to offer, use the search or read through the available [`functions`]
7//! to find out what "parameters" such remote call needs. Then, create an
8//! instance of it and pass it to some higher level library that can talk
9//! with Telegram's servers.
10//!
11//! To preserve compatibility with older applications, the API has a concept
12//! of "layers", where new layers can change, remove or add new  definitions.
13//! The [`LAYER`] constant indicates which of the many layers was used to
14//! generate the definitions present in this version of this crate.
15//!
16//! # Usage
17//!
18//! The primary purpose is using these definitions to create requests
19//! (known as [`functions`]) which can be serialized and sent to Telegram.
20//! Note that this crate has no way to "invoke" any of these requests,
21//! this is to be done by a higher-level crate.
22//!
23//! All of the requests implement [`RemoteCall`]. This trait's associated
24//! type indicates what type the response from Telegram will be when invoked.
25//!
26//! After opening one of the many [`types`], you can inspect their fields
27//! to figure out what data Telegram will return.
28//!
29//! # Features
30//!
31//! The default feature set is intended to make the use of the library
32//! comfortable, and not intended to be minimal in code size. If you need
33//! a smaller library or are concerned about build-times, consider disabling
34//! some of the default features.
35//!
36//! The default feature set includes:
37//!
38//! * `impl-debug`.
39//! * `impl-from-enum`.
40//! * `impl-from-type`.
41//! * `tl-api`.
42//!
43//! The available features are:
44//!
45//! * `deserializable-functions`: implements [`Deserializable`] for
46//!   [`functions`]. This might be of interest for server implementations,
47//!   which need to deserialize the client's requests, but is otherwise not
48//!   required.
49//!
50//! * `impl-debug`: implements `Debug` for the generated code.
51//!
52//! * `impl-from-enum`: implements `TryFrom<Enum> for Type`.
53//!
54//! * `impl-from-type`: implements `From<Type> for Enum`.
55//!
56//! * `tl-api`: generates code for the `api.tl`.
57//!   This is what high-level libraries often need.
58//!
59//! * `tl-mtproto`: generates code for the `mtproto.tl`.
60//!   Only useful for low-level libraries.
61//!
62//! [`types`]: types/index.html
63//! [`functions`]: functions/index.html
64//! [`RemoteCall`]: trait.RemoteCall.html
65//! [`Serializable`]: trait.Serializable.html
66//! [`Deserializable`]: trait.Deserializable.html
67//! [`LAYER`]: constant.LAYER.html
68pub mod deserialize;
69pub mod serialize;
70
71pub use deserialize::{Cursor, Deserializable};
72pub use serialize::Serializable;
73
74/// This struct represents the concrete type of a vector, that is,
75/// `vector` as opposed to the type `Vector`. This bare type is less
76/// common, so instead of creating a enum for `Vector` wrapping `vector`
77/// as Rust's `Vec` (as we would do with auto-generated code),
78/// a new-type for `vector` is used instead.
79#[derive(Clone, Debug, PartialEq)]
80pub struct RawVec<T>(pub Vec<T>);
81
82/// This struct represents an unparsed blob, which should not be deserialized
83/// as a bytes string. Used by functions returning generic objects which pass
84/// the underlying result without any modification or interpretation.
85#[derive(Clone, Debug, PartialEq)]
86pub struct Blob(pub Vec<u8>);
87
88impl From<Vec<u8>> for Blob {
89    fn from(value: Vec<u8>) -> Self {
90        Self(value)
91    }
92}
93
94/// Anything implementing this trait is identifiable by both ends (client-server)
95/// when performing Remote Procedure Calls (RPC) and transmission of objects.
96pub trait Identifiable {
97    /// The unique identifier for the type.
98    const CONSTRUCTOR_ID: u32;
99}
100
101/// Structures implementing this trait indicate that they are suitable for
102/// use to perform Remote Procedure Calls (RPC), and know what the type of
103/// the response will be.
104pub trait RemoteCall: Serializable {
105    /// The type of the "return" value coming from the other end of the
106    /// connection.
107    type Return: Deserializable;
108}