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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
mod builder;
mod helpers;
use crate::Error;
use helpers::*;
use std::{collections::HashMap, path::PathBuf};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ParameterAttribute {
None,
Opt,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Type {
Option(String),
Named(String),
Unit,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Method {
pub has_self: bool,
pub name: String,
pub self_type: String,
pub parameters: Vec<(String, Type, ParameterAttribute)>,
pub return_type: Type,
pub documentation: String,
pub file: PathBuf,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Property {
pub name: String,
pub typ: Type,
pub documentation: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GdnativeClass {
pub name: String,
pub inherit: String,
pub documentation: String,
pub properties: Vec<Property>,
pub methods: Vec<Method>,
pub file: PathBuf,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Documentation {
pub name: String,
pub root_file: PathBuf,
pub root_documentation: String,
pub classes: HashMap<String, GdnativeClass>,
}
impl Documentation {
pub(crate) fn from_root_file(name: String, root_file: PathBuf) -> Result<Self, Error> {
use syn::visit::Visit;
let root_file_content = read_file_at(&root_file)?;
let mut builder = builder::DocumentationBuilder {
documentation: Self {
name,
root_file: root_file.clone(),
root_documentation: String::new(),
classes: HashMap::new(),
},
current_file: (root_file, true),
current_module: Vec::new(),
error: None,
};
let root_documentation = get_docs(&root_file_content.attrs);
for item in root_file_content.items {
builder.visit_item(&item);
if let Some(error) = builder.error.take() {
return Err(error);
}
}
builder.documentation.root_documentation = root_documentation;
Ok(builder.documentation)
}
}
impl GdnativeClass {
fn add_method(&mut self, method: &syn::ImplItemMethod, file: PathBuf) {
let syn::ImplItemMethod {
vis, attrs, sig, ..
} = method;
if !matches!(vis, syn::Visibility::Public(_)) {
return;
}
if !(attributes_contains(attrs, "method") || sig.ident == "new") {
return;
}
let has_self = sig.receiver().is_some();
let syn::Signature {
ident: method_name,
inputs,
output,
..
} = sig;
let mut parameters = inputs.into_iter();
parameters.next();
let parameters = {
let mut params = Vec::new();
for arg in parameters {
if let syn::FnArg::Typed(syn::PatType { attrs, pat, ty, .. }) = arg {
let arg_name = {
if let syn::Pat::Ident(syn::PatIdent { ident, .. }) = pat.as_ref() {
ident.to_string()
} else {
String::new()
}
};
params.push((
arg_name,
get_type_name(ty).unwrap_or_else(|| Type::Named("{ERROR}".to_string())),
if attributes_contains(attrs, "opt") {
ParameterAttribute::Opt
} else {
ParameterAttribute::None
},
))
}
}
params
};
let return_type = match output {
syn::ReturnType::Default => Type::Unit,
syn::ReturnType::Type(_, typ) => get_type_name(typ).unwrap_or(Type::Unit),
};
log::trace!(
"added method {}: parameters = {:?}, return = {:?}",
method_name,
parameters,
return_type
);
self.methods.push(Method {
has_self,
name: method_name.to_string(),
self_type: self.name.clone(),
parameters,
return_type,
documentation: get_docs(attrs),
file,
})
}
fn get_properties(&mut self, fields: &syn::FieldsNamed) {
for field in &fields.named {
if attributes_contains(&field.attrs, "property") {
let property = Property {
name: field
.ident
.as_ref()
.map(|ident| ident.to_string())
.unwrap_or_default(),
typ: get_type_name(&field.ty).unwrap_or(Type::Unit),
documentation: get_docs(&field.attrs),
};
log::trace!(
"added property '{}' of type {:?}",
property.name,
property.typ
);
self.properties.push(property)
}
}
}
}