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
// A struct
#[derive(Debug)]
pub struct XMLStruct {
name: String, // The name of the struct
pub fields: Vec<XMLField>, // Fields in the struct
}
// A field
#[derive(Debug, Clone)]
pub struct XMLField {
pub name: String, // The name of the field
pub field_type: String, // The type of the field
}
fn create_structs(reader: &mut Reader<&[u8]>) -> HashMap<String, XMLStruct> {
// Stack of structs being constructed
let mut stack: Vec<XMLStruct> = Vec::new();
// Finalized structs
let mut structs: HashMap<String, XMLStruct> = HashMap::new();
loop {
match reader.read_event() {
// Handle a start tag:
Ok(Start(ref e)) => {
// Create a new struct for this element
// Push the struct onto the stack
// If there's a parent struct, add this struct as a field to the parent struct
},
// Handle a self-closing tag:
Ok(Empty(ref e)) => {
// Create a new struct for this element
// If there's a parent struct, add this struct as a field to the parent struct
},
// Handle an end tag:
Ok(End(ref e)) => {
// Pop the current struct from the stack
// Update the finalized structs with the current struct
// Merge fields if the struct already exists in the finalized collection of structs
},
// End of XML, exit the loop
Ok(Eof) => break,
// Handle parsing errors
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
// Handle other events
_ => (),
}
}
structs
}