๐ layer-tl-parser
A parser for Telegram's TL (Type Language) schema files.
Turns raw .tl schema text into a structured AST โ the foundation of the entire type system.
๐ฆ Installation
[]
= "0.4.6"
Note: Most users don't depend on this crate directly. It is used internally by
layer-tl-gen(as abuild-dependency) which in turn is used bylayer-tl-typesto generate all Telegram API types at build time.
โจ 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 with multiple constructors
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)
/// Parameter types cover: bare, boxed, flags field, conditional, repeated
๐ก Usage
use ;
let schema = read_to_string.unwrap;
let definitions = parse_tl_file.unwrap;
for def in &definitions
let types_count = definitions.iter.filter.count;
let fns_count = definitions.iter.filter.count;
println!;
๐ How the Iterator Works
The parser exposes a streaming iterator (TlIterator) over definitions, not a batch parse. This lets the code generator process one definition at a time without holding the full AST in memory.
parse_tl_file() collects the iterator into a Vec<Definition> for convenience. If you need streaming, use the iterator directly:
use TlIterator;
let schema = read_to_string.unwrap;
for def in new
โ ๏ธ Error Handling
Parse errors are returned as layer_tl_parser::ParseError with the line content that failed to parse. Unknown or malformed tokens cause the iterator to stop and return an error rather than silently skipping.
๐ Part of the layer stack
layer-tl-types (generated types, user-facing)
โโโ layer-tl-gen (code generator, build-time)
โโโ layer-tl-parser โ you are here
๐ 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