Skip to main content

layer_tl_parser/
lib.rs

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