tdlib_tl_gen/
lib.rs

1// Copyright 2020 - developers of the `grammers` project.
2// Copyright 2021 - developers of the `tdlib-rs` project.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! This module gathers all the code generation submodules and coordinates
11//! them, feeding them the right data.
12mod enums;
13mod functions;
14mod metadata;
15mod rustifier;
16mod types;
17
18use std::io::{self, Write};
19use tdlib_tl_parser::tl::{Definition, Type};
20
21/// Don't generate types for definitions of this type,
22/// since they are "core" types and treated differently.
23const SPECIAL_CASED_TYPES: [&str; 6] = ["Bool", "Bytes", "Int32", "Int53", "Int64", "Ok"];
24
25fn ignore_type(ty: &Type) -> bool {
26    SPECIAL_CASED_TYPES.iter().any(|&x| x == ty.name)
27}
28
29pub fn generate_rust_code(
30    file: &mut impl Write,
31    definitions: &[Definition],
32    gen_bots_only_api: bool,
33) -> io::Result<()> {
34    write!(
35        file,
36        "\
37         // Copyright 2020 - developers of the `grammers` project.\n\
38         // Copyright 2021 - developers of the `tdlib-rs` project.\n\
39         //\n\
40         // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or\n\
41         // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license\n\
42         // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your\n\
43         // option. This file may not be copied, modified, or distributed\n\
44         // except according to those terms.\n\
45         "
46    )?;
47
48    let metadata = metadata::Metadata::new(definitions);
49    types::write_types_mod(file, definitions, &metadata, gen_bots_only_api)?;
50    enums::write_enums_mod(file, definitions, &metadata, gen_bots_only_api)?;
51    functions::write_functions_mod(file, definitions, &metadata, gen_bots_only_api)?;
52
53    Ok(())
54}