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
use heck::*;
use once_cell::sync::Lazy;
use serde::Deserialize;

use crate::*;

pub const DATA_STR: &str =
	include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/declarations/Messages.toml"));

pub static DATA: Lazy<MessageDeclarations> = Lazy::new(|| 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_message_group(&self, msg: &Message) -> &MessageGroup {
		for g in &self.msg_group {
			for m in &g.msg {
				if std::ptr::eq(m, msg) {
					return g;
				}
			}
		}
		panic!("Cannot find message group for message");
	}

	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);
		}
	}

	pub fn uses_lifetime(&self, msg: &Message) -> bool {
		for a in &msg.attributes {
			let field = self.get_field(a);
			if field.get_type(a).unwrap().to_ref(true).uses_lifetime() {
				return true;
			}
		}
		false
	}
}

#[derive(Deserialize, Debug, Clone, Eq, Hash, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Field {
	/// Internal name of this declarations file to map fields to messages.
	pub map: String,
	/// The name as called by TeamSpeak in messages.
	pub ts: String,
	/// The pretty name in PascalCase. This will be used for the fields in rust.
	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 {
	/// How we call this message.
	pub name: String,
	/// How TeamSpeak calls this message.
	pub notify: Option<String>,
	pub attributes: Vec<String>,
}

impl Field {
	pub fn get_rust_name(&self) -> String { self.pretty.to_snake_case() }

	/// Takes the attribute to look if it is optional
	pub fn get_type(&self, a: &str) -> Result<RustType> {
		RustType::with(&self.type_s, a.ends_with('?'), None, false, self.is_array())
	}

	/// Returns if this field is optional in the message.
	pub fn is_opt(&self, msg: &Message) -> bool { !msg.attributes.iter().any(|a| *a == self.map) }

	pub fn is_array(&self) -> bool { self.modifier.as_ref().map(|s| s == "array").unwrap_or(false) }
}