1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use clap::Parser;
use indexmap::{IndexMap, IndexSet};
use rustdoc_types::{Crate, Item, ItemEnum, Visibility};
use serde::Deserialize;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    /// Paths to json files generated by `rustdoc -p <crate> --output-format json`
    #[clap(short, long, value_parser)]
    pub json: Vec<String>,

    /// The path to toml config file which contains the types to be wrapped and overrides
    #[clap(short, long, value_parser)]
    pub config: String,

    /// if true the excluded methods will show up as commented out code with reasons for exclusion
    #[clap(long)]
    pub print_errors: bool,
}

#[derive(Deserialize, Debug)]
pub struct Config {
    #[serde(skip_deserializing, default)]
    pub types: IndexMap<String, Newtype>,

    #[serde(rename = "types")]
    pub types_: Vec<Newtype>,

    pub imports: String,
    pub other: String,

    pub lua_api_defaults: String,

    /// Describes the set of non generic things which are representible
    /// as simple lua types and don't need UserData proxies
    pub primitives: IndexSet<String>,

    pub manual_lua_types: Vec<ManualLuaType>,
}

#[derive(Deserialize, Debug)]
pub struct ManualLuaType {
    pub name: String,

    /// whether or not to exclude this type from the type walker list
    #[serde(default)]
    pub dont_process: bool,

    /// the name exposed to scripts as global
    #[serde(default)]
    pub proxy_name: String,

    /// whether or not to include global proxy
    #[serde(default)]
    pub include_global_proxy: bool,

    /// whether or not to use a dummy instance (DummyTypeName)
    #[serde(default)]
    pub use_dummy_proxy: bool,
}

#[derive(Deserialize, Debug, Hash, PartialEq, Eq)]
pub struct Newtype {
    #[serde(rename = "type")]
    pub type_: String,

    /// Override type-level docstring
    pub doc: Option<String>,

    #[serde(default)]
    pub source: Source,

    #[serde(default)]
    pub lua_methods: Vec<String>,

    #[serde(default)]
    pub derive_flags: Vec<String>,

    #[serde(default)]
    pub import_path: String,

    #[serde(default)]
    pub traits: Vec<TraitMethods>,
}

#[derive(Deserialize, Debug, PartialEq, Eq, Hash, Default)]
pub struct TraitMethods {
    pub name: String,
    pub import_path: String,
}

#[derive(Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct Source(pub String);

impl Default for Source {
    fn default() -> Self {
        Self("bevy".to_string())
    }
}

impl Newtype {
    /// Returns true if this Type:
    /// - describes the given item element
    /// - if the element is fully described in the source crate
    /// - if the element is a struct or enum
    /// - if the element has no generics
    pub fn matches_result(&self, item: &Item, source: &Crate) -> bool {
        match &item.inner {
            ItemEnum::Struct(s) => {
                if !s.generics.params.is_empty() {
                    return false;
                }
            }
            ItemEnum::Enum(_) => {}
            _ => return false,
        };

        if source.external_crates.contains_key(&item.crate_id) {
            return false;
        };

        matches!(item.visibility, Visibility::Public)
    }
}