| 1 | // Copyright 2020 - developers of the `grammers` project. |
| 2 | // Copyright 2021 - developers of the `tdlib-rs` project. |
| 3 | // Copyright 2024 - developers of the `tgt` and `tdlib-rs` projects. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
| 11 | //! This library provides a public interface to parse [Type Language] |
| 12 | //! definitions. |
| 13 | //! |
| 14 | //! It exports a single public method, [`parse_tl_file`] to parse entire |
| 15 | //! `.tl` files and yield the definitions it contains. This method will |
| 16 | //! yield [`Definition`]s containing all the information you would possibly |
| 17 | //! need to later use somewhere else (for example, to generate code). |
| 18 | //! |
| 19 | //! [Type Language]: https://core.telegram.org/mtproto/TL |
| 20 | //! [`parse_tl_file`]: fn.parse_tl_file.html |
| 21 | //! [`Definition`]: tl/struct.Definition.html |
| 22 | pub mod errors; |
| 23 | pub mod tl; |
| 24 | mod tl_iterator; |
| 25 | |
| 26 | use errors::ParseError; |
| 27 | use tl::Definition; |
| 28 | use 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 tdlib_rs_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 |
| 53 | pub fn parse_tl_file(contents: String) -> impl Iterator<Item = Result<Definition, ParseError>> { |
| 54 | TlIterator::new(contents) |
| 55 | } |