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