layer-tl-types 0.1.2

Auto-generated Telegram TL types โ€” constructors, functions, enums (Layer 222)
Documentation

๐Ÿ“ฆ layer-tl-types

Auto-generated Rust types for all Telegram API Layer 222 constructors, functions and enums.

Crates.io License: MIT OR Apache-2.0 Rust TL Layer

2,295 TL constructors. Every Telegram type, function and enum โ€” as idiomatic Rust.


๐Ÿ“ฆ Installation

[dependencies]
layer-tl-types = "0.1.1"

# With MTProto low-level types too:
layer-tl-types = { version = "0.1.1", features = ["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;
pub mod types {
    pub struct Message {
        pub id: i32,
        pub from_id: Option<enums::Peer>,
        pub peer_id: enums::Peer,
        pub message: String,
        // ... all fields
    }
}

// Every abstract TL type becomes a Rust enum
// e.g.  message | messageEmpty | messageService = Message
pub mod enums {
    pub enum Message {
        Message(types::Message),
        Service(types::MessageService),
        Empty(types::MessageEmpty),
    }
}

// Every TL function becomes a struct implementing RemoteCall
// e.g.  messages.sendMessage#... peer:InputPeer message:string ... = Updates
pub mod functions {
    pub mod messages {
        pub struct SendMessage {
            pub peer: enums::InputPeer,
            pub message: String,
            // ...
        }
        impl RemoteCall for SendMessage {
            type Return = enums::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 layer_tl_types::{functions, Serializable};

let req = functions::messages::SendMessage {
    peer:     enums::InputPeer::PeerSelf,
    message:  "Hello!".to_string(),
    random_id: 12345,
    // ...
};

// Serialize to TL wire bytes
let bytes = req.serialize();

Deserializing a TL response

use layer_tl_types::{Cursor, Deserializable, enums};

let mut cur = Cursor::from_slice(&response_bytes);
let msg = enums::Message::deserialize(&mut cur)?;

match msg {
    enums::Message::Message(m) => println!("text: {}", m.message),
    enums::Message::Service(s) => println!("service action"),
    enums::Message::Empty(_)   => println!("empty"),
}

Using RemoteCall

use layer_tl_types::{RemoteCall, functions::updates::GetState};

// The return type is encoded in the trait โ€” no guessing
let req = GetState {};
// req implements RemoteCall<Return = enums::updates::State>

Type conversions

use layer_tl_types::{types, enums};

// types โ†’ enums (From)
let peer: enums::Peer = types::PeerUser { user_id: 123 }.into();

// enums โ†’ types (TryFrom)
if let Ok(user) = types::PeerUser::try_from(peer) {
    println!("user_id: {}", user.user_id);
}

๐Ÿ”„ Updating the TL Schema

To update to a newer Telegram API layer:

# 1. Replace the schema file
cp new-api.tl layer-tl-types/tl/api.tl

# 2. Rebuild โ€” code regenerates automatically
cargo build

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:


๐Ÿ‘ค Author

Ankit Chaubey github.com/ankit-chaubey ยท ankitchaubey.in ยท ankitchaubey.dev@gmail.com

๐Ÿ“ฆ github.com/ankit-chaubey/layer