layer-tl-parser 0.2.0

Parser for Telegram's Type Language (.tl) schema files
Documentation
  • Coverage
  • 100%
    46 out of 46 items documented1 out of 4 items with examples
  • Size
  • Source code size: 28.57 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • ร˜ build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • ankit-chaubey/layer
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ankit-chaubey

๐Ÿ” layer-tl-parser

A parser for Telegram's TL (Type Language) schema files.

Crates.io License: MIT OR Apache-2.0 Rust

Turns raw .tl schema text into a structured AST โ€” the foundation of the entire type system.


๐Ÿ“ฆ Installation

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

โœจ What It Does

The Telegram API is defined in a custom schema language called TL (Type Language). Every type, constructor, and function in the Telegram protocol is described in .tl files โ€” api.tl for the high-level API, mtproto.tl for the low-level MTProto protocol.

layer-tl-parser reads these schema files and produces a structured AST that can be consumed by code generators (like layer-tl-gen) to produce native Rust types.


๐Ÿ“ TL Schema Format

A .tl file looks like this:

// A constructor (type definition)
message#9cb490e9 flags:# out:flags.1?true id:int peer_id:Peer message:string = Message;

// A function (RPC call)
messages.sendMessage#545cd15a peer:InputPeer message:string random_id:long = Updates;

// An abstract type
inputPeerEmpty#7f3b18ea = InputPeer;
inputPeerSelf#7da07ec9 = InputPeer;
inputPeerUser#dde8a54c user_id:long access_hash:long = InputPeer;

layer-tl-parser parses all of this into typed Rust structures.


๐Ÿ—๏ธ AST Structure

/// A single parsed TL definition (constructor or function)
pub struct Definition {
    pub name:       String,           // e.g. "message"
    pub id:         Option<u32>,      // e.g. 0x9cb490e9 (CRC)
    pub params:     Vec<Parameter>,   // field definitions
    pub ty:         Type,             // return / abstract type
    pub category:   Category,         // Type or Function
}

pub struct Parameter {
    pub name: String,     // field name
    pub ty:   ParameterType,
}

// Parameter types cover: bare, boxed, flags, conditional, generic
pub enum ParameterType {
    Flags,
    Normal { ty: Type, flag: Option<Flag> },
    Repeated { params: Vec<Parameter> },
}

๐Ÿ’ก Usage

use layer_tl_parser::{parse_tl_file, tl::Category};

let schema = std::fs::read_to_string("api.tl").unwrap();
let definitions = parse_tl_file(&schema).unwrap();

for def in &definitions {
    match def.category {
        Category::Type => {
            println!("Constructor: {} โ†’ {}", def.name, def.ty.name);
        }
        Category::Function => {
            println!("Function:    {} โ†’ {}", def.name, def.ty.name);
        }
    }
}

println!("Total definitions: {}", definitions.len());

๐Ÿ”— Part of the layer stack

layer-tl-types  (generated types)
โ””โ”€โ”€ layer-tl-gen    (code generator, uses parser)
    โ””โ”€โ”€ layer-tl-parser    โ† you are here

๐Ÿ“„ 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