Skip to main content

layer_tl_parser/
lib.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// NOTE:
5// The "Layer" project is no longer maintained or supported.
6// Its original purpose for personal SDK/APK experimentation and learning
7// has been fulfilled.
8//
9// Please use Ferogram instead:
10// https://github.com/ankit-chaubey/ferogram
11// Ferogram will receive future updates and development, although progress
12// may be slower.
13//
14// Ferogram is an async Telegram MTProto client library written in Rust.
15// Its implementation follows the behaviour of the official Telegram clients,
16// particularly Telegram Desktop and TDLib, and aims to provide a clean and
17// modern async interface for building Telegram clients and tools.
18
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![doc(html_root_url = "https://docs.rs/layer-tl-parser/0.5.0")]
21//! Parser for Telegram's [Type Language] (TL) schema files.
22//!
23//! This crate converts raw `.tl` text into a structured [`Definition`] AST
24//! which can then be used by code-generators (see `layer-tl-gen`).
25//!
26//! # Quick start
27//!
28//! ```rust
29//! use layer_tl_parser::parse_tl_file;
30//!
31//! let src = "user#12345 id:long name:string = User;";
32//! for def in parse_tl_file(src) {
33//! println!("{:#?}", def.unwrap());
34//! }
35//! ```
36//!
37//! [Type Language]: https://core.telegram.org/mtproto/TL
38
39#![deny(unsafe_code)]
40#![warn(missing_docs)]
41
42/// Parse error types for TL schema parsing.
43pub mod errors;
44mod iterator;
45pub mod tl;
46mod utils;
47
48use errors::ParseError;
49use tl::Definition;
50
51/// Parses a complete TL schema file, yielding [`Definition`]s one by one.
52///
53/// Lines starting with `//` are treated as comments and skipped.
54/// The special `---functions---` and `---types---` section markers switch
55/// the [`tl::Category`] applied to the following definitions.
56///
57/// Returns an iterator of `Result<Definition, ParseError>` so callers can
58/// decide whether to skip or hard-fail on bad lines.
59pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> + '_ {
60    iterator::TlIterator::new(contents)
61}