Skip to main content

ferogram_tl_parser/
lib.rs

1/*
2 * Copyright (c) 2026 Ankit Chaubey <ankitchaubey.dev@gmail.com>
3 * https://github.com/ankit-chaubey
4 *
5 * Project: ferogram
6 * Website: https://ferogram.dev
7 *
8 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
9 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
10 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
11 * This file may not be copied, modified, or distributed except according
12 * to those terms.
13 */
14
15#![cfg_attr(docsrs, feature(doc_cfg))]
16#![doc(html_root_url = "https://docs.rs/ferogram-tl-parser/0.6.5")]
17//! Parser for Telegram's Type Language (TL) schema files.
18//!
19//! This crate is part of [ferogram](https://crates.io/crates/ferogram), an async Rust
20//! MTProto client built by [Ankit Chaubey](https://github.com/ankit-chaubey).
21//!
22//! - Channel: [t.me/Ferogram](https://t.me/Ferogram)
23//! - Chat: [t.me/FerogramChat](https://t.me/FerogramChat)
24//!
25//! Converts raw `.tl` schema text into a [`tl::Definition`] AST. That AST is
26//! then consumed by `ferogram-tl-gen` to produce the Rust type bindings in
27//! `ferogram-tl-types`.
28//!
29//! Most users never touch this crate. It matters when you are upgrading the
30//! TL layer, writing a custom code generator, or doing schema introspection.
31//!
32//! # Usage
33//!
34//! ```rust
35//! use ferogram_tl_parser::parse_tl_file;
36//!
37//! let src = "user#12345678 id:long name:string = User;";
38//! for def in parse_tl_file(src) {
39//!     let def = def.unwrap();
40//!     println!("{} #{:08x}", def.name, def.id);
41//! }
42//! ```
43//!
44//! [`parse_tl_file`] returns an iterator of `Result<Definition, ParseError>`.
45//! It handles both constructor and function sections of a `.tl` file.
46//!
47//! # AST
48//!
49//! The main types are in the [`tl`] module:
50//! - [`tl::Definition`]: one parsed TL declaration (constructor or function).
51//! - [`tl::Parameter`]: a field name and type.
52//! - [`tl::Type`]: a TL type reference (with optional generic argument).
53//!
54//! [Type Language]: https://core.telegram.org/mtproto/TL
55#![deny(unsafe_code)]
56#![warn(missing_docs)]
57
58/// Parse error types for TL schema parsing.
59pub mod errors;
60mod iterator;
61/// Core TL schema types: definitions, parameters, types, flags, and categories.
62pub mod tl;
63mod utils;
64
65use errors::ParseError;
66use tl::Definition;
67
68/// Parses a complete TL schema file, yielding [`Definition`]s one by one.
69///
70/// Lines starting with `//` are treated as comments and skipped.
71/// The special `---functions---` and `---types---` section markers switch
72/// the [`tl::Category`] applied to the following definitions.
73///
74/// Returns an iterator of `Result<Definition, ParseError>` so callers can
75/// decide whether to skip or hard-fail on bad lines.
76pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> + '_ {
77    iterator::TlIterator::new(contents)
78}