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
use super::super::{GlobalElements, Tree};
use inflector::Inflector;
use std::io::Write;
/// Generate all structs in the global_elements.output file using all elements gathered from
/// the nuscr protocol.
pub(crate) fn generate_structs(
global_elements: &mut GlobalElements,
main_tree: &Tree,
already_generated: &mut Vec<String>,
comment_payload: bool,
) -> Result<(), Box<dyn std::error::Error>> {
match global_elements.output.as_mut() {
Some(generated_file) => {
// Generate the structs for the global_elements.payloads
if comment_payload {
writeln!(generated_file, "// Types of the payloads")?;
}
for payload in global_elements.payloads.iter() {
if !already_generated.contains(&(payload).to_title_case()) {
writeln!(
generated_file,
"struct {};",
payload
.to_title_case()
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
)?;
already_generated.push(payload.to_title_case().to_string());
}
}
for (name_message, payload) in main_tree.messages_with_payloads.iter() {
if !already_generated.contains(&(name_message).to_title_case()) {
if payload.is_empty() {
writeln!(
generated_file,
"struct {};",
name_message
.to_title_case()
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
)?;
} else {
writeln!(
generated_file,
"struct {} {{ payload: {} }}",
name_message
.to_title_case()
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>(),
payload
.to_title_case()
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
)?;
}
already_generated.push(name_message.to_title_case().to_string());
}
}
writeln!(generated_file)?;
for sub_tree in &main_tree.sub_trees {
generate_structs(global_elements, sub_tree, already_generated, false)?;
}
Ok(())
}
None => Err("Generated file was not initialised.".into()),
}
}