Skip to main content

ferogram_tl_gen/
lib.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13#![deny(unsafe_code)]
14#![cfg_attr(docsrs, feature(doc_cfg))]
15#![doc(html_root_url = "https://docs.rs/ferogram-tl-gen/0.3.7")]
16//! Build-time code generator from a parsed TL schema to Rust source files.
17//!
18//! This crate is part of [ferogram](https://crates.io/crates/ferogram), an async Rust
19//! MTProto client built by [Ankit Chaubey](https://github.com/ankit-chaubey).
20//!
21//! - Channel: [t.me/Ferogram](https://t.me/Ferogram)
22//! - Chat: [t.me/FerogramChat](https://t.me/FerogramChat)
23//!
24//! Use this crate from a `build.rs` script to regenerate `ferogram-tl-types`
25//! when you want to update to a new Telegram API layer. You feed it a parsed
26//! TL schema (from `ferogram-tl-parser`) and it writes the Rust source for
27//! `types`, `functions`, and `enums` modules.
28//!
29//! # Usage from build.rs
30//!
31//! ```no_run
32//! use ferogram_tl_gen::{Config, Outputs, generate};
33//! use ferogram_tl_parser::parse_tl_file;
34//! use std::fs;
35//!
36//! fn main() {
37//!     let schema = fs::read_to_string("tl/api.tl").unwrap();
38//!     let defs: Vec<_> = parse_tl_file(&schema)
39//!         .filter_map(|r| r.ok())
40//!         .collect();
41//!
42//!     let config = Config::default();
43//!     let mut outputs = Outputs {
44//!         common:    Vec::new(),
45//!         types:     Vec::new(),
46//!         functions: Vec::new(),
47//!         enums:     Vec::new(),
48//!     };
49//!     generate(&defs, &config, &mut outputs).unwrap();
50//!
51//!     let mut combined = outputs.common;
52//!     combined.extend(outputs.types);
53//!     combined.extend(outputs.functions);
54//!     combined.extend(outputs.enums);
55//!     fs::write("src/generated.rs", combined).unwrap();
56//! }
57//! ```
58//!
59//! # What it generates
60//!
61//! - `types` module: one `struct` per TL bare constructor, with named fields.
62//! - `functions` module: one `struct` per TL function, implementing `RemoteCall`.
63//! - `enums` module: one `enum` per TL boxed type, with one variant per constructor.
64//!
65//! All types implement `Serializable`. All enums implement `Deserializable`.
66//!
67//! Most users never touch this crate. It only matters when you are upgrading
68//! the TL layer or maintaining a fork of `ferogram-tl-types`.
69mod codegen;
70mod grouper;
71mod metadata;
72mod namegen;
73
74pub use codegen::{Config, Outputs, generate};