ferogram_tl_parser/lib.rs
1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13#![cfg_attr(docsrs, feature(doc_cfg))]
14#![doc(html_root_url = "https://docs.rs/ferogram-tl-parser/0.3.7")]
15//! Parser for Telegram's Type Language (TL) schema files.
16//!
17//! This crate is part of [ferogram](https://crates.io/crates/ferogram), an async Rust
18//! MTProto client built by [Ankit Chaubey](https://github.com/ankit-chaubey).
19//!
20//! - Channel: [t.me/Ferogram](https://t.me/Ferogram)
21//! - Chat: [t.me/FerogramChat](https://t.me/FerogramChat)
22//!
23//! Converts raw `.tl` schema text into a [`tl::Definition`] AST. That AST is
24//! then consumed by `ferogram-tl-gen` to produce the Rust type bindings in
25//! `ferogram-tl-types`.
26//!
27//! Most users never touch this crate. It matters when you are upgrading the
28//! TL layer, writing a custom code generator, or doing schema introspection.
29//!
30//! # Usage
31//!
32//! ```rust
33//! use ferogram_tl_parser::parse_tl_file;
34//!
35//! let src = "user#12345678 id:long name:string = User;";
36//! for def in parse_tl_file(src) {
37//! let def = def.unwrap();
38//! println!("{} #{:08x}", def.name, def.id);
39//! }
40//! ```
41//!
42//! [`parse_tl_file`] returns an iterator of `Result<Definition, ParseError>`.
43//! It handles both constructor and function sections of a `.tl` file.
44//!
45//! # AST
46//!
47//! The main types are in the [`tl`] module:
48//! - [`tl::Definition`]: one parsed TL declaration (constructor or function).
49//! - [`tl::Parameter`]: a field name and type.
50//! - [`tl::Type`]: a TL type reference (with optional generic argument).
51//!
52//! [Type Language]: https://core.telegram.org/mtproto/TL
53#![deny(unsafe_code)]
54#![warn(missing_docs)]
55
56/// Parse error types for TL schema parsing.
57pub mod errors;
58mod iterator;
59/// Core TL schema types: definitions, parameters, types, flags, and categories.
60pub mod tl;
61mod utils;
62
63use errors::ParseError;
64use tl::Definition;
65
66/// Parses a complete TL schema file, yielding [`Definition`]s one by one.
67///
68/// Lines starting with `//` are treated as comments and skipped.
69/// The special `---functions---` and `---types---` section markers switch
70/// the [`tl::Category`] applied to the following definitions.
71///
72/// Returns an iterator of `Result<Definition, ParseError>` so callers can
73/// decide whether to skip or hard-fail on bad lines.
74pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> + '_ {
75 iterator::TlIterator::new(contents)
76}