๐ฆ layer-tl-types
Auto-generated Rust types for all Telegram API Layer 223 constructors, functions and enums.
2,295 TL constructors. Every Telegram type, function and enum โ as idiomatic Rust.
๐ฆ Installation
[]
= "0.1.1"
# With MTProto low-level types too:
= { = "0.1.1", = ["tl-mtproto"] }
โจ What It Does
layer-tl-types is the type system for the entire layer stack. It takes Telegram's .tl schema file (the source of truth for every type and function in the Telegram API) and, at build time, generates idiomatic Rust structs, enums, and trait implementations for all of them.
The result is a fully type-safe, zero-surprise Rust representation of 2,295 Telegram API definitions, with Serialize / Deserialize support baked in.
๐ Generated Structure
// Every TL constructor becomes a Rust struct
// e.g. message#9cb490e9 id:int from_id:Peer ... = Message;
// Every abstract TL type becomes a Rust enum
// e.g. message | messageEmpty | messageService = Message
// Every TL function becomes a struct implementing RemoteCall
// e.g. messages.sendMessage#... peer:InputPeer message:string ... = Updates
๐ง Feature Flags
| Feature | Default | Description |
|---|---|---|
tl-api |
โ | Telegram API schema (api.tl) โ all high-level types |
tl-mtproto |
โ | MTProto internal schema (mtproto.tl) โ low-level types |
impl-debug |
โ | #[derive(Debug)] on all generated types |
impl-from-type |
โ | From<types::T> for enums::E conversions |
impl-from-enum |
โ | TryFrom<enums::E> for types::T conversions |
name-for-id |
โ | name_for_id(u32) -> Option<&'static str> lookup |
impl-serde |
โ | serde::Serialize / Deserialize on all types |
๐ก Usage Examples
Serializing a TL request
use ;
let req = SendMessage ;
// Serialize to TL wire bytes
let bytes = req.serialize;
Deserializing a TL response
use ;
let mut cur = from_slice;
let msg = deserialize?;
match msg
Using RemoteCall
use ;
// The return type is encoded in the trait โ no guessing
let req = GetState ;
// req implements RemoteCall<Return = enums::updates::State>
Type conversions
use ;
// types โ enums (From)
let peer: Peer = PeerUser .into;
// enums โ types (TryFrom)
if let Ok = try_from
๐ Updating the TL Schema
To update to a newer Telegram API layer:
# 1. Replace the schema file
# 2. Rebuild โ code regenerates automatically
The build.rs invokes layer-tl-gen at compile time, so all types stay in sync with the schema automatically.
๐ Part of the layer stack
layer-client
โโโ layer-mtproto
โโโ layer-tl-types โ you are here
โโโ layer-tl-gen (build-time codegen)
โโโ layer-tl-parser (schema parser)
๐ License
Licensed under either of, at your option:
- MIT License โ see LICENSE-MIT
- Apache License, Version 2.0 โ see LICENSE-APACHE
๐ค Author
Ankit Chaubey github.com/ankit-chaubey ยท ankitchaubey.in ยท ankitchaubey.dev@gmail.com