Skip to main content

layer_tl_parser/
lib.rs

1//! Parser for Telegram's [Type Language] (TL) schema files.
2//!
3//! This crate converts raw `.tl` text into a structured [`Definition`] AST
4//! which can then be used by code-generators (see `layer-tl-gen`).
5//!
6//! # Quick start
7//!
8//! ```rust
9//! use layer_tl_parser::parse_tl_file;
10//!
11//! let src = "user#12345 id:long name:string = User;";
12//! for def in parse_tl_file(src) {
13//!     println!("{:#?}", def.unwrap());
14//! }
15//! ```
16//!
17//! [Type Language]: https://core.telegram.org/mtproto/TL
18
19#![deny(unsafe_code)]
20#![warn(missing_docs)]
21
22/// Parse error types for TL schema parsing.
23pub mod errors;
24pub mod tl;
25mod iterator;
26mod utils;
27
28use errors::ParseError;
29use tl::Definition;
30
31/// Parses a complete TL schema file, yielding [`Definition`]s one by one.
32///
33/// Lines starting with `//` are treated as comments and skipped.
34/// The special `---functions---` and `---types---` section markers switch
35/// the [`tl::Category`] applied to the following definitions.
36///
37/// Returns an iterator of `Result<Definition, ParseError>` so callers can
38/// decide whether to skip or hard-fail on bad lines.
39pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> + '_ {
40    iterator::TlIterator::new(contents)
41}