Skip to main content

ferogram_tl_parser/
lib.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3//
4// ferogram: async Telegram MTProto client in Rust
5// https://github.com/ankit-chaubey/ferogram
6//
7// Based on layer: https://github.com/ankit-chaubey/layer
8// Follows official Telegram client behaviour (tdesktop, TDLib).
9//
10// If you use or modify this code, keep this notice at the top of your file
11// and include the LICENSE-MIT or LICENSE-APACHE file from this repository:
12// https://github.com/ankit-chaubey/ferogram
13
14#![cfg_attr(docsrs, feature(doc_cfg))]
15#![doc(html_root_url = "https://docs.rs/ferogram-tl-parser/0.4.6")]
16//! Parser for Telegram's [Type Language] (TL) schema files.
17//!
18//! This crate converts raw `.tl` text into a structured [`Definition`] AST
19//! which can then be used by code-generators (see `ferogram-tl-gen`).
20//!
21//! # Quick start
22//!
23//! ```rust
24//! use ferogram_tl_parser::parse_tl_file;
25//!
26//! let src = "user#12345 id:long name:string = User;";
27//! for def in parse_tl_file(src) {
28//! println!("{:#?}", def.unwrap());
29//! }
30//! ```
31//!
32//! [Type Language]: https://core.telegram.org/mtproto/TL
33
34#![deny(unsafe_code)]
35#![warn(missing_docs)]
36
37/// Parse error types for TL schema parsing.
38pub mod errors;
39mod iterator;
40pub mod tl;
41mod utils;
42
43use errors::ParseError;
44use tl::Definition;
45
46/// Parses a complete TL schema file, yielding [`Definition`]s one by one.
47///
48/// Lines starting with `//` are treated as comments and skipped.
49/// The special `---functions---` and `---types---` section markers switch
50/// the [`tl::Category`] applied to the following definitions.
51///
52/// Returns an iterator of `Result<Definition, ParseError>` so callers can
53/// decide whether to skip or hard-fail on bad lines.
54pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> + '_ {
55    iterator::TlIterator::new(contents)
56}