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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/// A 'recursive descent' parser for Doxygen XML files
mod builders;
use xml::{
attribute::OwnedAttribute,
reader::{EventReader, ParserConfig, XmlEvent},
};
use std::collections::HashMap;
use std::io::Read;
// === Types ===
#[derive(Debug, Clone)]
pub(crate) enum DoxygenType {
NameSpace(NameSpace),
Class(Class),
Unhandled,
}
#[derive(Debug, Clone)]
pub(crate) struct NameSpace {
pub(crate) name: String,
pub(crate) members: Vec<String>,
pub(crate) brief_description: Description,
pub(crate) detailed_description: Description,
}
#[derive(Debug, Clone)]
pub(crate) struct FunctionArgument {
pub(crate) name: String,
pub(crate) r#type: String,
pub(crate) default_value: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) enum Member {
Variable {
name: String,
r#type: String,
},
// What
/*
Enum {
}
*/
Function {
name: String,
args: Vec<FunctionArgument>,
return_type: String,
},
Unhandled,
}
#[derive(Debug, Clone)]
pub(crate) struct Class {
pub(crate) name: String,
pub(crate) inners: Vec<(String, String)>,
pub(crate) sections: HashMap<String, Vec<Member>>,
pub(crate) brief_description: Description,
pub(crate) detailed_description: Description,
}
// === Description ===
#[derive(Debug, Clone)]
pub(crate) struct Description {
pub(crate) inner: Vec<DescriptionNode>,
}
impl Description {
fn new() -> Self {
Self { inner: Vec::new() }
}
}
#[derive(Debug, Clone)]
pub(crate) enum DescriptionNode {
Text(String),
Title(String),
Para(Paragraph),
Sect1(String),
Internal(String),
}
#[derive(Debug, Clone)]
pub(crate) struct Paragraph {
pub(crate) inner: Vec<ParagraphNode>,
}
#[derive(Debug, Clone)]
pub(crate) enum ParagraphNode {
Text(String),
Ref(Ref),
}
#[derive(Debug, Clone)]
pub(crate) struct Ref {
pub(crate) id: String,
pub(crate) kind: RefKind,
pub(crate) text: String,
}
#[derive(Debug, Clone)]
pub(crate) enum RefKind {
Compound,
Member,
}
#[derive(Debug, Clone)]
enum DoxygenTypeKind {
NameSpace,
Unhandled,
}
trait TypeBuilder {
//const KIND: DoxygenTypeKind;
fn build(self: Box<Self>) -> DoxygenType;
fn name(&mut self, value: String);
fn brief_description(&mut self, description: Description);
fn detailed_description(&mut self, description: Description);
fn add_inner(&mut self, name: String, kind: String);
fn add_member(&mut self, section: String, member: Member);
}
trait FromXML {
fn from_xml(attributes: Vec<OwnedAttribute>, event_reader: &mut EventReader<impl Read>)
-> Self;
}
impl FromXML for Description {
fn from_xml(
_attributes: Vec<OwnedAttribute>,
mut event_reader: &mut EventReader<impl Read>,
) -> Self {
let mut inner = Vec::new();
while let Ok(event) = event_reader.next() {
match event {
XmlEvent::Characters(text) => inner.push(DescriptionNode::Text(text)),
XmlEvent::StartElement {
name, attributes, ..
} => inner.push(match name.local_name.as_str() {
"para" => {
DescriptionNode::Para(Paragraph::from_xml(attributes, &mut event_reader))
}
"title" => DescriptionNode::Title(event_simple(&mut event_reader)),
"sect1" => DescriptionNode::Sect1(event_simple(&mut event_reader)),
"internal" => DescriptionNode::Internal(event_simple(&mut event_reader)),
other => {
warn!("Description element not supported: {}", other);
continue;
}
}),
XmlEvent::EndElement { .. } => break,
other => {
warn!("Description event not supported: {:?}", other);
}
}
}
Self { inner }
}
}
impl FromXML for Paragraph {
fn from_xml(
_attributes: Vec<OwnedAttribute>,
mut event_reader: &mut EventReader<impl Read>,
) -> Self {
let mut inner = Vec::new();
while let Ok(event) = event_reader.next() {
match event {
XmlEvent::Characters(text) => inner.push(ParagraphNode::Text(text)),
XmlEvent::StartElement {
name, attributes, ..
} => inner.push(match name.local_name.as_str() {
"ref" => ParagraphNode::Ref(Ref::from_xml(attributes, &mut event_reader)),
other => {
warn!("Paragraph element not supported: {}", other);
continue;
}
}),
XmlEvent::EndElement { .. } => break,
other => {
warn!("Paragraph event not supported: {:?}", other);
}
}
}
Self { inner }
}
}
impl FromXML for Ref {
fn from_xml(
attributes: Vec<OwnedAttribute>,
mut event_reader: &mut EventReader<impl Read>,
) -> Self {
let mut id = None;
let mut kind = None;
for attr in attributes.into_iter() {
match attr.name.local_name.as_str() {
"refid" => id = Some(attr.value),
"kindref" => {
kind = Some(match attr.value.as_str() {
"compound" => RefKind::Compound,
"member" => RefKind::Member,
other => {
warn!("Ref kind not supported: {}", other);
RefKind::Compound
}
});
}
other => {
warn!("Ref element not supported: {}", other);
}
}
}
let text = event_simple(&mut event_reader);
Ref {
id: id.unwrap(),
kind: kind.unwrap(),
text,
}
}
}
/// Parse a type from a XML Doxygen file
pub(crate) fn parse_type(reader: impl Read) -> Vec<DoxygenType> {
let mut event_reader = EventReader::new(reader);
match event_reader.next().unwrap() {
XmlEvent::StartDocument { .. } => (),
_ => panic!("XML file does not begin with document"),
}
match event_reader.next().unwrap() {
XmlEvent::StartElement { name, .. } => {
if name.local_name != "doxygen" {
panic!("XML file does not start with a 'doxygen' element");
}
}
_ => panic!("XML file does not start with a 'doxygen' element"),
}
let mut result = Vec::new();
while let Ok(event) = event_reader.next() {
match dbg!(event) {
XmlEvent::StartElement {
name, attributes, ..
} => {
if name.local_name != "compounddef" {
panic!("'doxygen' is not a sequence of 'compounddef' elements");
}
result.push(event_compounddef(attributes, &mut event_reader));
}
XmlEvent::Whitespace(_) => (),
XmlEvent::EndElement { .. } => break,
_ => panic!("'doxygen' is not a sequence of 'compounddef' elements"),
}
}
// Unnecessarily strict?
match event_reader.next().unwrap() {
XmlEvent::EndDocument => (),
_ => panic!("XML file does not end after 'doxygen' element"),
}
dbg!(result)
}
fn event_compounddef(
attributes: Vec<OwnedAttribute>,
mut event_reader: &mut EventReader<impl Read>,
) -> DoxygenType {
let kind = attributes
.into_iter()
.find_map(|attr| {
if attr.name.local_name == "kind" {
Some(attr.value)
} else {
None
}
})
.unwrap();
let mut builder = builders::builder_for(kind);
while let Ok(event) = event_reader.next() {
match dbg!(event) {
XmlEvent::StartElement {
name, attributes, ..
} => match name.local_name.as_str() {
"compoundname" => {
let name = event_simple(&mut event_reader);
builder.name(name);
}
"briefdescription" => {
let brief_description = Description::from_xml(attributes, &mut event_reader);
builder.brief_description(brief_description);
}
"detaileddescription" => {
let detailed_description = Description::from_xml(attributes, &mut event_reader);
builder.detailed_description(detailed_description);
}
"location" => {
event_simple(&mut event_reader);
debug!("Do something?");
}
"sectiondef" => {
event_section(&mut builder, &mut event_reader);
}
other_name if is_inner_type(other_name) => {
let inner = event_simple(&mut event_reader);
builder.add_inner(inner, other_name.to_string());
}
_other_name => {
event_ignore(&mut event_reader);
}
},
XmlEvent::Whitespace(_) => (),
XmlEvent::EndElement { .. } => break,
_ => panic!("Unhandled XML event"),
}
}
builder.build()
}
/// Returns true if the given XML Element is a reference to another inner type.
///
/// Corresponds to the "refType" XSD type in `compound.xsd`
fn is_inner_type(element_name: &str) -> bool {
match element_name {
"innerdir" | "innerfile" | "innerclass" | "innernamespace" | "innerpage" | "innergroup" => {
true
}
_ => false,
}
}
/// Get the text inside a simple, non recursive XML tag
fn event_simple(event_reader: &mut EventReader<impl Read>) -> String {
let result;
match dbg!(event_reader.next().unwrap()) {
XmlEvent::Characters(tmp_result) => {
result = tmp_result;
}
XmlEvent::EndElement { .. } => return "".to_string(),
_ => panic!("Simple XML event is not so simple"),
}
match dbg!(event_reader.next().unwrap()) {
XmlEvent::EndElement { .. } => (),
_ => panic!("Simple XML event is not so simple"),
}
result
}
fn event_ignore(mut event_reader: &mut EventReader<impl Read>) {
loop {
let event = event_reader.next().unwrap();
match dbg!(event) {
XmlEvent::StartElement { .. } => event_ignore(&mut event_reader),
XmlEvent::EndElement { .. } => break,
_ => (),
}
}
}
fn event_section(
mut builder: &mut Box<dyn TypeBuilder>,
mut event_reader: &mut EventReader<impl Read>,
) {
loop {
let event = event_reader.next().unwrap();
match dbg!(event) {
XmlEvent::StartElement {
name, attributes, ..
} => match name.local_name.as_str() {
"memberdef" => {
let member = event_member(attributes, &mut event_reader);
builder.add_member("bla".to_owned(), member);
}
_ => panic!("Unhandled thing"),
},
XmlEvent::Whitespace(_) => (),
XmlEvent::EndElement { .. } => break,
_ => panic!("Unknown section XML event"),
}
}
}
fn event_member(
attributes: Vec<OwnedAttribute>,
mut event_reader: &mut EventReader<impl Read>,
) -> Member {
let kind = attributes
.into_iter()
.find_map(|attr| {
if attr.name.local_name == "kind" {
Some(attr.value)
} else {
None
}
})
.unwrap();
match kind.as_str() {
"variable" => event_member_variable(&mut event_reader),
_ => {
event_ignore(&mut event_reader);
Member::Unhandled
}
}
}
fn event_member_variable(mut event_reader: &mut EventReader<impl Read>) -> Member {
let mut member_name = None;
let mut r#type = None;
loop {
let event = event_reader.next().unwrap();
match dbg!(event) {
XmlEvent::StartElement { name, .. } => match name.local_name.as_str() {
"name" => member_name = Some(event_simple(&mut event_reader)),
"definition" => r#type = Some(event_simple(&mut event_reader)),
_ => event_ignore(&mut event_reader),
},
XmlEvent::Whitespace(_) => (),
XmlEvent::EndElement { .. } => break,
_ => panic!("Unknown member XML event"),
}
}
Member::Variable {
name: member_name.unwrap(),
r#type: r#type.unwrap(),
}
}
|