⚙️ layer-tl-gen
Build-time Rust code generator for Telegram's TL schema.
From TL AST to idiomatic Rust structs, enums, and traits — automatically.
📦 Installation
[]
= "0.4.6"
This crate is a build-dependency only. It runs during
cargo buildviabuild.rsand produces Rust source code. It is not linked into your final binary.
✨ What It Does
layer-tl-gen takes a parsed TL AST (from layer-tl-parser) and generates complete, idiomatic Rust source code — structs for constructors, enums for abstract types, and trait implementations for functions.
It runs at build time via build.rs, so the generated code is always in sync with the schema — no manual updates needed. Regeneration is automatic whenever the .tl file changes (via cargo:rerun-if-changed).
🏗️ What Gets Generated
For every TL definition in the schema, layer-tl-gen produces three categories of output:
1. Constructor → Rust struct + ser/de
peerUser#9db1bc6d user_id:long = Peer;
// in mod types
2. Abstract type → Rust enum + discriminated deserialization
peerUser = Peer;
peerChat = Peer;
peerChannel = Peer;
// in mod enums
// Deserializable dispatches on the 4-byte CRC32 constructor ID
3. Function → struct + RemoteCall impl
messages.sendMessage#545cd15a peer:InputPeer message:string random_id:long = Updates;
// in mod functions::messages
4. Optional derives and conversions
Depending on the Config flags passed to generate():
// impl_debug = true
// impl_from_type = true
// impl_from_enum = true
// name_for_id = true
💡 Usage in build.rs
// layer-tl-types/build.rs
use ;
Then in your lib.rs or generated.rs:
// include the generated file
include!;
⚙️ Config Options
🧩 Module Layout of Generated Code
generated.rs
├── mod types — one struct per TL constructor
├── mod enums — one enum per TL abstract type
└── mod functions
├── mod account
├── mod auth
├── mod channels
├── mod contacts
├── mod messages
├── mod payments
├── mod phone
├── mod photos
├── mod stickers
├── mod stories
├── mod updates
└── mod users
Each functions::* module mirrors the namespace prefix from the TL schema (messages.sendMessage → functions::messages::SendMessage).
🔗 Part of the layer stack
layer-tl-types (consumes the generated code)
└── layer-tl-gen ← you are here (generates at build time)
└── layer-tl-parser (parses the .tl schema)
📄 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