Build-time Rust code generator for Telegram's TL schema.
Runs during cargo build via build.rs. Takes a parsed TL AST from ferogram-tl-parser and writes Rust source to $OUT_DIR. Not a runtime dependency add it to [build-dependencies], not [dependencies].
[build-dependencies]
ferogram-tl-gen = "0.3.0"use ferogram_tl_gen::{generate, Config};
fn main() {
let out_dir = std::env::var("OUT_DIR").unwrap();
generate(
"tl/api.tl",
&format!("{out_dir}/generated_api.rs"),
Config {
impl_debug: true,
impl_from_type: true,
impl_from_enum: true,
impl_serde: false,
name_for_id: false,
},
).expect("TL code generation failed");
println!("cargo:rerun-if-changed=tl/api.tl");
}In lib.rs:
include!(concat!(env!("OUT_DIR"), "/generated_api.rs"));| Field | Description |
|---|---|
impl_debug |
#[derive(Debug)] on all types |
impl_from_type |
From<types::T> for enums::E |
impl_from_enum |
TryFrom<enums::E> for types::T |
impl_serde |
serde::Serialize / Deserialize |
name_for_id |
name_for_id(u32) -> Option<&'static str> CRC32 lookup |
For each TL constructor, generates a Rust struct with Serializable / Deserializable impls. For each abstract type, generates a Rust enum with discriminated deserialization on the 4-byte CRC32 ID. For each function, generates a struct implementing RemoteCall with the correct return type.
Module layout:
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 messages
└ ...
ferogram-tl-types (consumes generated code)
└ ferogram-tl-gen <-- here
└ ferogram-tl-parser
MIT or Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.
Ankit Chaubey - github.com/ankit-chaubey