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
use crate::*;
use lazy_static::lazy_static;
use serde_derive::Deserialize;
pub const DATA_STR: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/declarations/Messages.toml"
));
lazy_static! {
pub static ref DATA: MessageDeclarations =
toml::from_str(DATA_STR).unwrap();
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct MessageDeclarations {
pub fields: Vec<Field>,
pub msg_group: Vec<MessageGroup>,
}
impl MessageDeclarations {
pub fn get_message_opt(&self, name: &str) -> Option<&Message> {
self.msg_group
.iter()
.flat_map(|g| g.msg.iter())
.find(|m| m.name == name)
}
pub fn get_message(&self, name: &str) -> &Message {
self.get_message_opt(name)
.unwrap_or_else(|| panic!("Cannot find message {}", name))
}
pub fn get_field(&self, mut map: &str) -> &Field {
if map.ends_with('?') {
map = &map[..map.len() - 1];
}
if let Some(f) = self.fields.iter().find(|f| f.map == map) {
f
} else {
panic!("Cannot find field {}", map);
}
}
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Field {
pub map: String,
pub ts: String,
pub pretty: String,
#[serde(rename = "type")]
pub type_s: String,
#[serde(rename = "mod")]
pub modifier: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct MessageGroup {
pub default: MessageGroupDefaults,
pub msg: Vec<Message>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct MessageGroupDefaults {
pub s2c: bool,
pub c2s: bool,
pub response: bool,
pub low: bool,
pub np: bool,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Message {
pub name: String,
pub notify: Option<String>,
pub attributes: Vec<String>,
}
impl Field {
pub fn get_rust_name(&self) -> String { to_snake_case(&self.pretty) }
pub fn get_rust_type(&self, a: &str, is_ref: bool) -> String {
let mut res = convert_type(&self.type_s, is_ref);
if self
.modifier
.as_ref()
.map(|s| s == "array")
.unwrap_or(false)
{
res = format!("Vec<{}>", res);
}
if a.ends_with('?') {
res = format!("Option<{}>", res);
}
res
}
pub fn is_opt(&self, msg: &Message) -> bool {
!msg.attributes.iter().any(|a| *a == self.map)
}
}