tdlib_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//! [`parse_tl_file`]: fn.parse_tl_file.html
19//! [`Definition`]: tl/struct.Definition.html
20pub mod errors;
21pub mod tl;
22mod tl_iterator;
23
24use errors::ParseError;
25use tl::Definition;
26use tl_iterator::TlIterator;
27
28/// Parses a file full of [Type Language] definitions.
29///
30/// # Examples
31///
32/// ```no_run
33/// use std::fs::File;
34/// use std::io::{self, Read};
35/// use tdlib_tl_parser::parse_tl_file;
36///
37/// fn main() -> std::io::Result<()> {
38/// let mut file = File::open("api.tl")?;
39/// let mut contents = String::new();
40/// file.read_to_string(&mut contents)?;
41///
42/// for definition in parse_tl_file(contents) {
43/// dbg!(definition);
44/// }
45///
46/// Ok(())
47/// }
48/// ```
49///
50/// [Type Language]: https://core.telegram.org/mtproto/TL
51pub fn parse_tl_file(contents: String) -> impl Iterator<Item = Result<Definition, ParseError>> {
52 TlIterator::new(contents)
53}