tsproto_structs/
messages.rs1use heck::*;
2use once_cell::sync::Lazy;
3use serde::Deserialize;
4
5use crate::*;
6
7pub const DATA_STR: &str =
8 include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/declarations/Messages.toml"));
9
10pub static DATA: Lazy<MessageDeclarations> = Lazy::new(|| toml::from_str(DATA_STR).unwrap());
11
12#[derive(Deserialize, Debug, Clone)]
13#[serde(deny_unknown_fields)]
14pub struct MessageDeclarations {
15 pub fields: Vec<Field>,
16 pub msg_group: Vec<MessageGroup>,
17}
18
19impl MessageDeclarations {
20 pub fn get_message_opt(&self, name: &str) -> Option<&Message> {
21 self.msg_group.iter().flat_map(|g| g.msg.iter()).find(|m| m.name == name)
22 }
23
24 pub fn get_message(&self, name: &str) -> &Message {
25 self.get_message_opt(name).unwrap_or_else(|| panic!("Cannot find message {}", name))
26 }
27
28 pub fn get_message_group(&self, msg: &Message) -> &MessageGroup {
29 for g in &self.msg_group {
30 for m in &g.msg {
31 if std::ptr::eq(m, msg) {
32 return g;
33 }
34 }
35 }
36 panic!("Cannot find message group for message");
37 }
38
39 pub fn get_field(&self, mut map: &str) -> &Field {
40 if map.ends_with('?') {
41 map = &map[..map.len() - 1];
42 }
43 if let Some(f) = self.fields.iter().find(|f| f.map == map) {
44 f
45 } else {
46 panic!("Cannot find field {}", map);
47 }
48 }
49
50 pub fn uses_lifetime(&self, msg: &Message) -> bool {
51 for a in &msg.attributes {
52 let field = self.get_field(a);
53 if field.get_type(a).unwrap().to_ref(true).uses_lifetime() {
54 return true;
55 }
56 }
57 false
58 }
59}
60
61#[derive(Deserialize, Debug, Clone, Eq, Hash, PartialEq)]
62#[serde(deny_unknown_fields)]
63pub struct Field {
64 pub map: String,
66 pub ts: String,
68 pub pretty: String,
70 #[serde(rename = "type")]
71 pub type_s: String,
72 #[serde(rename = "mod")]
73 pub modifier: Option<String>,
74}
75
76#[derive(Deserialize, Debug, Clone)]
77#[serde(deny_unknown_fields)]
78pub struct MessageGroup {
79 pub default: MessageGroupDefaults,
80 pub msg: Vec<Message>,
81}
82
83#[derive(Deserialize, Debug, Clone)]
84#[serde(deny_unknown_fields)]
85pub struct MessageGroupDefaults {
86 pub s2c: bool,
87 pub c2s: bool,
88 pub response: bool,
89 pub low: bool,
90 pub np: bool,
91}
92
93#[derive(Deserialize, Debug, Clone)]
94#[serde(deny_unknown_fields)]
95pub struct Message {
96 pub name: String,
98 pub notify: Option<String>,
100 pub attributes: Vec<String>,
101}
102
103impl Field {
104 pub fn get_rust_name(&self) -> String { self.pretty.to_snake_case() }
105
106 pub fn get_type(&self, a: &str) -> Result<RustType> {
108 RustType::with(&self.type_s, a.ends_with('?'), None, false, self.is_array())
109 }
110
111 pub fn is_opt(&self, msg: &Message) -> bool { !msg.attributes.iter().any(|a| *a == self.map) }
113
114 pub fn is_array(&self) -> bool { self.modifier.as_ref().map(|s| s == "array").unwrap_or(false) }
115}