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
#[derive(Debug, Clone)]
pub struct SectionList(pub &'static [Section]);
impl SectionList {
pub fn messages(&self) -> DynamicMessageList {
let messages: Vec<Message> = self.0.iter().flat_map(|s| s.messages.0.to_vec()).collect();
DynamicMessageList(messages)
}
pub fn map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Section) -> String,
{
self.0.iter().map(f).collect()
}
pub fn flat_map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Section) -> Vec<String>,
{
self.0.iter().flat_map(f).collect()
}
}
#[derive(Debug, Clone)]
pub struct Section {
pub name: &'static str,
pub messages: BuiltinMessageList,
}
#[derive(Debug, Clone)]
pub struct BuiltinMessageList(pub &'static [Message]);
impl BuiltinMessageList {
pub fn map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Message) -> String,
{
self.0.iter().map(f).collect()
}
pub fn flat_map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Message) -> Vec<String>,
{
self.0.iter().flat_map(f).collect()
}
}
#[derive(Debug, Clone)]
pub struct DynamicMessageList(pub Vec<Message>);
impl DynamicMessageList {
pub fn map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Message) -> String,
{
self.0.iter().map(f).collect()
}
pub fn flat_map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&Message) -> Vec<String>,
{
self.0.iter().flat_map(f).collect()
}
}
#[derive(Debug, Clone)]
pub struct Message {
pub camelcase_name: &'static str,
pub fields: MessageFieldList,
pub comment: &'static [&'static str],
}
impl Message {
pub fn render_comment(&self, prefix: &str, offset: usize) -> String {
crate::comment::Comment::new(&self.comment, prefix).to_string(offset)
}
pub fn upper_name(&self) -> String {
crate::helpers::camel_case_to_underscored(self.camelcase_name).to_uppercase()
}
pub fn lower_name(&self) -> String {
crate::helpers::camel_case_to_underscored(self.camelcase_name).to_lowercase()
}
}
#[derive(Debug, Clone)]
pub struct MessageFieldList(pub &'static [MessageField]);
impl MessageFieldList {
pub fn map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&MessageField) -> String,
{
self.0.iter().map(f).collect()
}
pub fn flat_map<F>(&self, f: F) -> Vec<String>
where
F: Fn(&MessageField) -> Vec<String>,
{
self.0.iter().flat_map(f).collect()
}
}
#[derive(Debug, Clone)]
pub struct MessageField {
pub name: &'static str,
pub field_type: MessageFieldType,
pub comment: &'static [&'static str],
}
impl MessageField {
pub fn render_comment(&self, prefix: &str, offset: usize) -> String {
crate::comment::Comment::new(&self.comment, prefix).to_string(offset)
}
}
#[derive(Debug, Clone)]
pub enum MessageFieldType {
Str,
Byte,
}