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