grammers_tl_parser/lib.rs
1// Copyright 2020 - developers of the `grammers` project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! This library provides a public interface to parse [Type Language]
10//! definitions.
11//!
12//! It exports a single public method, [`parse_tl_file`] to parse entire
13//! `.tl` files and yield the definitions it contains. This method will
14//! yield [`Definition`]s containing all the information you would possibly
15//! need to later use somewhere else (for example, to generate code).
16//!
17//! [Type Language]: https://core.telegram.org/mtproto/TL
18
19#![deny(unsafe_code)]
20
21pub mod errors;
22pub mod tl;
23mod tl_iterator;
24mod utils;
25
26use errors::ParseError;
27use tl::Definition;
28use tl_iterator::TlIterator;
29
30/// Parses a file full of [Type Language] definitions.
31///
32/// # Examples
33///
34/// ```no_run
35/// use std::fs::File;
36/// use std::io::{self, Read};
37/// use grammers_tl_parser::parse_tl_file;
38///
39/// fn main() -> std::io::Result<()> {
40/// let mut file = File::open("api.tl")?;
41/// let mut contents = String::new();
42/// file.read_to_string(&mut contents)?;
43///
44/// for definition in parse_tl_file(&contents) {
45/// dbg!(definition);
46/// }
47///
48/// Ok(())
49/// }
50/// ```
51///
52/// [Type Language]: https://core.telegram.org/mtproto/TL
53pub fn parse_tl_file(contents: &str) -> impl Iterator<Item = Result<Definition, ParseError>> {
54 TlIterator::new(contents)
55}