Skip to main content

layer_tl_parser/
lib.rs

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